版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 机场卫生间装修改造施工方案
- 医疗机构健康教育方案
- 高速公路改建拆除施工方案
- 桥梁抗震预应力施工方案
- 市场管理合同(2篇)
- 智能城市环境保护管理方案
- 合肥2024年09版小学四年级下册英语第二单元期末试卷
- 中小学心理健康月活动方案
- 公共交通安全项目实施方案
- 房地产开发公司客户纠纷处理流程
- 一年级下册道德与法治《分享真快乐》课件【新部编版】
- 甘肃省定西市2023-2024学年八年级上学期期中语文试题
- 景观可达性和包容性的新进展
- 《滴水实验》(教学设计)-2023-2024学年四年级上册数学北师大版
- 2024年头孢菌素行业现状分析:头孢菌素国内市场规模达到5515.47亿元
- 班主任能力大赛情景答辩环节真题及答案高中组
- 机动车检验检测机构授权签字人考核试题及答案
- icu手册第一部分-常见病诊疗规范
- 2024-2025学年高中语文上学期 文言文句子翻译教学设计
- 静脉留置针产品营销计划书
- 2024年江苏江南水务股份限公司公开招聘工作人员10人(高频重点提升专题训练)共500题附带答案详解
评论
0/150
提交评论