小标
2018-12-14
来源 :
阅读 4245
评论 0
摘要:本文主要向大家介绍了VC编程之MFC的PNG图片按钮,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程之MFC的PNG图片按钮,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

[cpp] view plain copy
1.#pragma once
2.#include "afxwin.h"
3.
4./////////////////////////////////////////////////////////////////////
5.// 工程: YF_GCM -> ButtonDemo
6.// 作者: **
7.// 描述: 自绘制位图按钮
8.// 主要函数:
9.// SetButtonUpBitmapEx()设置鼠标放置在按钮上的图片
10.// SetButtonDownBitmapEx()设置按钮按下的图片
11.// SetButtonNormalBitmapEx()设置鼠标不在按钮是的图片
12.// 日期: 2013.12.16
13.// 版本: 1.0
14.// 修改:
15./////////////////////////////////////////////////////////////////////
16.// CBitmapButtonEx
17.
18.class CPngButton : public CBitmapButton
19.{
20. DECLARE_DYNAMIC(CPngButton)
21.
22.public:
23. CPngButton();
24. virtual ~CPngButton();
25.
26.protected:
27. DECLARE_MESSAGE_MAP()
28.
29.protected:
30.
31. virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
32.
33.public:
34. // 设置按钮抬起图片
35. BOOL SetButtonUpBitmapEx(const CString& szFilePath);
36. // 设置按钮按下图片
37. BOOL SetButtonDownBitmapEx(const CString& szFilePath);
38. // 普通按钮图片
39. BOOL SetButtonNormalBitmapEx(const CString& szFilePath);
40.protected:
41. virtual void PreSubclassWindow();
42.public:
43. virtual BOOL PreTranslateMessage(MSG* pMsg);
44. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
45. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
46. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
47.protected:
48. BOOL m_lButtonDown;
49. // 按钮有效区域
50.// CRgn m_btRgn;
51. // 绘制按钮图片区域
52.// CRect m_rectDrawButton;
53.private:
54. // 图片
55. CImage m_imageButtonUp;
56. CImage m_imageButtonDown;
57. CImage m_imageBitmapNormal;
58. CDC m_bkDc; // 记录背景
59. bool m_first;
60.public:
61.
62.protected:
63. BOOL m_bMouseOver;
64. BOOL m_bTrack;
65.public:
66.};/////////////////////////////////////////////////////////////////////
67.// 工程: YF_GCM -> ButtonDemo
68.// 作者: **
69.// 描述: 自绘制位图按钮
70.// 主要函数:
71.// SetButtonUpBitmapEx()设置鼠标放置在按钮上的图片
72.// SetButtonDownBitmapEx()设置按钮按下的图片
73.// SetButtonNormalBitmapEx()设置鼠标不在按钮是的图片
74.// 日期: 2013.12.16
75.// 版本: 1.0
76.// 修改:
77./////////////////////////////////////////////////////////////////////
78.
79.#include "stdafx.h"
80.#include "pngbutton.h"
81.#include "TransparentPNG.h"
82.// #include "LogToFile.h"
83.
84.// CBitmapButtonEx
85.
86.IMPLEMENT_DYNAMIC(CPngButton, CBitmapButton)
87.
88. CPngButton::CPngButton()
89. : CBitmapButton()
90. , m_lButtonDown(FALSE)
91. , m_bMouseOver(FALSE)
92. , m_bTrack(FALSE)
93. , m_first(true)
94.{
95.}
96.
97.CPngButton::~CPngButton()
98.{
99. if (NULL != m_imageButtonUp)
100. {
101. ::DeleteObject(m_imageButtonUp);
102. }
103. if (NULL != m_imageButtonDown)
104. {
105. ::DeleteObject(m_imageButtonDown);
106. }
107. if (NULL != m_imageBitmapNormal)
108. {
109. ::DeleteObject(m_imageButtonDown);
110. }
111.}
112.
113.
114.BEGIN_MESSAGE_MAP(CPngButton, CBitmapButton)
115. ON_WM_LBUTTONDOWN()
116. ON_WM_LBUTTONUP()
117. ON_WM_MOUSEMOVE()
118.END_MESSAGE_MAP()
119.
120.
121.
122.// CBitmapButtonEx 消息处理程序
123.
124.BOOL CPngButton::SetButtonUpBitmapEx(const CString& szFilePath)
125.{
126. if (!m_imageButtonUp.IsNull())
127. {
128. m_imageButtonUp.Destroy();
129. }
130. m_imageButtonUp.Load(szFilePath);
131. if (!m_imageButtonUp.IsNull())
132. {
133. CTransparentPNG tran;
134. tran(&m_imageButtonUp);
135. }
136. return !m_imageButtonUp.IsNull();
137.}
138.
139.
140.BOOL CPngButton::SetButtonNormalBitmapEx(const CString& szFilePath)
141.{
142. if (!m_imageBitmapNormal.IsNull())
143. {
144. m_imageBitmapNormal.Destroy();
145. }
146. m_imageBitmapNormal.Load(szFilePath);
147. if (!m_imageBitmapNormal.IsNull())
148. {
149. CTransparentPNG tran;
150. tran(&m_imageBitmapNormal);
151. }
152. return !m_imageBitmapNormal.IsNull();
153.}
154.
155.// BOOL CPngButton::SetRedPointImage(const CString& szFilePath)
156.// {
157.// if (!m_imageRedPoint.IsNull())
158.// {
159.// m_imageRedPoint.Destroy();
160.// }
161.// m_imageRedPoint.Load(szFilePath);
162.// if (!m_imageRedPoint.IsNull())
163.// {
164.// CTransparentPNG tran;
165.// tran(&m_imageRedPoint);
166.// }
167.// return !m_imageRedPoint.IsNull();
168.// }
169.BOOL CPngButton::SetButtonDownBitmapEx(const CString& szFilePath)
170.{
171. if (!m_imageButtonDown.IsNull())
172. {
173. m_imageButtonDown.Destroy();
174. }
175. m_imageButtonDown.Load(szFilePath);
176. if (!m_imageButtonDown.IsNull())
177. {
178. CTransparentPNG tran;
179. tran(&m_imageButtonDown);
180. }
181. return !m_imageButtonDown.IsNull();
182.}
183.
184.void CPngButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
185.{
186. // TODO: 添加您的代码以绘制指定项
187. CRect rect = lpDrawItemStruct->rcItem;
188.
189. if (m_first)
190. {// 复制背景
191. CDialog* pParent=(CDialog*)GetParent();
192. CPoint pt(0,0);
193. MapWindowPoints(pParent,&pt,1);
194. CDC * pdc = GetParent ()->GetDC ();
195. m_bkDc.CreateCompatibleDC (pdc);
196. CBitmap memBmp;
197. memBmp.CreateCompatibleBitmap(pdc, rect.right, rect.bottom);
198. m_bkDc.SelectObject(&memBmp);
199. m_bkDc.BitBlt (0, 0, rect.right, rect.bottom, pdc, pt.x, pt.y, SRCCOPY);
200. ReleaseDC (pdc);
201. m_first = false;
202. }
203.// CClientDC dc(this);
204.// dc.Rectangle (rect);
205. // 背景
206. CDC* thisdc = NULL;
207. thisdc = GetDC();
208. thisdc->BitBlt (0, 0, rect.right, rect.bottom, &m_bkDc, 0/*rect.right*/, 0/*rect.bottom*/, SRCCOPY);
209.
210. if (m_lButtonDown)
211. {
212. if (m_imageButtonDown.IsNull())
213. {
214.// CString szLog;
215.// szLog.Format(_T("m_imageButtonDown 未加载背景图片!"));
216.// AppLog(szLog);
217. }
218. else
219. {
220. m_imageButtonDown.Draw(thisdc->m_hDC, rect/*m_rectDrawButton*/);
221. }
222. }
223. else
224. {
225. if (m_bMouseOver)
226. {
227. if (m_imageButtonUp.IsNull())
228. {
229.// CString szLog;
230.// szLog.Format(_T("m_imageButtonUp 未加载背景图片!"));
231.// AppLog(szLog);
232. }
233. else
234. {
235. m_imageButtonUp.Draw(thisdc->m_hDC, rect/*m_rectDrawButton*/);
236. }
237. }
238. else
239. {
240. if (m_imageBitmapNormal.IsNull())
241. {
242.// CString szLog;
243.// szLog.Format(_T("m_imageBitmapNormal 未加载背景图片!"));
244.// AppLog(szLog);
245. }
246. else
247. {
248. m_imageBitmapNormal.Draw(thisdc->m_hDC, rect/*m_rectDrawButton*/);
249. }
250. }
251. }
252. if (0 == this->ReleaseDC(thisdc))
253. {
254.// CString szLog;
255.// szLog.Format(_T("ReleaseDC error!"));
256.// AppLog(szLog);
257. }
258.}
259.
260.void CPngButton::PreSubclassWindow()
261.{
262. // TODO: 在此添加专用代码和/或调用基类
263.
264. ModifyStyle(0, WS_CLIPCHILDREN | BS_OWNERDRAW ); //设置按钮的有效区域
265.
266.// CRect rect;
267.// GetClientRect(&rect);
268.// CDialog* pParent=(CDialog*)GetParent();
269.// CPoint pt(rect.left, rect.top);
270.// MapWindowPoints(pParent, &pt, 1);
271.// CDC * pdc = GetParent ()->GetDC ();
272.// // 复制背景
273.// m_bkDc.CreateCompatibleDC (pdc);
274.// CBitmap memBmp;
275.// memBmp.CreateCompatibleBitmap(pdc, rect.right, rect.bottom);
276.// m_bkDc.SelectObject(&memBmp);
277.// m_bkDc.BitBlt (0, 0, rect.right, rect.bottom, pdc, 0, 0, SRCCOPY);
278.// ReleaseDC (pdc);
279.
280.// m_btRgn.CreateRectRgnIndirect (rc);
281.// SetWindowRgn(m_btRgn, TRUE);
282.
283. CBitmapButton::PreSubclassWindow();
284.}
285.
286.void CPngButton::OnLButtonDown(UINT nFlags, CPoint point)
287.{
288. // TODO: 在此添加消息处理程序代码和/或调用默认值
289.
290. m_lButtonDown = TRUE;
291. CBitmapButton::OnLButtonDown(nFlags, point);
292.}
293.
294.void CPngButton::OnLButtonUp(UINT nFlags, CPoint point)
295.{
296. // TODO: 在此添加消息处理程序代码和/或调用默认值
297. m_lButtonDown = FALSE;
298. CBitmapButton::OnLButtonUp(nFlags, point);
299.}
300.
301.BOOL CPngButton::PreTranslateMessage(MSG* pMsg)
302.{
303. // TODO: 在此添加专用代码和/或调用基类
304.
305. if(pMsg->message==WM_MOUSELEAVE)
306. {
307. m_bTrack=FALSE;
308. m_bMouseOver = FALSE;
309. InvalidateRect(NULL);
310. }
311. else if (pMsg->message==WM_MOUSEHOVER)
312. {
313. m_bMouseOver = TRUE;
314. InvalidateRect(NULL);
315. }
316.
317. return CBitmapButton::PreTranslateMessage(pMsg);
318.}
319.
320.void CPngButton::OnMouseMove(UINT nFlags, CPoint point)
321.{
322. // TODO: 在此添加消息处理程序代码和/或调用默认值
323.
324. if(!m_bTrack)
325. {
326. TRACKMOUSEEVENT tme;
327. tme.cbSize=sizeof(TRACKMOUSEEVENT);
328. tme.dwFlags=TME_HOVER | TME_LEAVE;
329. tme.dwHoverTime=HOVER_DEFAULT;
330. tme.hwndTrack=m_hWnd;
331.
332. m_bTrack=_TrackMouseEvent(&tme);
333. }
334.
335. CBitmapButton::OnMouseMove(nFlags, point);
336.}
以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号