-
June 14th, 2004, 09:35 PM
#1
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!
-
June 15th, 2004, 12:14 AM
#2
what are your
class AA, class BB
I think for sending messages you must have a handle to destination window. You need to provide more information.
By the way are you using new here....
[CODE]HWND *viewhandle = new HWND[\CODE]
Hope, you must be deleting this memory. But, i think there is no need to pointers when all the time you are accessing their value only.
[CODE]*viewhandle = GetSafeHwnd()[\CODE]
Its dangerous, try to avoid this.
Last edited by Amit Sebiz; June 15th, 2004 at 12:16 AM.
-
June 15th, 2004, 12:36 AM
#3
hi,
try to use this code...
if both class AA and class BB are derived from CWnd then you can use
CWnd *pWnd = GetParent();
pWnd->SendMessage(..);
rajs
-
June 15th, 2004, 01:19 AM
#4
Or you might use AfxGetMainWnd as in
AfxGetMainWnd()->SendMessage(WM_COMMAND, IDM_QUAT);
Regards,
Terry
-
June 15th, 2004, 08:25 AM
#5
Re: Sending messages with ::PostMessage
Removed by myself on account of bad advice.
Last edited by Bond; June 15th, 2004 at 09:02 AM.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
June 15th, 2004, 08:58 AM
#6
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:
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();
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.
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.
-
June 15th, 2004, 03:07 PM
#7
Hi, i'm trying to do it this way:
CWnd *pWnd = GetParent();
pWnd->SendMessage(..);
But both classes aren't derived from CWnd, so i did that manually (they are normal classes, not dialogs or stuff)
Well, as they are not dialogs, there is no message map in that class. Should i do one manually, copying from a dialog class??
gstercken:
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.
I'll try to answer that by explaining myself better:
Well, class AA is a variable, and inside it there's a pointer to class BB. With new, in a function from class AA i call new to the pointer of class BB, to allocate memory.
I want to send a message from class BB to class AA, so i can call a function from class AA to delete what's been allocated to that pointer from class AA which is a variable which points to a BB object. Because BB runs in the background, so when something happens (don't worry about that) i want to call a delete from class AA.
Class AA includes class BB, so i can't do the opposite (class BB including class AA) If i could, i would call from class BB a funtion from class AA to do the delete.
-
June 15th, 2004, 03:53 PM
#8
If you try to use CWnd to send messages, he CWnd - object have to be a 'real' window (with an underlying HWND). Simply deriving from CWnd doesn't do the job, you have to create a 'windows' - window.
As I understand your problem you want to do the following:
class AA
{
...
private:
BB *pBB;
public:
void CreateBB ()
{
pBB = new BB;
}
...
};
class BB
{
...
void TellAAToDeleteMe ()
{
// this is the point where monkey jumps into the water
}
};
Why don't you give the BB class a pointer to the parent class AA, which the BB class may use to inform the parent?
class BB
{
public:
BB (AA *pParent)
{
pAA = pParent;
}
void TellAAToDeleteMe ()
{
pAA->BBWantsToDie ();
}
private:
AA *pAA;
};
-
June 15th, 2004, 04:06 PM
#9
YES!!! you understood me lzinggl! THANKSS
Now it's me who doesn't understand your solution.
this:
BB (AA *pParent)
{
pAA = pParent;
}
is a constructor, right?
and BBWantsToDie is a function from AA, right?
pAA->BBWantsToDie ();
well, i think i understand now
-
June 15th, 2004, 05:16 PM
#10
:(
i have a problem.
i can't create this variable:
AA *pAA;
because from BB i don't know AA (there's no include AA in BB's header file, because i've included BB in AA's header file)
-
June 16th, 2004, 03:49 AM
#11
Re: :(
Originally posted by kfaday
i have a problem.
i can't create this variable:
AA *pAA;
because from BB i don't know AA (there's no include AA in BB's header file, because i've included BB in AA's header file)
That's where the forward declaration I mentioned earlier comes in:
Code:
class AA; // Forward declaration
class BB
{
AA* m_pAA;
}
-
June 16th, 2004, 07:26 AM
#12
thanks, i didn't know about that. i'll try
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|