Click here to Skip to main content
16,011,754 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Dll Problem Pin
Antti Keskinen15-Jul-04 8:32
Antti Keskinen15-Jul-04 8:32 
QuestionMaking objects talk with each other? Pin
0v3rloader15-Jul-04 4:26
0v3rloader15-Jul-04 4:26 
AnswerRe: Making objects talk with each other? Pin
Maximilien15-Jul-04 4:43
Maximilien15-Jul-04 4:43 
GeneralRe: Making objects talk with each other? Pin
0v3rloader15-Jul-04 4:59
0v3rloader15-Jul-04 4:59 
AnswerRe: Making objects talk with each other? Pin
Antti Keskinen15-Jul-04 9:18
Antti Keskinen15-Jul-04 9:18 
GeneralRe: Making objects talk with each other? Pin
0v3rloader15-Jul-04 10:02
0v3rloader15-Jul-04 10:02 
GeneralRe: Making objects talk with each other? Pin
0v3rloader15-Jul-04 10:12
0v3rloader15-Jul-04 10:12 
GeneralRe: Making objects talk with each other? Pin
Antti Keskinen15-Jul-04 11:36
Antti Keskinen15-Jul-04 11:36 
First, the message ID problem. Basically, you have two valid options:
1. Either decide beforehand the message IDs.
2. Use RegisterWindowMessage and create a handler for ALL messages for the parent window.

In approach #1, we cannot be sure that the message ID is available on the global (system-wide) scale. However, since we are operating on single-thread context (both the button and the form exist, and the message needs only be understood, in the context of the running thread), we can safely use any message ID we want, as long as it is in the form WM_USER + xxx, up to 0x7FFF. WM_USER signals a starting integer, above which (almost; see MSDN) all message ID numbers are available for use. Also, we must assume that you can either see or can decide all message IDs used in the application/project, so that there are no overlappings during the design phase.

In approach #2, we use RegisterWindowMessage to make sure that our message ID is unique on the system-wide scale. This function should be used only when you have two or more applications (running on seperate threads) that need to handle a certain customized message. Here, the message ID is bound to a string variable, and RegisterWindowMessage can be used to decipher the ID from the string, if the ID is registered already.

This leads us to solving your next problem, which is "How to inform the parent of the message ID ?".

Again, there are two ways of doing this:
1. During the design phase, decide the message ID (WM_USER + xxx) and use ON_MESSAGE -macro to capture the message in the parent
2. During run-time, use a class member variable (UINT) that stores the message ID.

Approach #1 is quite clear. All WM_USER -message handlers are similar. You can search the MSDN for "User-defined handlers" to get the actual C++ function declaration you must use. It's something like afx_msg LRESULT <memberFxn>(LPARAM arg1, WPARAM arg2), and it must be a member of the class.

In approach #2, the parent class is "listening" to all messages (override the virtual window procedure function, CWnd::WindowProc to capture all messages; remember to call the original CWnd::WindowProc in the end). When a message with a matching UINT is received, we provide a custom handling to it. This approach, as you might have guessed, doesn't rely on the message-macros. The reason is simple: there is no macro that would cover all messages, or messages with a varying ID (ON_REGISTERED_MESSAGE is a different issue).

Let's have a break from theory: here is an example on how to use approach #2:
// Assuming that our class is CMyFormView, derived from CFormView<DIV>

class CMyFormView : CFormView
{
   .
   .
   .<DIV>

public:
   void SetMessageID( UINT nID ) { this->nMessageID = nID; }
   UINT GetMessageID()           { return this->nMessageID; }<DIV>

private:
   // Because CFormView is derived from CWnd, the CWnd::WindowProc exists
   virtual LRESULT WindowProc( UINT message, LPARAM lParam, WPARAM wParam );
   UINT nMessageID;
};<DIV>

.
.
.<DIV>

LRESULT CMyFormView::WindowProc( UINT message, LPARAM lParam, WPARAM wParam )
{
   // First, let's see if it is our 'custom' message
   if ( message == this->nMessageID )
   {
      // Yup, so let's do something, perhaps use a member function or something ?
   }<DIV>

   // Our check is complete, so let's call the base class implementation to finish up. It checks through the message-maps and passes the control onwards
   return CFormView::WindowProc( message, lParam, wParam );
}
For the last question - about informing the identity of the sender - the solution is pretty simple. Almost every Windows message that roams out there has two parameters attached: an LPARAM and a WPARAM. These are 32-bit integers that can be used in any way you please. If you design your custom message so that it's ID is, say, WM_USER + 1001 and the sender's ID is stored in the LPARAM argument, then it should be pretty simple to receive and decipher this message, no matter which one of the above approaches you used to catch the message. Both the ON_MESSAGE -macro functions and the window procedure grant you access to the LPARAM and WPARAM arguments.

Enjoy Smile | :)

-Antti Keskinen

P.S. Perhaps I should write an article about this Blush | :O

----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
GeneralRe: Making objects talk with each other? Pin
0v3rloader15-Jul-04 11:46
0v3rloader15-Jul-04 11:46 
Generalpacked data strcutures Pin
Henry miller15-Jul-04 3:51
Henry miller15-Jul-04 3:51 
GeneralRe: packed data strcutures Pin
vmaltsev15-Jul-04 11:52
vmaltsev15-Jul-04 11:52 
GeneralRe: packed data strcutures Pin
Henry miller15-Jul-04 12:04
Henry miller15-Jul-04 12:04 
GeneralCInternetSession timeout Pin
fredvinzenz15-Jul-04 3:46
fredvinzenz15-Jul-04 3:46 
Generalwsprintf Pin
vikramlinux15-Jul-04 2:37
vikramlinux15-Jul-04 2:37 
GeneralRe: wsprintf Pin
V.15-Jul-04 2:47
professionalV.15-Jul-04 2:47 
GeneralRe: wsprintf Pin
vikramlinux15-Jul-04 2:56
vikramlinux15-Jul-04 2:56 
GeneralRe: wsprintf Pin
V.15-Jul-04 3:11
professionalV.15-Jul-04 3:11 
GeneralRe: wsprintf Pin
vikramlinux15-Jul-04 3:46
vikramlinux15-Jul-04 3:46 
GeneralRe: wsprintf Pin
David Crow15-Jul-04 3:23
David Crow15-Jul-04 3:23 
Generala threading question Pin
ns15-Jul-04 2:15
ns15-Jul-04 2:15 
GeneralRe: a threading question Pin
jmkhael15-Jul-04 3:12
jmkhael15-Jul-04 3:12 
GeneralRe: a threading question Pin
David Crow15-Jul-04 3:28
David Crow15-Jul-04 3:28 
GeneralRe: a threading question Pin
ns15-Jul-04 3:59
ns15-Jul-04 3:59 
GeneralRe: a threading question Pin
V.15-Jul-04 4:13
professionalV.15-Jul-04 4:13 
GeneralRe: a threading question Pin
David Crow15-Jul-04 4:19
David Crow15-Jul-04 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.