6.7 Windows通用对话框
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
任务六对话框应用程序设计
6.7 Windows通用对话框
Windows通用对话框是由操作系统提供的任何应用程序都可以获得的对话框。在VC++中,对这些对话框进行了封装,使用户在开发程序时可以方便的调用这些对话框。
6.7.1 使用“文件”对话框打开和保存文件
主程序名:FileDialog
1、添加两个按钮控件,一个编辑框控件,两个静态文本控件
2、为控件关联变量
3、编辑“打开”按钮,编辑程序
void CFileDialogDlg::OnOpen()
{
// TODO: Add your control notification handler code here CFileDialog
dlg(TRUE,NULL,NULL,OFN_HIDEREADONL Y|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 { strText += read[i]; } file.Close(); m_FileText.SetWindowText(strText); } } 4、编辑“保存”按钮,编辑程序 void CFileDialogDlg::OnSave() { // TODO: Add your control notification handler code here CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONL Y|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(); } } 6.7.2 使用“字体”对话框设置文本字体 主程序名:FontDialog 1、添加一个按钮控件,一个编辑框控件 2、为控件关联变量 3、编辑文本中的文字,程序 BOOL CFontDialogDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CString str = ""; str +="有志者,事竟成,破釜沉舟,百二秦关终属楚\r\n"; str +="苦心人,天不负,卧薪尝胆,三千越甲可吞吴\r\n"; m_Text.SetWindowText(str); return TRUE; // return TRUE unless you set the focus to a control } 4、“文字”按钮控件的程序编辑 void CFontDialogDlg::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); //设置字体 } } 6.7.3 使用“颜色”对话框设置文本背景颜色 主程序名:ColorDialog