当前位置:文档之家› 6.7 Windows通用对话框

6.7 Windows通用对话框

任务六对话框应用程序设计6.7 Windows通用对话框Windows通用对话框是由操作系统提供的任何应用程序都可以获得的对话框。

在VC++中,对这些对话框进行了封装,使用户在开发程序时可以方便的调用这些对话框。

6.7.1 使用“文件”对话框打开和保存文件主程序名:FileDialog1、添加两个按钮控件,一个编辑框控件,两个静态文本控件2、为控件关联变量3、编辑“打开”按钮,编辑程序void CFileDialogDlg::OnOpen(){// TODO: Add your control notification handler code here CFileDialogdlg(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<file.GetLength();i++){strText += read[i];}file.Close();m_FileText.SetWindowText(strText);}}4、编辑“保存”按钮,编辑程序void CFileDialogDlg::OnSave(){// TODO: Add your control notification handler code hereCFileDialogdlg(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 使用“字体”对话框设置文本字体主程序名:FontDialog1、添加一个按钮控件,一个编辑框控件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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization hereCString 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 hereCFont* 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 使用“颜色”对话框设置文本背景颜色主程序名:ColorDialog1、添加一个按钮控件,一个静态文本对话框ID:IDD_COLORDIALOG_DIALOG对话框标:使用"颜色"对话框设置文本背景颜色按钮ID:IDC_COLOR按钮标题:颜色静态文本控件ID:IDC_STATIC静态文本控件标题:红笺小字,说尽平生易。

鸿雁在云鱼在水,\r\n惆怅此情难寄。

斜阳独倚西楼,遥山恰对恋钩,\r\n人面不知何处,绿波依旧东流。

\r\n2、“颜色”按钮对话框的程序编写void CColorDialogDlg::OnColor(){// TODO: Add your control notification handler code hereCColorDialog dlg(m_Color); //创建颜色对话框if (dlg.DoModal()==IDOK){m_Color = dlg.GetColor();//获取用户选择的颜色Invalidate();//重绘窗口}}3、添加事件,在处理对话框中添加WM_CTLCOLOR事件,在该事件的处理函数中设置静态文本控件显示文本的背景颜色。

HBRUSH CColorDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);if(nCtlColor == CTLCOLOR_STA TIC){pDC->SetBkColor(m_Color);}return hbr;}6.7.4 使用“查找/替换”对话框在文本中替换字符串主程序名:ReplaceDialog1、添加一个按钮控件,一个文本框控件对话框ID:IDD_REPLACEDIALOG_DIALOG对话框标:使用"查找/替换"对话框在文本中替换字符串按钮ID:IDC_REPLACE按钮标题:替换文本框控件ID:IDC_EDIT12、为控件关联变量3、在头文件中声明变量class CReplaceDialogDlg : public CDialog{// Constructionpublic:CReplaceDialogDlg(CWnd* pParent = NULL); // standard constructor CFindReplaceDialog* dlg; //声明"查找/替换"对话框指针int nindex; //存储查找字符串的起始位置int rindex; //替换字符串的大小BOOL degree; //判断是否为第一次替换的变量BOOL find; //判断是否进行查找的变量4、初始化对话框时添加文本内容程序BOOL CReplaceDialogDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization hereCString str="";str += "君不见黄河之水天上来,奔流到海不复回。

相关主题