改变CButton背景颜色(自绘CButton)-VC(转)_第1页
改变CButton背景颜色(自绘CButton)-VC(转)_第2页
改变CButton背景颜色(自绘CButton)-VC(转)_第3页
改变CButton背景颜色(自绘CButton)-VC(转)_第4页
改变CButton背景颜色(自绘CButton)-VC(转)_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、改变CButton背景颜(绘CButton)-VC(转)这是个实现绘CButton的类,期望对家有点帮助,我提供这个类,只是为家提供个绘控件的思路,你可以在此基础上,随意地修改代码,直到它能完成你所需要的功能.其它控件的绘,原理了和这个差不多.#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:/设置背景颜void SetBkColor(COLORREF BkColor);/设置标在按钮上的偏移颜void SetShOffset(int nShOffset);/设置字体颜void SetTextColor(COLORREF TextColor);/设置页链接vo

3、id SetURL(CString strURL);/设置背景图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:/标是否在按钮

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);/标离开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/设置背景颜void CButtonST:SetBkColor(COLORREF BkColor)m_BkColor =

10、BkColor;this-Invalidate();/设置标在按钮上的偏移颜void CButtonST:SetShOffset(int nShOffset)m_nShOffset = nShOffset;this-Invalidate();/设置字体颜void CButtonST:SetTextColor(COLORREF TextColor)m_TextColor = TextColor;this-Invalidate();/设置页链接void CButtonST:SetURL(CString strURL)m_strURL = strURL;/设置背景图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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论