1 / 30

第 11 章 对话框 (DIALOG)

第 11 章 对话框 (DIALOG). 11.1 对话框模板资源描述文件 11.2 自定义对话框类 11.3 对话框的数据交换和数据检验 11.4 以对话框为主界面的应用程序 11.5 MFC 预置的通用对话框 11.6 非模态对话框 11.7 属性页 ( 自学 ). CWnd. 对话框 (Dialog) :是用户与应用程序交互的重要的界面 ( 输入数据,显示数据 ) 。. CDialog. 设计一个对话框, 创建对话框的模板资源 派生 CDialog 的子类,使用 1) 步的对话框资源. 11.1 对话框模板资源描述文件.

medge-mckee
Download Presentation

第 11 章 对话框 (DIALOG)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第11章 对话框(DIALOG) 11.1 对话框模板资源描述文件 11.2 自定义对话框类 11.3 对话框的数据交换和数据检验 11.4 以对话框为主界面的应用程序 11.5 MFC预置的通用对话框 11.6 非模态对话框 11.7 属性页(自学)

  2. CWnd • 对话框(Dialog):是用户与应用程序交互的重要的界面(输入数据,显示数据)。 CDialog • 设计一个对话框, • 创建对话框的模板资源 • 派生CDialog的子类,使用1)步的对话框资源

  3. 11.1 对话框模板资源描述文件 VC++中通常在资源视图(Resource View)中创建和编辑对话框资源。参见图1和图2,创建的对话框资源保存在.RC文件。 图1 图2

  4. 对话框模板资源描述文件(.RC) 对话框名 DIALOG [载入特性选项] X, Y, Width, Height [设置选项] BEGIN [对话框上控件的定义] END 设置选项:包括CAPTION(标题)、FONT(字体)和STYLE(样式)等。

  5. 对话框常用样式

  6. 对话框中常用的控件及其说明

  7. CObject 11.2 自定义对话框类 CCmdTarget 1. MFC的对话框类及用户自定义对话框类 用户自定义对话框类由CDialog派生。其构造函数如下: CWnd CDialog lpszTemplateName : 不是资源标识的字符串常量形式,而是模板资源的名称。如, CDialog(“IDD_DIALOG1”); // ERROR CDialog(IDD_DIALOG1); // OK 除非.RC文件中定义的模板资源名称是双引号括起来的。 此时, CDialog(“MyDialog”); // OK

  8. 依据对话框资源模板,创建用户自定义的对话框类(CLASSWIZARD)依据对话框资源模板,创建用户自定义的对话框类(CLASSWIZARD)

  9. 2. 模态对话框(Modal Dialog Box) • 对话框分为模态和非模态之分,两者的差别在于对话框是否垄断应用程序的所有消息。 • Modal dialog boxes, which require the user to respond before continuing the program • Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities 模态对话框的创建: virtual int CDialog::DoModal(); Remarks Call this member function to invoke the modal dialog box and return the dialog-box result when done. This member function handles all interaction with the user while the dialog box is active. This is what makes the dialog box modal; that is, the user cannot interact with other windows until the dialog box is closed.

  10. 载入对话框模板 OnOK() OnCancel() Example: CMyDialog dlg; dlg.DoModal(); UpdateData() EndDialog() EndDialog() OnInitDialog() return IDOK return IDCANCEL 垄断消息 UpdateData() OK按钮消息 Cancel按钮消息 消息循环 DoModal()执行过程

  11. 分析、讲解例11-1 virtual void CDialog::OnOK(); virtual void CDialog::OnCancel(); void MFCexp11_1:View:OnLButtonDown(….) { ….. CMyDialog dlg; dlg.DoModal(); ….. }

  12. Binding 显示 11.3 对话框数据交换和数据检验 Control Memory Variables Control Data 1. 数据交换:DDX( Dialog Data eXchange ) 保存 如何实现?

  13. 2. 用Class Wizard为对话框添加成员变量 • Category: • Control: 与控件绑定的是类对象 • Value:与控件中的数据进行绑定 • 使用Control还是Value根据需要设定,可以同时或分别设定

  14. TRUE 3. 如何交换 Control Mem Variable FALSE BOOL CWnd::UpdateData( BOOLbSaveAndValidate=TRUE); bSaveAndValidate Flag that indicates whether dialog box is being initialized (FALSE) or data is being retrieved (TRUE). 如何实现? virtualvoidCWnd::DoDataExchange ( CDataExchange *pDX);

  15. DDX函数 MFC定义了大量的以DDX_为前缀的函数,用来实现变量与控件的绑定以及数据交换。如,DDX_Text()、DDX_Radio()、DDX_Check()等。 DDX_Text void AFXAPI DDX_Text( CDataExchange* pDX, int nIDC, BYTE& value ); void AFXAPI DDX_Text( CDataExchange* pDX, int nIDC, short& value ); ……

  16. UpdateData() DoDataExchange() DDX_XXXX() 分析和讲解 例11-2 4. 数据检验(DDV, Dialog Data Validation) MFC中提供了以DDV_为前缀的函数,用于输入数据的检验。

  17. 11.4 以对话框为主界面的应用程序 模态对话框拥有自己的消息循环和对应的窗口函数,因此除了可以用作应用程序的一个部件之外,还可以用对话框作为应用程序的主界面。如常见的工具软件,杀毒软件、词典等。 1. 创建方法 virtualBOOL CDialog::OnInitDialog(); This member function is called in response to the WM_INITDIALOG message. This message is sent to the dialog box during the Create, CreateIndirect, or DoModal calls, which occur immediately before the dialog box is displayed.

  18. 调用了OnInitDialog() 分析和讲解 例11-3

  19. X:\PROGRAM FILES\...\TEXT.ABC.TXT 11.5 MFC预置的通用对话框 PATH FILE NAME • MFC专门提供了一些通用的对话框类,如, • 颜色选择对话框(CColorDialog) • 文件选择对话框(CFileDialog) • 字体选择对话框(CFontDialog) • 打印和打印设置对话框(CPrintDialog) • …… TITLE 以上对话框类均在afxdlgs.h中进行了定义。 EXTEND 1. 文件对话框(CFileDialog) The CFileDialog class encapsulates the Windows common file dialog box, which provides an easy way to implement the standard File Open and File Save As dialog boxes.

  20. CFileDialog(BOOLbOpenFileDialog, LPCTSTRlpszDefExt=NULL, LPCTSTRlpszFileName=NULL, DWORDdwFlags=OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, LPCTSTRlpszFilter=NULL, CWnd*pParentWnd=NULL); Parameters bOpenFileDialog : TRUE-文件打开对话框;FALSE-保存文件对话框 lpszDefExt : 默认的扩展名。if NULL,则无默认扩展名 lpszFileName :初始的文件名,if NULL,则显示空文件名 dwFlags :不同的组合来自定义对话框 lpszFilter :文件名过滤参数,用于筛选指定的文件 pParentWnd :父窗口 更多信息参考MSDN,或http://msdn.microsoft.com

  21. lpszFilter parameter • 用来指明对话框文件类型框中所使用的过滤参数,每一个参数由两项组成: • 第一项:显示的文本 • 第二项:文件的类型, 即 第一项 | 第二项 • 如果使用多个过滤器,则使用“|”连接起来;同种类型文件的扩展名间可以用“;” 分割,末尾用 “||” 指明。 Example: Word文档(*.doc) | *.doc|| Word文档(*.doc)|*.doc|位图(*.bmp)|*.bmp;*.dib|全部文件(*.*)|*.*|| char * pFilters = “Word文档(*.doc)|*.doc|位图(*.bmp)|*.bmp|全部文件(*.*)|*.*||”; CFileDialog fileDlg(TRUE, NULL, “*.doc”, NULL, pFilters); int result = fileDlg.DoModal(); ……

  22. 常用的成员函数及其功能 CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目录名和扩展名如:c:\test\test1.txtCString CFileDialog::GetFileName( ) 得到文件名,包括扩展名如:test1.txtCString CFileDialog::GetExtName( ) 得到完整的文件扩展名,如:txtCString CFileDialog::GetFileTitle ( ) 得到文件主名,不包括目录名和扩展名如:test1POSITION CFileDialog::GetStartPosition( ) 对于选择了多个文件的情况得到第一个文件位置。CString CFileDialog::GetNextPathName( POSITION& pos ) 对于选择了多个文件的情况得到下一个文件位置,并同时返回当前文件名。 POSITION CFileDialog::GetStartPosition( ) 来得到最初的POSITION变量。

  23. 分析和讲解 例11-4 更正

  24. 2. 字体选择对话框(CFontDialog) CFontDialog(LPLOGFONTlplfInitial=NULL, DWORDdwFlags=CF_EFFECTS| CF_SCREENFONTS, CDC*pdcPrinter=NULL, CWnd*pParentWnd=NULL); 参数说明: lplfInitial:指向一个LOGFONT结构;如果该参数设置为NULL表示不设置初始字体。 pdcPrinter:指向一个代表打印机设备环境的DC对象,若设置该参数则选择的字体就为打印机所用。 pParentWnd:用于指定父窗口。

  25. 通过调用DoModal()创建对话框,在返回后通过调用以下函数来得到用户选择: void CFontDialog::GetCurrentFont( LPLOGFONT lplf ); 用来获得所选字体的属性。该函数有一个参数,该参数是指向LOGFONT结构的指针,函数将所选字体的各种属性写入这个LOGFONT结构中。 CString CFontDialog::GetFaceName( ) :得到所选字体名字。 int CFontDialog::GetSize( ) :得到所选字体的尺寸(以10个象素为单位)。 COLORREF CFontDialog::GetColor( ) :得到所选字体的颜色。 BOOL CFontDialog::IsStrikeOut( )BOOL CFontDialog::IsUnderline( )BOOL CFontDialog::IsBold( )BOOL CFontDialog::IsItalic( )得到所选字体的其他属性,是否有删除线,是否有下划线,是否为粗体,是否为斜体

  26. 通常,定义字体对话框只使用构造函数的第一个参数。如:通常,定义字体对话框只使用构造函数的第一个参数。如: LOGFONT m_logFont; CFontDialog fontDialog(&m_logFont); 分析和讲解 例11-5

  27. 11.6 非模态对话框 • 非模态对话框(Modeless Dialog), • 1)没有自己的、独立的消息循环,而是与应用程序使用同一个消息循环,从而使它不能垄断用户消息。 • 2)设计非模态对话框的方法和过程与模态对话框(Modal Dialog)相同,即设计对话框模板资源,然后是创建对话框类。 • 使用非模态对话框:与模态对话框的使用方法不同,非模态对话框, • 1)打开对话框,使用CDialog::Create(),因为Create()不会启动新的消息循环。 • 2)销毁,使用CWnd::DestroyWindow()销毁,即关闭对话框 • 3)在动态存储器中创建,即使用new操作符。关闭之后使用delete操作符 • 4)使用标志性变量跟踪非模态对话框的运行状态,避免多次打开

  28. Example: CNonMdlDlg * m_pNameDlg; bool m_bActive; m_pNameDlg = new CNonMdlDlg; … … m_bActive = TRUE; CDialog::Create(CNonMdlDlg::IDD); … … m_bActive = FALSE; DestroyWindow(); … … m_pNameDlg->SetActiveWindow(); … … delete m_pNameDlg;

  29. 分析和讲解 例11-6 更正:

  30. 11.7 属性页 (自学)

More Related