改變CButton背景顏色(自繪CButton)-VC(轉(zhuǎn))_第1頁
改變CButton背景顏色(自繪CButton)-VC(轉(zhuǎn))_第2頁
改變CButton背景顏色(自繪CButton)-VC(轉(zhuǎn))_第3頁
改變CButton背景顏色(自繪CButton)-VC(轉(zhuǎn))_第4頁
改變CButton背景顏色(自繪CButton)-VC(轉(zhuǎn))_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、改變CButton背景顏(繪CButton)-VC(轉(zhuǎn))這是個(gè)實(shí)現(xiàn)繪CButton的類,期望對(duì)家有點(diǎn)幫助,我提供這個(gè)類,只是為家提供個(gè)繪控件的思路,你可以在此基礎(chǔ)上,隨意地修改代碼,直到它能完成你所需要的功能.其它控件的繪,原理了和這個(gè)差不多.#if !defined(AFX_BUTTONST_H_1271FF9C_E28C_4D3B_B429_AFE65924A5D0_INCLUDED_)#define AFX_BUTTONST_H_1271FF9C_E28C_4D3B_B429_AFE65924A5D0_INCLUDED_#if _MSC_VER 1000#pragma once#endi

2、f / _MSC_VER 1000/ ButtonST.h : header file/ CButtonST windowclass CButtonST : public CButton/ Constructionpublic:CButtonST();/ Attributespublic:/ Operationspublic:/設(shè)置背景顏void SetBkColor(COLORREF BkColor);/設(shè)置標(biāo)在按鈕上的偏移顏void SetShOffset(int nShOffset);/設(shè)置字體顏void SetTextColor(COLORREF TextColor);/設(shè)置頁鏈接vo

3、id SetURL(CString strURL);/設(shè)置背景圖void SetBkPicture(CBitmap *pBitMap);/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CButtonST)public:virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, co

4、nst RECT& rect, CWnd*pParentWnd, UINT nID, CCreateContext* pContext = NULL);protected:virtual BOOL PreCreateWindow(CREATESTRUCT& cs);virtual void PreSubclassWindow();/AFX_VIRTUAL/ Implementationpublic:virtual CButtonST();void DrawItem1(LPDRAWITEMSTRUCT lpDrawItemStruct);/ Generated message map funct

5、ionsprotected:/AFX_MSG(CButtonST)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg BOOL OnNcActivate(BOOL bActive);afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);afx_msg void OnKillFocus(CWnd* pNewWnd);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);/AFX_MSGprotected:/標(biāo)是否在按鈕

6、上BOOL m_IsPressed;COLORREF m_BkColor;int m_nShOffset;COLORREF m_TextColor;CString m_strURL;CBitmap *m_pBitMapBK;protected:/背景顏偏移COLORREF OffsetColor(COLORREF color,int nOffset);/標(biāo)離開LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);DECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will

7、 insert additional declarations immediately before the previous line.#endif / !defined(AFX_BUTTONST_H_1271FF9C_E28C_4D3B_B429_AFE65924A5D0_INCLUDED_)/ ButtonST.cpp : implementation file/#include stdafx.h#include ContrlST.h#include ButtonST.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic ch

8、ar THIS_FILE = _FILE_;#endif#define BS_TYPEMASK SS_TYPEMASK/ CButtonSTCButtonST:CButtonST()m_IsPressed = FALSE;m_BkColor = RGB(216,233,216);m_nShOffset = 30;m_TextColor = RGB(0,0,0);m_strURL = ;m_pBitMapBK = NULL;CButtonST:CButtonST()if(m_pBitMapBK!=NULL)delete m_pBitMapBK;m_pBitMapBK = NULL;BEGIN_M

9、ESSAGE_MAP(CButtonST, CButton)/AFX_MSG_MAP(CButtonST)ON_WM_MOUSEMOVE()ON_WM_NCACTIVATE()ON_WM_NCMOUSEMOVE()ON_WM_KILLFOCUS()ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)ON_WM_LBUTTONDOWN()/AFX_MSG_MAPEND_MESSAGE_MAP()/ CButtonST message handlers/設(shè)置背景顏void CButtonST:SetBkColor(COLORREF BkColor)m_BkColor =

10、BkColor;this-Invalidate();/設(shè)置標(biāo)在按鈕上的偏移顏void CButtonST:SetShOffset(int nShOffset)m_nShOffset = nShOffset;this-Invalidate();/設(shè)置字體顏void CButtonST:SetTextColor(COLORREF TextColor)m_TextColor = TextColor;this-Invalidate();/設(shè)置頁鏈接void CButtonST:SetURL(CString strURL)m_strURL = strURL;/設(shè)置背景圖void CButtonST:Se

11、tBkPicture(CBitmap *pBitMap)if(m_pBitMapBK=pBitMap)return;if(m_pBitMapBK!=NULL)delete m_pBitMapBK;m_pBitMapBK = pBitMap;m_pBitMapBK = pBitMap;this-Invalidate();/背景顏偏移COLORREF CButtonST:OffsetColor(COLORREF color,int nOffset)BYTE byRed = 0;BYTE byGreen = 0;BYTE byBlue = 0;short shOffsetR = nOffset;sh

12、ort shOffsetG = nOffset;short shOffsetB = nOffset;if (nOffset 255)nOffset = 30;/ Get RGB components of specified colorbyRed = GetRValue(color);byGreen = GetGValue(color);byBlue = GetBValue(color);/ Calculate max. allowed real offsetif (nOffset 0)if (byRed + nOffset 255) shOffsetR = 255 - byRed;if (b

13、yGreen + nOffset 255) shOffsetG = 255 - byGreen;if (byBlue + nOffset 255) shOffsetB = 255 - byBlue;nOffset = min(min(shOffsetR, shOffsetG), shOffsetB);elseif (byRed + nOffset 0) shOffsetR = -byRed;if (byGreen + nOffset 0) shOffsetG = -byGreen;if (byBlue + nOffset hDC);CRect rect = lpDrawItemStruct-r

14、cItem;CRect offRect = rect;int ndist = 2;offRect.left+= ndist;offRect.right -=ndist;offRect.top += ndist;offRect.bottom -=ndist;pDC-SetBkMode(TRANSPARENT);pDC-SetTextColor(m_TextColor);/繪制背景if(m_pBitMapBK!=NULL)CDC* pNewDC = new CDC;pNewDC-CreateCompatibleDC(pDC);pNewDC-SelectObject(m_pBitMapBK);BIT

15、MAP bmp;m_pBitMapBK-GetBitmap(&bmp);if(m_IsPressed)pDC-StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);elseCBrush brush(RGB(220,220,220);/RGB(m_nShOffset,m_nShOffset,m_nShOffset);pDC-FillRect(&rect,&brush);pDC-StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,

16、0,bmp.bmWidth,bmp.bmHeight,SRCAND);delete pNewDC;pNewDC = NULL;elseif(m_IsPressed)CBrush brush(OffsetColor(m_BkColor,m_nShOffset);pDC-FillRect(rect,&brush);CBrush brush1(RGB(128,128,128);pDC-FrameRect(rect,&brush1);pDC-FrameRect(offRect,&brush1);elseCBrush brush(m_BkColor);pDC-FillRect(rect,&brush);

17、CBrush brush1(RGB(128,128,128);pDC-FrameRect(offRect,&brush1);pDC-FrameRect(offRect,&brush1);CString sTitle;GetWindowText(sTitle);offRect.OffsetRect(0,rect.Height()/2-8);/ pDC-DrawText(sTitle, sTitle.GetLength(),offRect, DT_WORDBREAK | DT_CENTER);pDC-DrawText(sTitle, sTitle.GetLength(),offRect, DT_W

18、ORDBREAK|DT_CENTER);void CButtonST:DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)/ TODO: Add your code to draw the specified itemDrawItem1(lpDrawItemStruct);return;CDC* pDC = CDC:FromHandle(lpDrawItemStruct-hDC);CRect rect = lpDrawItemStruct-rcItem;CRect offRect = rect;int ndist = 2;offRect.left+= ndis

19、t;offRect.right -=ndist;offRect.top += ndist;offRect.bottom -=ndist;CDC* pResetDC=new CDC();pResetDC-CreateCompatibleDC(pDC);CBitmap bitmap;bitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height();pResetDC-SelectObject(&bitmap);pResetDC-SetBkMode(TRANSPARENT);pResetDC-SetTextColor(m_TextColor);/

20、繪制背景if(m_pBitMapBK!=NULL)CDC* pNewDC = new CDC;pNewDC-CreateCompatibleDC(pResetDC);pNewDC-SelectObject(m_pBitMapBK);BITMAP bmp;m_pBitMapBK-GetBitmap(&bmp);if(m_IsPressed)pResetDC-StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);elseCBrush brush(RGB(220,220,220);

21、/RGB(m_nShOffset,m_nShOffset,m_nShOffset);pResetDC-FillRect(&rect,&brush);pResetDC-StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCAND);delete pNewDC;pNewDC = NULL;elseif(m_IsPressed)CBrush brush(OffsetColor(m_BkColor,m_nShOffset);pResetDC-FillRect(rect,&brush);CBrus

22、h brush1(RGB(128,128,128);pResetDC-FrameRect(rect,&brush1);pResetDC-FrameRect(offRect,&brush1);elseCBrush brush(m_BkColor);pResetDC-FillRect(rect,&brush);CBrush brush1(RGB(128,128,128);pResetDC-FrameRect(offRect,&brush1);CString sTitle;GetWindowText(sTitle);offRect.OffsetRect(0,rect.Height()/2-8);/

23、pDC-DrawText(sTitle, sTitle.GetLength(),offRect, DT_WORDBREAK | DT_CENTER);pResetDC-DrawText(sTitle, sTitle.GetLength(),offRect, DT_CENTER);pDC-StretchBlt(0,0,rect.Width(),rect.Height(),pResetDC,0,0,rect.Width(),rect.Height(),SRCCOPY);delete pResetDC;void CButtonST:OnMouseMove(UINT nFlags, CPoint po

24、int)/ TODO: Add your message handler code here and/or call defaultTRACKMOUSEEVENT csTME;csTME.cbSize = sizeof(csTME);csTME.dwFlags = TME_LEAVE;csTME.hwndTrack = m_hWnd;:_TrackMouseEvent(&csTME);if(m_IsPressed=FALSE)m_IsPressed = TRUE;this-Invalidate();CButton:OnMouseMove(nFlags, point);BOOL CButtonS

25、T:OnNcActivate(BOOL bActive)/ TODO: Add your message handler code here and/or call defaultreturn CButton:OnNcActivate(bActive);void CButtonST:OnNcMouseMove(UINT nHitTest, CPoint point)/ TODO: Add your message handler code here and/or call defaultCButton:OnNcMouseMove(nHitTest, point);void CButtonST:

26、OnKillFocus(CWnd* pNewWnd)CButton:OnKillFocus(pNewWnd);/ TODO: Add your message handler code hereLRESULT CButtonST:OnMouseLeave(WPARAM wParam, LPARAM lParam)m_IsPressed = FALSE;this-Invalidate();return 0; / End of OnMouseLeaveBOOL CButtonST:Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect,CWnd* pParentWnd, UINT nID, CCreateContext* pContext)/ TODO: Add your specialized code here and/or c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論