首页 > 代码库 > VS对话框按钮贴图和Static控件自定义
VS对话框按钮贴图和Static控件自定义
软件开发中,为了让用户更喜欢使用我们的软件,除了具有良好的性能外,还需要美观的界面。
本文简单地介绍下对话框背景贴图和按钮贴图,以及Static控件自定义成各种Static效果。
首先贴个图,让大家看看效果
1.新建对话框应用程序
2.添加图片资源
3.背景贴图
重写OnPaint函数
void CDlgBmpTestDemoDlg::OnPaint() { CPaintDC dc(this); // 用于绘制的设备上下文 //绘制背景 CBitmap bitmap; bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_BITMAP_PIC)); BITMAP bmpInfo; bitmap.GetBitmap(&bmpInfo); CDC bitmapDC; bitmapDC.CreateCompatibleDC(&dc); CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap); dc.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &bitmapDC,0, 0, SRCCOPY); bitmapDC.SelectObject(pOldBitmap); bitmap.DeleteObject(); }
设置位置和大小
<span style="white-space:pre"> </span>//对话框背景图片宽和高 const int nBkBmpWidth = 480; const int nBkBmpHeight = 300; int xPos = 0; int yPos = 0; //获得电脑显示器的像素宽度和像素高度 int ax = GetDC()->GetDeviceCaps(HORZRES) - nBkBmpWidth; int ay = GetDC()->GetDeviceCaps(VERTRES) - nBkBmpHeight; int nWidth = 0; int nHeight = 0; if(ax <= 0) { ax = 0; } else { ax = ax/2; } if(ay <= 0) { ay = 0; } else { ay = ay/2; } RECT clientRect; RECT rt; clientRect.left = ax; clientRect.top = ay; clientRect.right = clientRect.left + nBkBmpWidth; clientRect.bottom = clientRect.top + nBkBmpHeight; MoveWindow(&clientRect);效果
4.按钮贴图
OwnerDraw的属性设置为True
定义变量
CBitmapButton m_btnOne;加载图片资源
m_btnOne.LoadBitmaps(IDB_BITMAP_BTN);
设置按钮位置和大小
const int nBottomBtnWidth = 95; const int nBottomBtnHeight = 30; const int nPadding = 8;//按钮的水平间距 xPos = 0; yPos = 0; yPos = clientRect.top + 250; //xPos = clientRect.left + nPadding; xPos = clientRect.left + nPadding*4 + nBottomBtnWidth*3; m_btnOne.MoveWindow(xPos,yPos,nBottomBtnWidth,nBottomBtnHeight);效果
5.Static标题自定义
DlgTitleStatic.h
#pragma once // CDlgTitleStatic class CDlgTitleStatic : public CStatic { DECLARE_DYNAMIC(CDlgTitleStatic) public: CDlgTitleStatic(); virtual ~CDlgTitleStatic(); protected: DECLARE_MESSAGE_MAP() private: CString m_sTitleText; LOGFONT m_LogFontTitle; UINT m_nFormat; DWORD m_dwColor; public: afx_msg void OnPaint(); void SetTitleText(CString sTitleText); };
DlgTitleStatic.cpp
// DlgTitleStatic.cpp : 实现文件 // #include "stdafx.h" #include "DlgBmpTestDemo.h" #include "DlgTitleStatic.h" // CDlgTitleStatic IMPLEMENT_DYNAMIC(CDlgTitleStatic, CStatic) CDlgTitleStatic::CDlgTitleStatic() { m_LogFontTitle.lfHeight = 32; m_LogFontTitle.lfWeight = 600; m_LogFontTitle.lfOrientation= 0; m_LogFontTitle.lfEscapement = 0; m_LogFontTitle.lfWidth = 14; m_LogFontTitle.lfItalic = false; m_LogFontTitle.lfUnderline = false; m_LogFontTitle.lfStrikeOut = false; m_LogFontTitle.lfCharSet = GB2312_CHARSET; m_LogFontTitle.lfOutPrecision = OUT_DEFAULT_PRECIS; m_LogFontTitle.lfClipPrecision = CLIP_DEFAULT_PRECIS; m_LogFontTitle.lfQuality = DRAFT_QUALITY; m_LogFontTitle.lfPitchAndFamily = DEFAULT_PITCH; wcscpy_s(m_LogFontTitle.lfFaceName,_T("黑体")); m_nFormat = DT_SINGLELINE|DT_CENTER |DT_VCENTER|DT_END_ELLIPSIS; //m_nFormat = DT_LEFT |DT_TOP |DT_END_ELLIPSIS; m_dwColor = RGB(255,0,0); m_sTitleText = _T("对话框测试"); } CDlgTitleStatic::~CDlgTitleStatic() { } BEGIN_MESSAGE_MAP(CDlgTitleStatic, CStatic) ON_WM_PAINT() END_MESSAGE_MAP() // CDlgTitleStatic 消息处理程序 void CDlgTitleStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CStatic::OnPaint() RECT rect; GetClientRect(&rect); CBrush brush0(RGB(250,250,250)); dc.FillRect(&rect,&brush0); HFONT hFont = ::CreateFontIndirect(&m_LogFontTitle); HFONT hOldFont = (HFONT)::SelectObject(dc.m_hDC,hFont); dc.SetTextColor(m_dwColor);//画文字 int oldMode = dc.SetBkMode(TRANSPARENT); dc.DrawText(m_sTitleText,&rect,m_nFormat); hFont = (HFONT)::SelectObject(dc.m_hDC,hOldFont); ::DeleteObject(hFont); dc.SetBkMode(oldMode); } void CDlgTitleStatic::SetTitleText(CString sTitleText) { m_sTitleText = sTitleText; }
变量
CDlgTitleStatic m_stcDlgTitle;
//改变对话框标题静态文本框位置 rt.left = clientRect.left + 10; rt.top = clientRect.top + 30; m_stcDlgTitle.MoveWindow(rt.left,rt.top,460,35); m_stcDlgTitle.SetTitleText(TEXT("对话框测试"));效果
6.Static区域自定义
DlgAreaStatic.h#pragma once // CDlgAreaStatic class CDlgAreaStatic : public CStatic { DECLARE_DYNAMIC(CDlgAreaStatic) public: CDlgAreaStatic(); virtual ~CDlgAreaStatic(); protected: DECLARE_MESSAGE_MAP() private: UINT m_nFormat; CString m_sAreaTitleText; LOGFONT m_LogFontAreaTitle; DWORD m_dwColor; public: afx_msg void OnPaint(); void SetAreaTitle(CString sAreaTitle); };
DlgAreaStatic.cpp
// DlgAreaStatic.cpp : 实现文件 // #include "stdafx.h" #include "DlgBmpTestDemo.h" #include "DlgAreaStatic.h" // CDlgAreaStatic IMPLEMENT_DYNAMIC(CDlgAreaStatic, CStatic) CDlgAreaStatic::CDlgAreaStatic() { m_sAreaTitleText = _T("Title"); m_LogFontAreaTitle.lfHeight = 20; m_LogFontAreaTitle.lfWeight = 400; m_LogFontAreaTitle.lfOrientation= 0; m_LogFontAreaTitle.lfEscapement = 0; m_LogFontAreaTitle.lfWidth = 10; m_LogFontAreaTitle.lfItalic = false; m_LogFontAreaTitle.lfUnderline = false; m_LogFontAreaTitle.lfStrikeOut = false; m_LogFontAreaTitle.lfCharSet = GB2312_CHARSET; m_LogFontAreaTitle.lfOutPrecision = OUT_DEFAULT_PRECIS; m_LogFontAreaTitle.lfClipPrecision = CLIP_DEFAULT_PRECIS; m_LogFontAreaTitle.lfQuality = DRAFT_QUALITY; m_LogFontAreaTitle.lfPitchAndFamily = DEFAULT_PITCH; wcscpy_s(m_LogFontAreaTitle.lfFaceName,_T("黑体")); m_dwColor = RGB(0,0,0); m_nFormat = DT_SINGLELINE|DT_LEFT|DT_TOP|DT_END_ELLIPSIS; } CDlgAreaStatic::~CDlgAreaStatic() { } BEGIN_MESSAGE_MAP(CDlgAreaStatic, CStatic) ON_WM_PAINT() END_MESSAGE_MAP() // CDlgAreaStatic 消息处理程序 void CDlgAreaStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CStatic::OnPaint() RECT rect; GetClientRect(&rect); CBrush brush0(RGB(192,192,192)); dc.FillRect(&rect,&brush0); RECT rt = rect; HFONT hFont = ::CreateFontIndirect(&m_LogFontAreaTitle); HFONT hOldFont = (HFONT)::SelectObject(dc.m_hDC,hFont); dc.SetTextColor(RGB(0,0,0));//画文字 dc.SetBkMode(TRANSPARENT); dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT); rt.left = rect.left + 21; rt.top = rect.top + 8; rt.bottom = rt.top + 40; dc.DrawText(m_sAreaTitleText,&rt,m_nFormat); hFont = (HFONT)::SelectObject(dc.m_hDC,hOldFont); ::DeleteObject(hFont); } void CDlgAreaStatic::SetAreaTitle(CString sAreaTitle) { m_sAreaTitleText = sAreaTitle; }
CDlgAreaStatic m_stcArea;
设置区域位置和大小
xPos = clientRect.left + 10; yPos = clientRect.top + 70; m_stcArea.MoveWindow(xPos,yPos,460,160); m_stcArea.SetAreaTitle(_T("区域划分"));
效果
7.Static文本自定义
DlgTextStatic.h#pragma once // CDlgTextStatic class CDlgTextStatic : public CStatic { DECLARE_DYNAMIC(CDlgTextStatic) public: CDlgTextStatic(); virtual ~CDlgTextStatic(); protected: DECLARE_MESSAGE_MAP() private: CString m_sTitleText; LOGFONT m_LogFontTitle; UINT m_nFormat; DWORD m_dwColor; public: afx_msg void OnPaint(); void SetTitleText(CString sTitleText); };
DlgTextStatic.cpp
// DlgTextStatic.cpp : 实现文件 // #include "stdafx.h" #include "DlgBmpTestDemo.h" #include "DlgTextStatic.h" // CDlgTextStatic IMPLEMENT_DYNAMIC(CDlgTextStatic, CStatic) CDlgTextStatic::CDlgTextStatic() { m_LogFontTitle.lfHeight = 20; m_LogFontTitle.lfWeight = 400; m_LogFontTitle.lfOrientation= 0; m_LogFontTitle.lfEscapement = 0; m_LogFontTitle.lfWidth = 10; m_LogFontTitle.lfItalic = false; m_LogFontTitle.lfUnderline = false; m_LogFontTitle.lfStrikeOut = false; m_LogFontTitle.lfCharSet = GB2312_CHARSET; m_LogFontTitle.lfOutPrecision = OUT_DEFAULT_PRECIS; m_LogFontTitle.lfClipPrecision = CLIP_DEFAULT_PRECIS; m_LogFontTitle.lfQuality = DRAFT_QUALITY; m_LogFontTitle.lfPitchAndFamily = DEFAULT_PITCH; wcscpy_s(m_LogFontTitle.lfFaceName,_T("黑体")); // m_nFormat = DT_SINGLELINE|DT_CENTER |DT_VCENTER|DT_END_ELLIPSIS; m_nFormat = DT_LEFT |DT_TOP |DT_END_ELLIPSIS; m_dwColor = RGB(0,0,255); m_sTitleText = _T("Title"); } CDlgTextStatic::~CDlgTextStatic() { } BEGIN_MESSAGE_MAP(CDlgTextStatic, CStatic) ON_WM_PAINT() END_MESSAGE_MAP() // CDlgTextStatic 消息处理程序 void CDlgTextStatic::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CStatic::OnPaint() RECT rect; GetClientRect(&rect); CBrush brush0(RGB(192,192,192)); dc.FillRect(&rect,&brush0); HFONT hFont = ::CreateFontIndirect(&m_LogFontTitle); HFONT hOldFont = (HFONT)::SelectObject(dc.m_hDC,hFont); dc.SetTextColor(m_dwColor);//画文字 int oldMode = dc.SetBkMode(TRANSPARENT); dc.DrawText(m_sTitleText,&rect,m_nFormat); hFont = (HFONT)::SelectObject(dc.m_hDC,hOldFont); ::DeleteObject(hFont); dc.SetBkMode(oldMode); } void CDlgTextStatic::SetTitleText(CString sTitleText) { m_sTitleText = sTitleText; }
CDlgTextStatic m_stcTip;
初始化
m_stcTip.SetTitleText(_T("这只是一个说明"));
设置位置和大小
<a target=_blank href=http://www.mamicode.com/"http://download.csdn.net/detail/bingdianlanxin/8392241">源码下载xPos = clientRect.left + 80;>
效果
8.Static数值自定义
DlgSignalStatic.h#pragma once // CDlgSignalStatic #define SSTC_STATUS_YES 0 #define SSTC_STATUS_NO 1 #define SSTC_STATUS_DEFAULT 2 class CDlgSignalStatic : public CStatic { DECLARE_DYNAMIC(CDlgSignalStatic) public: CDlgSignalStatic(); virtual ~CDlgSignalStatic(); protected: DECLARE_MESSAGE_MAP() private: public: CString m_sSignalName; CString m_sSignalValue; CString m_sSignalUnit; int m_nSignalStatus; int m_nNameWidth; int m_nValueWidth; int m_nUnitWidth; int m_nStatusWidth; int m_nHeight; DWORD m_dwSignalBgColor; DWORD m_dwSignalTxtColor; DWORD m_dwStatusColor; DWORD m_dwNameColor; LOGFONT m_LogFont; public: afx_msg void OnPaint(); void UpdateSignalValue(CString sValue); void UpdateSignalStatus(int flag); };DlgSignalStatic.cpp
// DlgSignalStatic.cpp : 实现文件 // #include "stdafx.h" #include "DlgBmpTestDemo.h" #include "DlgSignalStatic.h" // CDlgSignalStatic IMPLEMENT_DYNAMIC(CDlgSignalStatic, CStatic) CDlgSignalStatic::CDlgSignalStatic() { m_sSignalName = _T("--"); m_sSignalValue = http://www.mamicode.com/_T("--");>
变量CDlgSignalStatic m_stcValue;
初始化m_stcValue.m_sSignalName = _T("数值"); m_stcValue.m_sSignalValue = http://www.mamicode.com/_T("50");>设置位置和大小
xPos = clientRect.left + 80; yPos = clientRect.top + 150; m_stcValue.MoveWindow(xPos,yPos,300,40);
效果
9.对话框类
DlgBmpTestDemoDlg.h
// DlgBmpTestDemoDlg.h : 头文件 // #pragma once #include "afxwin.h" #include "DlgTitleStatic.h" #include "DlgAreaStatic.h" #include "DlgTextStatic.h" #include "DlgSignalStatic.h" // CDlgBmpTestDemoDlg 对话框 class CDlgBmpTestDemoDlg : public CDialog { // 构造 public: CDlgBmpTestDemoDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_DLGBMPTESTDEMO_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: CDlgTitleStatic m_stcDlgTitle; CDlgAreaStatic m_stcArea; CDlgTextStatic m_stcTip; CDlgSignalStatic m_stcValue; CBitmapButton m_btnOne; void initUi(void); afx_msg void OnBnClickedBtnOne(); };
DlgBmpTestDemoDlg.cpp
// DlgBmpTestDemoDlg.cpp : 实现文件 // #include "stdafx.h" #include "DlgBmpTestDemo.h" #include "DlgBmpTestDemoDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CDlgBmpTestDemoDlg 对话框 CDlgBmpTestDemoDlg::CDlgBmpTestDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDlgBmpTestDemoDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_stcTip.SetTitleText(_T("这只是一个说明")); m_stcValue.m_sSignalName = _T("数值"); m_stcValue.m_sSignalValue = http://www.mamicode.com/_T("50");>
10.文档结构
源码下载
VS对话框按钮贴图和Static控件自定义
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。