1.放置3个button控件Button1 2 3
2.ctrl+W 设置成员变量,选中Button3的click,add Variable
m_Button1
3.手动添加命令消息处理函数
view/ResourceSymbols name:MESSAGE Value:1234
在CE2Dlg中:
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CE2Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButton1();
afx_msg void OnButton2();
afx_msg void OnButton3();
afx_msg void OnNewMessage(); //添加此句
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
----------------
DoDataExchange中:
BEGIN_MESSAGE_MAP(CE2Dlg, CDialog)
//{{AFX_MSG_MAP(CE2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_MESSAGE(NEWMESSAGE,OnNewMessage) //添加此句
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
---------
Button中添加:
void CE2Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
SendMessage(NEWMESSAGE);
}
--------
最加写自定义对话框:
void CE2Dlg::OnNewMessage()
{
MessageBox("自定义消息");
}
=============
退出消息对话框的设置:
void CCloseDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if(MessageBox("确定要退出应用程序吗?","系统提
示",MB_OKCANCEL|MB_ICONQUESTION)!=IDOK)
return;
CDialog::OnClose();
}
//点确定退出,点取消返回
int MessageBox(显示文本,标题,按钮风格|图标|)
=========
//打开保存对话框
//两个静态文本IDC_OPENPATH,IDC_SAVEPATH,一个Edit:IDC_FILETEXT,二个按钮
IDC_OPEN IDC_SAVE
//设置变量后
IDC_FILETEXT CEdit m_FileText
IDC_OPENPATH CStatic m_OpenPath
IDC_SAVEPATH CSTatic m_SavePath
给两个两钮加动作(双击):
-----------
void CFileDialogDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CFileDialog dlg
(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"All Files(*.TXT)|*.TXT||",AfxGetMainWnd());
CString strPath,strText="";
if(dlg.DoModal() == IDOK)
{
strPath = dlg.GetPathName();
m_OpenPath.SetWindowText(strPath);
CFile file(strPath,CFile::modeRead);
char read[10000];
file.Read(read,10000);
for(int i=0;i<file.GetLength();i++)
{
strText += read[i];
}
file.Close();
m_FileText.SetWindowText(strText);
}
}
------
void CFileDialogDlg::OnSave()
{
// TODO: Add your control notification handler code here
CFileDialog dlg
(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"All Files(*.TXT)|*.TXT||",AfxGetMainWnd());
CString strPath,strText="";
char write[10000];
if(dlg.DoModal() == IDOK)
{
strPath = dlg.GetPathName();
if(strPath.Right(4) != ".TXT")
strPath += ".TXT";
m_SavePath.SetWindowText(strPath);
CFile file(_T
(strPath),CFile::modeCreate|CFile::modeWrite);
m_FileText.GetWindowText(strText);
strcpy(write,strText);
file.Write(write,strText.GetLength());
file.Close();
}
}
===========
//设置字体
//一个edit:IDC_TEXT,一个button:IDC_FONT
//在头中插入变量 CFont m_Font;
//设置成员变量 IDC_TEXT CEdit m_Text
//给字体按钮加程序
void CFontDlg::OnFont()
{
// TODO: Add your control notification handler code here
CFont* TempFont = m_Text.GetFont(); //获取当前字体
LOGFONT LogFont;
TempFont->GetLogFont(&LogFont);
CFontDialog dlg(&LogFont); //初始化字体信息
if(dlg.DoModal()==IDOK)
{
m_Font.Detach(); //分离字体
LOGFONT temp; // 声明结构指针
dlg.GetCurrentFont(&temp); //获取当前字体信息
m_Font.CreateFontIndirect(&temp); //直接创建字体
m_Text.SetFont(&m_Font); //设置字体
}
}
//在初始化处加
// TODO: Add extra initialization here
CString str="";
str+="有志者事竞成\r\n";
str+="有心者天不怕\r\n";
m_Text.SetWindowText(str);
===========
//设置背景颜色
//填写一edit和button
//头设置变量 COLORREF m_Color;
//设置button单击事件
void CColorDlg::OnColor()
{
// TODO: Add your control notification handler code here
CColorDialog dlg(m_Color); //创建颜色对话框
if (dlg.DoModal()==IDOK)
{
m_Color=dlg.GetColor(); //获取用户选择的颜色
Invalidate(); //重绘窗口
}
}
//添加一个WM_CTLCOLOR事件
HBRUSH CColorDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT
nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_STATIC) //是否是静态文本
{
pDC->SetBkColor(m_Color); //设置背景颜色
}
return hbr;
}
========