Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to notify CDialog 3

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hello all,
I have an object which contains some significant properties that a modeless CDialog needs to know about as soon as they are changed. These properties are modified via the main frame. I would like to know what is the best approach to notifying the CDialog of the changes. I experimented with overriding OnChildNotify() in the CDialog, because it get's called automatically by the parent window. Then in this function I just check the property to see if it has changed. It works fine, but this means that the property is checked every time this function is called, which is a alot. I would rather send a message from the property object to the CDialog and set up some sort of message handler to receive this notification. Not sure where to start. Can someone please give me some ideas?



many thanks,


Barry
 
Cool! Just one more question...
I can use this->SendMessage(WM_MYMESSAGE); from the CDialog (just to test) and it works fine, but how do I get a handle to the CDialog from anywhere in the application. I tried the following code, but it does not fire my message handler. None of the assertions fail, so it seems like I am getting the handle on the window. Any ideas?


thanks,

Barry

Code:
CWinApp* pApp = ::AfxGetApp();
ASSERT(pApp);

CWnd* pMainWin = pApp->GetMainWnd();
ASSERT(pMainWin);
    
CWnd* pChild = pMainWin->GetWindow(GW_CHILD);
ASSERT(pChild);
    
HWND hWin = pChild->GetSafeHwnd();
ASSERT(hWin);

SendMessage(hWin,WM_MYMESSAGE,0,0);
 
Barry,

If you have code that creates this dialog class object why do you need to use all of those generic framework functions to obtain a reference to the class you are creating? You should already have a reference to it, obviating the need to do all those accessor calls and casting.

However to answer your question directly. How do you know that
Code:
pMainWin->GetWindow(GW_CHILD);
returns the dialog window? You can test that using the Spy++ utility program to see if the HWND you are obtaining is the same as the actual dialog window handle.

-pete
 
"If you have code that creates this dialog class object why do you need to use all of those generic framework functions to obtain a reference to the class you are creating?"

Good point! I guess I was just interested in finding out how all this message stuff works. At least now I have some basic understanding of how to create my own message handler and post a message to it. It looking at what I need to do a little closer, I think I am better off just getting the reference to the class and make my function call directly and not do all this magical messaging. Thanks for your help!

BTW, The reason my SendMessage() did not work was because the CDialog was not a child after all, so I was getting the wrong window handle.


thanks,

Barry
 
On a side track;

It is not advisable to use WM_USER throughout an application, it's for use within a private window class.

WM_APP should be used to define messages to send around in the application.


The chance isn't very likely, but you might hit an already predefined message number in one of the window classes when using WM_USER and you will have a hell of a time debugging that one...

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top