我们知道在VC中可以用AfxMessageBox或者更灵活的MessageBox来实现消息框,但是有时候我们的要求不止于此,特别是在Vista和Windows 7下消息框的样式不好看,下面还有一个白栏。自己写了一个派生自CDialog的MessageBoxST类,模拟了消息框的实现,虽然不是特别完美,但基本功能已经实现,界面如下:
以上是用SkinSharp换肤 后的效果比较,下面介绍这个类的使用方法:
步骤一:新建一个基于对话框的工程,名字任意;
步骤二:插入一个对话框资源,更改其ID号为IDD_MESSAGE_DIALOG ;
步骤三:把MessageBoxST类添加进工程,在需要调用的地方添加头文件。例如:在对话框添加一个按钮,令其功能为弹出消息。添加头文件如下:
#include "MessageBoxST.h"
然后在功能函数里面,添加以下代码,包括MessageBoxST类,和原本的消息类做比较:
- void CMyMessageBoxDlg::OnBnClickedButton1()
- {
-
- CMessageBoxST aBox;
- aBox.SetMessageBoxDialog(_T("这是一个消息框,用CDialog实现的。"),_T("标题栏1"),MB_ICONINFORMATION,MB_YESNOCANCEL);
- if (aBox.DoModal()==IDOK)
- {
- if (aBox.GetMessageBoxValue()==IDOK)
- AfxMessageBox(_T("确定按钮"));
- else if (aBox.GetMessageBoxValue()==IDNO)
- AfxMessageBox(_T("否按钮"));
- else if (aBox.GetMessageBoxValue()==IDYES)
- AfxMessageBox(_T("是按钮"));
- else if (aBox.GetMessageBoxValue()==IDCANCEL)
- AfxMessageBox(_T("取消按钮"));
- }
- }
- void CMyMessageBoxDlg::OnBnClickedButton2()
- {
-
- MessageBox(_T("这是一个消息框,用MessageBox实现的。"),_T("标题栏2"),MB_ICONINFORMATION|MB_YESNOCANCEL);
- }
自定义MessageBoxST类
说明:①这个类只有两个函数,大家可以自行添加功能;
②这个类对中文支持比较好,有错误的话,请大家帮忙指出,指点一下。
MessageBoxST.h文件
-
-
-
-
-
-
- #pragma once
- #define ID_BUTTON_YES 6000
- #define ID_BUTTON_OK 6001
- #define ID_BUTTON_NO 6002
- #define ID_BUTTON_CANCEL 6003
-
-
-
- class CMessageBoxST : public CDialog
- {
- DECLARE_DYNAMIC(CMessageBoxST)
-
- public:
- CMessageBoxST(CWnd* pParent = NULL);
- virtual ~CMessageBoxST();
- void SetMessageBoxDialog(CString Text,CString Title=_T(""),UINT IDI_SET=MB_ICONWARNING,UINT ID_SETBUTTON=MB_OK);
- INT_PTR GetMessageBoxValue();
-
- enum { IDD = IDD_MESSAGE_DIALOG };
-
- protected:
- virtual void DoDataExchange(CDataExchange* pDX);
- virtual BOOL OnInitDialog();
- CString aTitle;
- CString aText;
- CStatic myStatic;
- CStatic icoStatic;
- LPCTSTR IDI_BACK;
- CFont font;
- INT_PTR ID_BACK;
- UINT ID_BUTTON;
- CButton myButton1;
- CButton myButton2;
- CButton myButton3;
- DECLARE_MESSAGE_MAP()
- public:
- afx_msg void OnBnClickedOk();
- afx_msg void OnBnClickedButton1();
- afx_msg void OnBnClickedButton2();
- afx_msg void OnBnClickedButton3();
- afx_msg void OnBnClickedButton4();
- };
MessageBoxST.cpp文件
-
-
-
-
-
-
-
-
-
- #include "stdafx.h"
- #include "MyMessageBox.h" //这里要根据工程名修改
- #include "MessageBoxST.h"
-
-
-
-
- IMPLEMENT_DYNAMIC(CMessageBoxST, CDialog)
-
- CMessageBoxST::CMessageBoxST(CWnd* pParent )
- : CDialog(CMessageBoxST::IDD, pParent)
- {
-
- }
-
- CMessageBoxST::~CMessageBoxST()
- {
- }
-
- void CMessageBoxST::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
-
-
- BEGIN_MESSAGE_MAP(CMessageBoxST, CDialog)
- ON_BN_CLICKED(IDOK, OnBnClickedOk)
- ON_BN_CLICKED(ID_BUTTON_YES, OnBnClickedButton1)
- ON_BN_CLICKED(ID_BUTTON_OK, OnBnClickedButton2)
- ON_BN_CLICKED(ID_BUTTON_NO, OnBnClickedButton3)
- ON_BN_CLICKED(ID_BUTTON_CANCEL, OnBnClickedButton4)
- END_MESSAGE_MAP()
-
-
-
-
- void CMessageBoxST::OnBnClickedOk()
- {
-
-
- }
- void CMessageBoxST::SetMessageBoxDialog(CString Text,CString Title,UINT IDI_SET,UINT ID_SETBUTTON)
- {
- if (Title=="")
- AfxGetMainWnd()->GetWindowText(aTitle);
- else
- aTitle=Title;
- aText=Text;
- switch(IDI_SET)
- {
- case MB_ICONWARNING:
- IDI_BACK=IDI_WARNING;break;
- case MB_ICONQUESTION:
- IDI_BACK=IDI_QUESTION;break;
- case MB_ICONERROR:
- IDI_BACK=IDI_ERROR;break;
- case MB_ICONINFORMATION:
- IDI_BACK=IDI_INFORMATION;break;
- }
- ID_BUTTON=ID_SETBUTTON;
-
- }
- BOOL CMessageBoxST::OnInitDialog()
- {
- CDialog::OnInitDialog();
- int i,high,sum=aText.GetLength();
- #ifdef _UNICODE
- for (i=0,sum=0;i
- {
- if (aText[i]<=0x80 && aText[i]>=0)
- sum++;
- else
- sum+=2;
- }
- #endif
- high=sum/52;
- if (high<2)
- high=0;
- if(sum*4>220)
- {
- sum=85*4;i=sum;
- }
- else
- {
- i=sum*6;
- sum*=8;
- }
- int length=aText.GetLength();
- int sumLength=95+i+45;
- switch(ID_BUTTON)
- {
- case MB_YESNOCANCEL:
- if (sumLength<294)
- sumLength=294+44;
- break;
- case MB_YESNO:
- if (sumLength<195)
- sumLength=195+44;
- break;
- case MB_OKCANCEL:
- if (sumLength<195)
- sumLength=195+44;
- break;
- case MB_OK:
- if (sumLength<96)
- sumLength=96+44;
- break;
- }
- SetWindowPos(NULL,0,0,sumLength,170+high*15,SWP_NOMOVE);
-
- VERIFY(font.CreateFont( 12,0,0, 0,FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("宋体")));
- CRect aRect;
- this->SetWindowText(aTitle);
- this->GetClientRect(&aRect);
- switch(ID_BUTTON)
- {
- case MB_YESNOCANCEL:
- myButton1.Create(_T("是"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-11-88-88-11-88,aRect.bottom-8-25,aRect.right-11-88-88-11-11,aRect.bottom-8), this, ID_BUTTON_YES);myButton1.SetFont(&font);
- myButton2.Create(_T("否"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88-11-88,aRect.bottom-8-25,aRect.right-11-88-11,aRect.bottom-8), this, ID_BUTTON_NO);myButton2.SetFont(&font);
- myButton3.Create(_T("取消"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88,aRect.bottom-8-25,aRect.right-11,aRect.bottom-8), this, ID_BUTTON_CANCEL);myButton3.SetFont(&font);
- break;
- case MB_YESNO:
- myButton1.Create(_T("是"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88-11-88,aRect.bottom-8-25,aRect.right-11-88-11,aRect.bottom-8), this, ID_BUTTON_YES);myButton1.SetFont(&font);
- myButton2.Create(_T("否"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88,aRect.bottom-8-25,aRect.right-11,aRect.bottom-8), this, ID_BUTTON_NO);myButton2.SetFont(&font);
- break;
- case MB_OKCANCEL:
- myButton1.Create(_T("确定"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88-11-88,aRect.bottom-8-25,aRect.right-11-88-11,aRect.bottom-8), this, ID_BUTTON_OK);myButton1.SetFont(&font);
- myButton2.Create(_T("取消"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88,aRect.bottom-8-25,aRect.right-11,aRect.bottom-8), this, ID_BUTTON_CANCEL);myButton2.SetFont(&font);
- break;
- case MB_OK:
- myButton1.Create(_T("确定"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(aRect.right-11-88,aRect.bottom-8-25,aRect.right-11,aRect.bottom-8), this, ID_BUTTON_OK);myButton1.SetFont(&font);
- break;
- }
- myButton1.SetFocus();
-
- icoStatic.Create(_T("ico"), WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTER, CRect(45,45,50,50),this);
- if (icoStatic.GetIcon() == NULL)
- icoStatic.SetIcon(::LoadIcon(NULL, IDI_BACK));
-
- myStatic.Create(aText, WS_CHILD|WS_VISIBLE|SS_LEFT, CRect(95,55,95+sum,95+high*15), this);
- myStatic.SetFont(&font);
- return true;
- }
- void CMessageBoxST::OnBnClickedButton1()
- {
- ID_BACK=IDYES;
- OnOK();
- }
- void CMessageBoxST::OnBnClickedButton2()
- {
- ID_BACK=IDOK;
- OnOK();
- }
- void CMessageBoxST::OnBnClickedButton3()
- {
- ID_BACK=IDNO;
- OnOK();
- }
- void CMessageBoxST::OnBnClickedButton4()
- {
- ID_BACK=IDCANCEL;
- OnOK();
- }
- INT_PTR CMessageBoxST::GetMessageBoxValue()
- {
- return ID_BACK;
- }
特别声明 :代码中有注释的,要依据工程而修改,主要是文件头和对话框资源ID。
例子下载 :