Sending messages with ::PostMessage
Hi!
i'm used to sending messages from a view class (in mfc), so first i get the window
Code:
HWND *viewhandle = new HWND;
*viewhandle = GetSafeHwnd();
copy the viewhandle to a variable of the object, and in a function, when i want to send a message, i call:
Code:
::PostMessage(*viewhandle, WM_ONWHATEVER, 0, 0);
That works fine. Now, i want to send a message from a class to another, and there are no windows involved.
I want to do this: class AA includes class BB. And i want to use a function of class AA from class BB, but class BB doesn't see class AA. So i want to send a message
But now i can't use GetSafeHwnd. How could i do that?
thanks!
Re: Sending messages with ::PostMessage
Removed by myself on account of bad advice. :)
Re: Sending messages with ::PostMessage
You seem to think be seeking an overly complicated solution for a very simple and common problem. A few notes:
Quote:
Originally posted by kfaday
i'm used to sending messages from a view class (in mfc), so first i get the window
Code:
HWND *viewhandle = new HWND;
*viewhandle = GetSafeHwnd();
As others already said, there's absolutely no need to declate a pointer to a handle and create a handle dynamically on the heap. The following code is fine and has the same effect (actually, it uses less time and memory, and is safer):
Code:
HWND hView = GetSafeHwnd();
Quote:
Originally posted by kfaday
copy the viewhandle to a variable of the object, and in a function, when i want to send a message, i call:
Code:
::PostMessage(*viewhandle, WM_ONWHATEVER, 0, 0);
Note that there is an important difference between sending and posting a message. With SendMessage(), the call is synchronous - the receiving window processes the message directly, after that, SendMessage() returns. PostMessage(), OTOH, adds the message to the window's message queue and returns immediately - the receiving window will process the message as soon as it can, but not immediately.
Quote:
Originally posted by kfaday
Now, i want to send a message from a class to another, and there are no windows involved.
I want to do this: class AA includes class BB. And i want to use a function of class AA from class BB, but class BB doesn't see class AA. So i want to send a message
But now i can't use GetSafeHwnd. How could i do that?
What do you mean by class BB doesn't "see" class AA? If it's just a matter of knowing the class, use a forward declaration. However, BB will also need a reference or pointer to a concete instance of AA (or at least a to an interface class which AA implements) to call a member function, and it depends on your design how the instance of those classes are created and how they get to know each other.