分类:
2008-10-13 16:48:41
如何实现类似VC属性表中的钉子按钮
作者 浙江大学
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); }其它实现细节请参见源代码。