1、新建一工程,基於對話框,采用默認設置。
2、添加一個新類,CSButton 基類CButton。
3、加入兩副位圖,分別表示釘子釘下和沒有釘下時的狀態。
4、向類中加入變量CBitmap bitmap,bitmap2,BOOL m_state 表示按鈕的狀態。CDC memdc;
5、重載函數PreSubclassWindow 已裝入位圖。
void CSButton::PreSubclassWindow()
{
m_state=FALSE;
bitmap.LoadBitmap(IDB_BITMAP1);
bitmap2.LoadBitmap(IDB_BITMAP2);
CDC *pDC=GetDC();
memdc.CreateCompatibleDC(pDC);
memdc.SelectObject(&bitmap);
memdc2.CreateCompatibleDC(pDC);
memdc2.SelectObject(&bitmap2);
CButton::PreSubclassWindow();
}
6、重載DrawItem畫出按鈕
void CSButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect clIEnt=lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
DWord state=lpDrawItemStruct->itemState;
CBrush brush;
brush.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
pDC->FillRect(clIEnt,&brush);
if(m_state)
{
pDC->StretchBlt(client.left,client.top,client.Width(),clIEnt.Height(),&memdc,0,0,24,21,SRCCOPY);
GetParent()->SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
}
else
{
pDC->StretchBlt(client.left,client.top,client.Width(),clIEnt.Height(),&memdc2,0,0,24,21,SRCCOPY);
GetParent()->SetWindowPos(&CWnd::wndNoTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
}
}
7、重載OnLButtonDown void CSButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_state=!m_state;
CButton::OnLButtonDown(nFlags, point);
}