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

However, since message WM_MOUSEMOVE will only be sent to a window when the mouse cursor is within<br />

it, it is difficult to be informed of the event that mouse has just left the button’s window. This is because<br />

once the mouse leaves, the button will not be able to receive WM_MOUSEMOVE message anymore.<br />

To help us solve this type of problems, Windows provides a technique that can be used to track<br />

mouse’s activities after it leaves a window. This technique is called Capture. By using this method, we<br />

could capture all the mouse-related messages to one specific window, no matter where the mouse is.<br />

We can call function CWnd::SetCapture() to set capture for a window. The capture can also be<br />

released by calling function ::ReleaseCapture(), which is a Win32 API function. Besides using this<br />

function, the capture can also be removed by the operating system under certain conditions. If this happens,<br />

the window that is losing capture will receive a WM_CAPTURECHANGED message.<br />

New Class<br />

Sample 4.7\Btn demonstrates how to implement “mouse sensitive button”. In the application, a new<br />

class MCSenButton is created for this purpose, and is defined as follows:<br />

class MCSenButton : public MCBitmapButton<br />

{<br />

public:<br />

MCSenButton();<br />

protected:<br />

BOOL m_bCheck;<br />

afx_msg void OnMouseMove(UINT, CPoint);<br />

afx_msg void OnCaptureChanged(CWnd *);<br />

DECLARE_DYNAMIC(MCSenButton)<br />

DECLARE_MESSAGE_MAP()<br />

};<br />

The class contains only a constructor, two message handlers, and a Boolean type variable m_bCheck.<br />

Variable m_bCheck will be used to indicate the button’s current state: it is TRUE if the button is currently<br />

highlighted, and is FALSE if the button is in the normal state. Within the constructor, this variable is<br />

initialized to FALSE:<br />

MCSenButton::MCSenButton() : MCBitmapButton()<br />

{<br />

m_bCheck=FALSE;<br />

}<br />

Functions MCSenButton::OnMouseMove(…) and MCSenButton::OnCaptureChanged(…) are message<br />

handlers for WM_MOUSEMOVE and WM_CAPTURECHANGED respectively. Like all other types of messages, their<br />

message mapping macros should be added in the implementation file:<br />

IMPLEMENT_DYNAMIC(MCSenButton, MCBitmapButton)<br />

BEGIN_MESSAGE_MAP(MCSenButton, MCBitmapButton)<br />

ON_WM_MOUSEMOVE()<br />

ON_WM_CAPTURECHANGED()<br />

END_MESSAGE_MAP()<br />

Here, ON_WM_MOUSEMOVE and ON_WM_CAPTURECHANGED are message mapping macros defined by <strong>MFC</strong>.<br />

The following two functions handle above two messages:<br />

void MCSenButton::OnCaptureChanged(CWnd *pWnd)<br />

{<br />

m_bCheck=FALSE;<br />

SetState(FALSE);<br />

Invalidate();<br />

}<br />

void MCSenButton::OnMouseMove(UINT nFlags, CPoint point)<br />

{<br />

if(m_bCheck == FALSE)<br />

{<br />

89

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

Saved successfully!

Ooh no, something went wrong!