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

SetCapture();<br />

m_bCheck=TRUE;<br />

SetState(TRUE);<br />

Invalidate();<br />

}<br />

else<br />

{<br />

CRect rect;<br />

}<br />

}<br />

GetClientRect(rect);<br />

if(!rect.PtInRect(point))<br />

{<br />

ReleaseCapture();<br />

m_bCheck=FALSE;<br />

SetState(FALSE);<br />

Invalidate();<br />

}<br />

In function MCSenButton::OnMouseMove(…), if m_bCheck is FALSE, it means the button is in the<br />

normal state. In this case, we need to set mouse capture, change the button to “highlighted” state, and<br />

redraw the button. If the button is currently highlighted, we need to check the current position of mouse<br />

cursor, if it has moved out of the button window, we should resume button’s normal state, and redraw the<br />

button. Here, CButton::SetState(…) is used to set the button to different states (it will cause the bitmap<br />

button to use the corresponding bitmap), and CWnd::Invalidate() is used to cause the button to be<br />

redrawn.<br />

In function MCSenButton::OnCaptureChanged(…), we need to change m_bCheck back to FALSE, and<br />

resume button’s normal state.<br />

Implementation<br />

In the sample, four new buttons are added to the application. The IDs of these new buttons are<br />

IDC_MOUSE_SEN_1, IDC_MOUSE_SEN_2, IDC_MOUSE_SEN_3 and IDC_MOUSE_SEN_4 respectively. An<br />

MCSenButton type array m_btnBmp (The array size is 4) is declared in class CBtnDlg and initialized in<br />

function CBtnDlg::OnInitDialog() as follows:<br />

……<br />

……<br />

……<br />

class CBtnDlg : public CDialog<br />

{<br />

protected:<br />

};<br />

MCSenButton m_btnBmp[4];<br />

BOOL CBtnDlg::OnInitDialog()<br />

{<br />

CDialog::OnInitDialog();<br />

CRect rect;<br />

int x, y;<br />

int i;<br />

……<br />

……<br />

}<br />

for(i=IDC_MOUSE_SEN_1; i

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

Saved successfully!

Ooh no, something went wrong!