Document Class and Dialog Windows :: MFC
-
Hi. I am working on a project that contains a formview class with multiple dialog windows visible. As the user make changes to each dialog box, I want to update the document class. I implemente a message solution. The dialog boxes update send messages to the view class. The view class update the document class. In general, the solution above is adequate. However, in this particular project, the dialog windows are visible in the formview. I would like to know is there other more elegant solutions? For example, is it possible to implement a GetDocument() function that returns a pointer to the document class inside the dialog boxes? As another example, how about passing the dialog boxes pointers to the document class? I do not want to implement these solution unless messages fail. Thus these solutions should be last resorts. Thanks, Kuphryn
-
Hi. I am working on a project that contains a formview class with multiple dialog windows visible. As the user make changes to each dialog box, I want to update the document class. I implemente a message solution. The dialog boxes update send messages to the view class. The view class update the document class. In general, the solution above is adequate. However, in this particular project, the dialog windows are visible in the formview. I would like to know is there other more elegant solutions? For example, is it possible to implement a GetDocument() function that returns a pointer to the document class inside the dialog boxes? As another example, how about passing the dialog boxes pointers to the document class? I do not want to implement these solution unless messages fail. Thus these solutions should be last resorts. Thanks, Kuphryn
I'm sure there are a million answers to your question. But I would stick to the message solution. It is the best way for windows to communicate. I would not pass pointers to class objects around to be used. That could be potentially dangerous. If the WPARAM and LPARAM parameters seem to limiting remember you can allocate memory (or objects; classes, structs, etc.) and pass the pointer to another window through a message.
if(IsWindow(hwnd))
{
CThing *pThing = new CThing;
PostMessage(hwnd, WM_NEW_THING, 0, (LPARAM)pThing);
}Just remember the receiver of the message is responible for deleting the memory.
long CMyWnd::OnNewThing(WPARAM wParam, LPARAM lParam)
{
ASSERT(lParam);
CThing *pThing = (CThing *)lParam;
...
delete pThing;
}One more thing, in the OnDestory method of the CWnd class make sure you have no more WM_NEW_THING messages in the queue. Their memory should be deleted if they are.
MSG msg;
while(::PeekMessage(&msg, (HWND)NULL, WM_NEW_THING, WM_NEW_THING, PM_REMOVE))
{
CThing *pThing = (CThing *)msg.lParam;
delete pThing;
}Hope this helps. Jonathan Craig www.mcw-tech.com
-
I'm sure there are a million answers to your question. But I would stick to the message solution. It is the best way for windows to communicate. I would not pass pointers to class objects around to be used. That could be potentially dangerous. If the WPARAM and LPARAM parameters seem to limiting remember you can allocate memory (or objects; classes, structs, etc.) and pass the pointer to another window through a message.
if(IsWindow(hwnd))
{
CThing *pThing = new CThing;
PostMessage(hwnd, WM_NEW_THING, 0, (LPARAM)pThing);
}Just remember the receiver of the message is responible for deleting the memory.
long CMyWnd::OnNewThing(WPARAM wParam, LPARAM lParam)
{
ASSERT(lParam);
CThing *pThing = (CThing *)lParam;
...
delete pThing;
}One more thing, in the OnDestory method of the CWnd class make sure you have no more WM_NEW_THING messages in the queue. Their memory should be deleted if they are.
MSG msg;
while(::PeekMessage(&msg, (HWND)NULL, WM_NEW_THING, WM_NEW_THING, PM_REMOVE))
{
CThing *pThing = (CThing *)msg.lParam;
delete pThing;
}Hope this helps. Jonathan Craig www.mcw-tech.com