11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4. Button<br />

ON_WM_LBUTTONUP()<br />

END_MESSAGE_MAP()<br />

Message WM_LBUTTONUP will be trapped to member function MCBitmapButton::OnLButtonUp(…),<br />

which has the following format:<br />

void MCBitmapButton::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

}<br />

We see that the mouse position is passed to parameter point of this function.<br />

Because the commands are generally handled in the parent window of the button, we need to resend<br />

mouse clicking information from the button to class CBtnDlg. In the sample application, this is<br />

implemented through sending a user- defined message.<br />

User-Defined Message<br />

User defined messages can be treated the same with other standard Windows messages: they can be<br />

sent from one window to another, and we can add message handlers for them. All user-defined messages<br />

must have a message ID equal to or greater than WM_USER.<br />

In the sample, a new message WM_BTNPOS is defined in file “MButton.h”:<br />

#define WM_BTNPOS WM_USER+1000<br />

By doing this, WM_BTNPOS becomes a message that can be used in the application. Please note that this<br />

message can not be sent to other applications. If we want to send user-defined message among different<br />

applications, we need to register that message to the system.<br />

In function MCBitmapButton::OnLButtonUp(…), user defined message WM_BTNPOS is sent to the parent<br />

window, with the current mouse position stored in LPARAM parameter:<br />

void MCBitmapButton::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

CBitmapButton::OnLButtonUp(nFlags, point);<br />

GetParent()->PostMessage<br />

(<br />

WM_BTNPOS, GetDlgCtrlID(), MAKELPARAM(point.x, point.y)<br />

);<br />

}<br />

First the default implementation of function OnLButtonUp(…)is called. Then a CWnd type pointer of<br />

parent window is obtained by calling function CWnd::GetParent(). Class CWnd has several member<br />

functions that can be used to send Windows message, the most commonly used ones are CWnd::<br />

SendMessage(…) and CWnd::PostMessage(…). The difference between the two functions is that after<br />

sending out the message, CWnd::SendMessage(…) does not return until the message has been processed by<br />

the target window, and CWnd::PostMessage(…) returns immediately after the message has been sent out. In<br />

the sample, function CWnd::PostMessage(…) is used to send WM_BTNPOS message.<br />

All messages in Windows have two parameters, WPARAM and LPARAM. For Win32 applications, both<br />

WPARAM and LPARAM are 32-bit integers. They can be used to send additional information.<br />

In <strong>MFC</strong>, usually message parameters are passed as arguments to message handlers, so they are rarely<br />

noticed. For example, for message WM_LBUTTONDOWN, its WPARAM parameter is used to indicate if any of<br />

CTRL, ALT, or SHIFT key is held down when the mouse button is up. In the message handler, this<br />

information is mapped to the first parameter of CWnd::OnLButtonUp(…). Again, its LPARAM parameter<br />

contains the information of current mouse position, which is mapped to the second parameter of<br />

CWnd::OnLButtonUp(…). Both CWnd::SendMessage(…) and CWnd::PostMessage(…) have three parameters,<br />

the first of which specifies message ID, and the rest two are WPARAM and LPARAM parameters. If we don’t<br />

want to send additional message, we can pass 0 to both of them.<br />

In the sample, we need to use both parameters: the parent window needs to know the control ID of the<br />

button; also, it needs to know the current mouse position.<br />

86

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!