鸡啄米教程
模态Dlg
非模态Dlg
一般属性页:
void CAdditionDlg::OnBnClickedInstructButton()
{
// 创建属性表对象
CAddSheet sheet(_T("使用说明"));
// 打开模态一般属性页对话框
sheet.DoModal();
}
向导Dlg:
CAddSheet::CAddSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage):CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
// 添加三个属性页到属性表
AddPage(&m_summandPage);
AddPage(&m_addendPage);
AddPage(&m_addPage);
}
void CAdditionDlg::OnBnClickedInstructButton()
{
// TODO: Add your control notification handler code here
// 创建属性表对象
CAddSheet sheet(_T(""));
// 设置属性对话框为向导对话框
sheet.SetWizardMode();
// 打开模态向导对话框
sheet.DoModal();
}
消息提示Dlg:
void CAdditionDlg::OnBnClickedAddButton()
{
INT_PTR nRes=MessageBox(_T("您确定要进行加法计算吗?"), _T("加法计算器"), MB_OKCANCEL | MB_ICONQUESTION);
}
打开文件Dlg:
void CFileopenDlg::OnBnClickedOpenButton()
{
// 设置过滤器
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
// 构造打开文件对话框
CFileDialog fileDlg(TRUE, _T("txt"), NULL, 0, szFilter, this);
CString strFilePath;
// 显示打开文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
strFilePath = fileDlg.GetPathName();
SetDlgItemText(IDC_OPEN_EDIT, strFilePath);
}
}
打开文件Dlg:
void CExample17Dlg::OnBnClickedSaveButton()
{
// 设置过滤器
TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|Word文件(*.doc)|*.doc|所有文件(*.*)|*.*||");
// 构造保存文件对话框
CFileDialog fileDlg(FALSE, _T("doc"), _T("my"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
CString strFilePath;
// 显示保存文件对话框
if (IDOK == fileDlg.DoModal())
{
// 如果点击了文件对话框上的“保存”按钮,则将选择的文件路径显示到编辑框里
strFilePath = fileDlg.GetPathName();
SetDlgItemText(IDC_SAVE_EDIT, strFilePath);
}
}
字体Dlg:
void CExample18Dlg::OnBnClickedFontButton()
{
CString strFontName; // 字体名称
LOGFONT lf; // LOGFONT变量
// 将lf所有字节清零
memset(&lf, 0, sizeof(LOGFONT));
// 将lf中的元素字体名设为“宋体”
_tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("宋体"));
// 构造字体对话框,初始选择字体名为“宋体”
CFontDialog fontDlg(&lf);
if (IDOK == fontDlg.DoModal()) // 显示字体对话框
{
// 如果m_font已经关联了一个字体资源对象,则释放它
if (m_font.m_hObject)
{
m_font.DeleteObject();
}
// 使用选定字体的LOGFONT创建新的字体
m_font.CreateFontIndirect(fontDlg.m_cf.lpLogFont);
// 获取编辑框IDC_FONT_EDIT的CWnd指针,并设置其字体
GetDlgItem(IDC_FONT_EDIT)->SetFont(&m_font);
// 如果用户选择了字体对话框的OK按钮,则获取被选择字体的名称并显示到编辑框里
strFontName = fontDlg.m_cf.lpLogFont->lfFaceName;
SetDlgItemText(IDC_FONT_EDIT, strFontName);
}
}
颜色Dlg:
void CExample19Dlg::OnBnClickedColorButton()
{
COLORREF color = RGB(255, 0, 0); // 颜色对话框的初始颜色为红色
CColorDialog colorDlg(color); // 构造颜色对话框,传入初始颜色值
if (IDOK == colorDlg.DoModal()) // 显示颜色对话框,并判断是否点击了“确定”
{
color = colorDlg.GetColor(); // 获取颜色对话框中选择的颜色值
SetDlgItemInt(IDC_COLOR_EDIT, color); // 在Color编辑框中显示所选颜色值
SetDlgItemInt(IDC_R_EDIT, GetRValue(color)); // 在R编辑框中显示所选颜色的R分量值
SetDlgItemInt(IDC_G_EDIT, GetGValue(color)); // 在G编辑框中显示所选颜色的G分量值
SetDlgItemInt(IDC_B_EDIT, GetBValue(color)); // 在B编辑框中显示所选颜色的B分量值
}
}