經過改寫一個buttonst類,實現360效果的button。函數
主要能夠完成:frame,hover,face效果,而且支持menu,tooltips測試
1)派生新的類QButton.添加虛函數,設置自繪stylethis
- void QButton::PreSubclassWindow()
- {
- UINT nBS;
-
- nBS = GetButtonStyle();
-
- m_nTypeStyle = nBS & BS_TYPEMASK;
-
- if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
-
- if (m_nTypeStyle == BS_DEFPUSHBUTTON)
- {
- m_bIsDefault = TRUE;
-
- m_nTypeStyle = BS_PUSHBUTTON;
- }
-
- ASSERT(m_nTypeStyle != BS_OWNERDRAW);
-
- ModifyStyle(BS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
- CButton::PreSubclassWindow();
- }
2)添加一些控制的變量spa
- void QButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
- CRect rc = lpDIS->rcItem;
- CBitmap bitmap;
- bitmap.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
- CDC memdc;
- memdc.CreateCompatibleDC(pDC);
- memdc.SelectObject(bitmap);
- PaintBk(&memdc);
- memdc.SelectObject(GetStockObject(NULL_BRUSH));
- memdc.Rectangle(rc);
- if(m_bIsCheckBox)
- {
- m_bIsPressed = (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
- }else
- {
- m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
- }
- m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
- m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
- if(m_bIsDisabled)
- {
- if(m_pImage_disable != NULL)
- m_pImage_disable->Draw(memdc.m_hDC, rc);
- }else
- {
- if(m_bIsPressed)
- {
- if(m_pImage_pressed != NULL){
- m_pImage_face->Draw(memdc.m_hDC, rc);
- CRect rc2;
- rc2 = rc;
- rc2.OffsetRect(2,2);
- m_pImage_pressed->Draw(memdc.m_hDC, rc);
- }
- }else if(m_bIsHovering)
- {
- if(m_pImage_hover != NULL && m_pImage_face != NULL)
- {
- m_pImage_face->Draw(memdc.m_hDC, rc);
- m_pImage_hover->Draw(memdc.m_hDC, rc);
- }
- }else
- {
- if(m_pImage_face != NULL){
- m_pImage_face->Draw(memdc.m_hDC, rc);
- }
- }
- }
-
- pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memdc, 0, 0, SRCCOPY);
- TRACE("m_bIsPressed = %d\n", m_bIsPressed);
- }
直接用memdc繪製,frame,hover,face.net
3)使用cximage來加載png圖片資源code
- BOOL QButton::LoadDisablePng(UINT nID, LPCTSTR cType)
- {
- if(m_pImage_disable != NULL)
- delete m_pImage_disable;
- m_pImage_disable = new CxImage;
- return m_pImage_disable->LoadResource(FindResource(NULL,MAKEINTRESOURCE(nID),cType), CXIMAGE_FORMAT_PNG);
- }
4)添加mouse hover等支持。blog
5)測試程序圖片
- OnInitDialog()
- {
- m_btn.LoadFacePng(IDR_PNG8,"PNG");
- m_btn.LoadPressPng(IDR_PNG10,"PNG");
-
- m_btn.LoadHoverPng(IDR_PNG9,"PNG");
- m_btn.SetTooltipText("Hunter lv Tool tips!", TRUE);
- m_btn.SetMenu(IDR_MENU1, this->m_hWnd, TRUE);
- }
6)須要本身編譯cximage的庫,而後添加庫文件,頭文件就能夠直接使用。ip