步骤1:简易教程(更换对话框的背景)及Gdiplus下载地址可查看这个博客https://blog.csdn.net/misads/article/details/78619735。
步骤2:使用Gdiplus实现自绘按钮可参考这个博客https://blog.csdn.net/wyansai/article/details/50971959。
首先感谢上面两位博主的分享,那么我写这个博客是干什么用的呢?有用的东西不都在上面两个博客里面了吗?
emmmm。。。。对,说得对。但是这个博客的作用就是总结而已?贴贴链接什么的。。。。当然不是,第一个博客的确可以直接查看并抄代码就能用了,但是直接使用第二个博客的代码会出现按钮的闪烁现象,三态/四态按钮的显示情况也会有异常,所以我在步骤2中用参考而不是用查看。
先贴自己根据第二个博客修改后得出的代码吧,方便自己以后查找,也方便说下去。
MyPngButton.h文件的代码如下
#if !defined(AFX_MYPNGBUTTON_H__07529129_0130_4E9C_B4D4_D01323B8E290__INCLUDED_)
#define AFX_MYPNGBUTTON_H__07529129_0130_4E9C_B4D4_D01323B8E290__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "gdiplus/GdiPlus.h"
// MyPngButton.h : header file
/
//按钮的状态
enum
{
CTRL_NOFOCUS = 0x01, //普通
CTRL_FOCUS, //mousemove
CTRL_SELECTED, //buttondown
CTRL_DISABLE, //无效
};
//图片形式
enum
{
BTN_IMG_1 = 1, //只有一张图片
BTN_IMG_3 = 3, //3分图(1个图片内有3小图,下同)
BTN_IMG_4 = 4, //4分图
};
//按钮类型
enum
{
BTN_TYPE_NORMAL = 0x10, //普通BTN
BTN_TYPE_MENU, //菜单类型的BTN
BTN_TYPE_STATIC, //静态类型的BTN
};
#define DEF_TEXT_FRAME_COLOR RGB(255,255,255) //默认颜色
#define DEF_TEXT_COLOR RGB(10,10,10) //默认颜色
#define TOOLTIP_ID 100
class CMyPngButton : public CButton
{
public:
CMyPngButton();
virtual ~CMyPngButton();
public:
// 初始化
void Init(UINT nImg, int nPartNum, UINT nBtnType = BTN_TYPE_NORMAL);
//设置颜色
bool SetTextColor(COLORREF crTextColor, COLORREF crTextFrameColor = DEF_TEXT_FRAME_COLOR, bool bShowFrame = false);
//设置提示
void SetToolTips(LPCTSTR pszTips);
//设置鼠标形状
void SetCursorType(HCURSOR hCursor);
//设置字体大小及类型
void SetFontType(int fontSize, CString fontType);
protected:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseHOver(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void PreSubclassWindow();
virtual BOOL PreTranslateMessage(MSG * pMsg);
bool ShowImage(CDC* pDC, Gdiplus::Image* pImage, UINT nState);
void PaintParent();
Gdiplus::Image *ImageFromResource(HINSTANCE hInstance, UINT uImgID, LPCTSTR lpType);
void UpdateToolTip();
void DrawTextString(CDC * pDC, LPCTSTR pszString, COLORREF crText, COLORREF crFrame, LPRECT lpRect);
void DrawTextString(CDC * pDC, LPCTSTR pszString, COLORREF crText, COLORREF crFrame, int nXPos, int