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 />

The button’s ID can be retrieved by calling function CWnd::GetDlgCtrlID(), it will be sent through<br />

WPARAM parameter to the button’s parent. The x and y coordinates of mouse cursor can be combined together<br />

to form an LPARAM parameter by using MAKELPARAM macro. Here, macro MAKELPARAM can combine two 16-<br />

bit numbers to form a 32-bit message. If we provide two 32-bit numbers, only the lower 16 bits will be<br />

used (Of course, screen coordinates won’t use more than 16 bits).<br />

The message is received and processed in class CBtnDlg. In <strong>MFC</strong>, general message can be mapped to a<br />

member function by using ON_MESSAGE macro. This type of message handler has two parameters, one for<br />

receiving WPARAM information and the other for receiving LPARAM information. Also, it must return a LONG<br />

type value.<br />

The following code fragment shows how member function OnBtnPos(…) is declared in class CBtnDlg<br />

(It will be used to receive WM_BTNPOS message):<br />

……<br />

……<br />

class CBtnDlg : public CDialog<br />

{<br />

protected:<br />

};<br />

afx_msg LONG OnBtnPos(UINT, LONG);<br />

DECLARE_MESSAGE_MAP()<br />

In the implementation file, ON_MESSAGE macro is added as follows:<br />

……<br />

BEGIN_MESSAGE_MAP(CBtnDlg, CDialog)<br />

ON_MESSAGE(WM_BTNPOS, OnBtnPos)<br />

END_MESSAGE_MAP()<br />

The control ID and the mouse information can be extracted within the message handler as follows:<br />

LONG CBtnDlg::OnBtnPos(UINT wParam, LONG lParam)<br />

{<br />

CPoint pt;<br />

UINT uID;<br />

uID=wParam;<br />

pt.x=LOWORD(lParam);<br />

pt.y=HIWORD(lParam);<br />

}<br />

return (LONG)TRUE;<br />

Sample<br />

Sample 4.6\Btn has a four-arrow bitmap button. First a button resource is added to the dialog template,<br />

whose ID is IDC_PLAY_POS and caption text is “PLAYPOS” (bitmaps will be loaded through automatic<br />

method). Two new bitmap resources “PLAYPOSU” and “PLAYPOSD” are also added to the application,<br />

which will be used to draw button’s “up” and “down” states.<br />

We need to know the sizes and the positions of four arrows within the bitmap button so we can judge if<br />

the mouse cursor is over any of the arrows. Within class CBtnDlg, a CRect type array with size of 4 is<br />

declared for this purpose. Their values are initialized in function CBtnDlg::OnInitDialog(). Also an<br />

MCBitmapButton type variable m_btnPlayPos is declared to implement this new button:<br />

……<br />

……<br />

……<br />

……<br />

class CBtnDlg : public CDialog<br />

{<br />

protected:<br />

};<br />

MCBitmapButton m_btnPlayPos;<br />

CRect m_rectBtnPos[4];<br />

87

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

Saved successfully!

Ooh no, something went wrong!