• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

about Dialog Box

I had a "modal" Dialog Box that I displayed it like:

         CDialogDlg dialog;

and the dialog works perfectly.

But Now I want to change from "Modal" to "Modeless" then I did for displaying:

        CDialogDlg * dialog;
     dialog = new CDialogDlg();
     if(dialog != NULL)
          BOOL ret = dialog->Create(IDD_DIALOG1,this);
          if(!ret)   //Create failed.
          AfxMessageBox("Error creating Dialog");
            AfxMessageBox("Error Creating Dialog Object");     }

Then the dialog is created perfectly but when I press one button of this dialog that calls to the "CDC:MoveTo" function appears one assert error. To debug it, the error appear in the line:
VERIFY(::MoveToEx(m_hAttribDC, x, y, &point));
of CDC:MoveTo definition.

The question is: Why when the Dialog is Modal everything working perfectly, but when the dialog os Modeless appears this error?

Thanks in advance.
1 Solution
How are you getting DC? I did somehing like this:

void CTwoDlg::OnButton3()
     CDC * dc = this ->GetDC();    

     dc ->MoveTo(100, 100);
     dc ->LineTo(200, 200);

... end it is working fine.
Kent OlsenData Warehouse Architect / DBACommented:
vadik's solution seems sound.

A couple of points:

If a DialogBox isn't modal, it isn't really a DialogBox.  Oh, it may LOOK like a DialogBox, but it's really just another open window (form).

The purpose of a DialogBox is to pause the task (thread) and receive interaction.  If you allow the task to continue executing by opening the form (intended to be a DialogBox) in a non-modal fashion, the originating task continues to execute and a fair amount of additional handshake is necessary to even detect that the pseudo-DiaglogBox has closed and left data lying around that is to be acted upon by the originating task.

Thhis is code snippet from DLGCORE.CPP. I don't see here what a modeless DialogBox is not rallr DialogBox :(

Sorry for my English...

BOOL CDialog::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
     ASSERT(HIWORD(lpszTemplateName) == 0 ||

     m_lpszTemplateName = lpszTemplateName;  // used for help
     if (HIWORD(m_lpszTemplateName) == 0 && m_nIDHelp == 0)
          m_nIDHelp = LOWORD((DWORD)m_lpszTemplateName);

#ifdef _DEBUG
     if (!_AfxCheckDialogTemplate(lpszTemplateName, FALSE))
          ASSERT(FALSE);          // invalid dialog template name
          PostNcDestroy();        // cleanup if Create fails too soon
          return FALSE;
#endif //_DEBUG

     HINSTANCE hInst = AfxFindResourceHandle(lpszTemplateName, RT_DIALOG);
     HRSRC hResource = ::FindResource(hInst, lpszTemplateName, RT_DIALOG);
     HGLOBAL hTemplate = LoadResource(hInst, hResource);
     BOOL bResult = CreateIndirect(hTemplate, pParentWnd, hInst);

     return bResult;
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Kent OlsenData Warehouse Architect / DBACommented:

A DialogBox is a communication between the application and the user, just as the Experts Exchange board is a dialog between its members.

As the name dialog implies, there is a two-way communication, with each participant taking turns.

A DialogBox is a request for input.  It is the Windows application's equivalent to reading from stdin.

It is also the reason that a DialogBox normally executes as a modal object.  The application needs data to continue (a file name, a service name, etc) so it pauses waiting for input.  Once the input has been accepted the application continues.

While you can program your application to treat a DialogBox as an asyncronous object, there is a lot more coding that has to occur for it to interact meaningfully with your application.  It's much easier to use the mechanism that comes with the modal behavior.

If you really want an asyncronous object, you probably want a separate form (Window).  The coding is probably easier if you create an asyncronous form and let it create the modal dialog box.  This will allow you full GUI control over the application and the appearance of a non-modal DialogBox.

You are right. Dialog box is "object" for communications between application's and user's...

I'm not sure you are right. From technical point of view you are completelly not right - the only difference between modal and modeless dialog boxes is how they process messages.

From UI point - look at the modeless dialog box "Project Options" in Visual Studio .NET - it is the dialog box but you can change things in the main window as well. Try to tell to Microsoft that this is not a dialog box - they will have a lot of fun :)))

The last point - the question was technical, and there is a good answer on it by Vadik. All your "bla-bla-bla" are not related to the subj.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Andrew,

Please note that the very first line of my very first response was to credit Vadik on his technical response.  He offered a sound technical solution.

To me, the original question suggested that Javieruco is new to Windows (GUI) programming.  The very question, "why does this work when executed modally but not modelessly?" didn't ask anyone to "debug this code" -- it asked for an explanation.  Vadik told him how to solve the problem, I attempted to warn him of the issues that he could expect.

Yes, most DialogBoxes can be executed modally or modelessly by calling the appropriate method.  Yes, a modeless DialogBox can be made to "feel" like a Modal DialogBox by setting the StayOnTop property.  Yes.  Yes.  Yes.  There is very little difference in the DialogBox when displayed in either manner.

But I stand by my original statement.  The behavior of most DialogBoxes is such that their execution is syncronous -- modal.  Additional programming is required for the calling application to detect and process data received from a modeless execution.  Much of the behavior of a modeless DialogBox resembles a separate form/Window more than it does a modal DialogBox.

Language is a barrier in this kind of forum.  This is compounded by the very nature of the people in this business -- communication is often a problem even when English is a person's primary language.  Coming to the party late and shooting the place up isn't very productive.

Sorry if my answer offended you.  Take a Valium.

No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to vadik: Grade A
(honorable mention to Kdo for keeping his cool)

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now