[转]VC++技术内幕(第四版)笔记(第7章)

来源:互联网 发布:魔兽世界地图数据库 编辑:程序博客网 时间:2024/06/05 15:00

/*****************************************/
第七章:无模式对话框 Windows通用对话框类

1[无模式对话框]在它处于激活状态下还允许用户在(同一个应用程序中)其它地方工作。
   [
通用对话框]则是C++和一组Windows的实用对话框之间的程序设计借口,包括File Open,PageSetup,Color等等,它们都是通过COMDLG32.DLL来实现的。

2,两种发送Windows消息:
CWnd::SendMessage//
立刻导致对窗口控制函数的调用
CWnd::PostMessage//
将消息放进Windows消息队列。对消息的处理可能被滞后。
具体:
1
LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
//Sends the specified message to this window. The SendMessage member functioncalls the window procedure directly and does not return until that windowprocedure has processed the message. This is in contrast to the PostMessagemember function, which places the message into the window’s message queue andreturns immediately.

2BOOL PostMessage( UINTmessage, WPARAM wParam = 0, LPARAM lParam = 0 );
//Places a message in the window’s message queue and then returns withoutwaiting for the corresponding window to process the message. Messages in amessage queue are retrieved by calls to the GetMessage or PeekMessage Windowsfunction.

3,对话框实际上应该属于应用程序的主框架窗口,而不属于视图。(对话框默认弹出特性)
(注:还未领悟,先留着。)

4,对话框窗口的创建和取消完全取决与用户的操作,而对话框对象则将直到应用程序被终止时才会被删除。
(除了主框架窗口之外,对于几乎所有的窗口类型,DestroyWindow函数都不会将C++对象删除掉。所以要注意手动添加删除对话框对象代码)

5Windows 常量WM_USER是用户自定义消息中可以利用的第一个消息ID
#define WM_USER       0x0400
//The WM_USER constant is used by applications to help define private messages,usually of the form WM_USER+X, where X is an integer value.
说明:
1
CWnd::PostMessage//发送消息。利用wParam , LPARAM可以向响应消息的处理函数传送附加数据信息。
BOOL PostMessage( UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
2
)在WIN32中,用wParam LPARAM参数来传递消息数据是最常用的手段(如:将鼠标的XY坐标压缩进lParam)。而在MFC库中,消息数据可以更多样的类型来传递(如:可以CPoint对象来传递鼠标信息)。
对于用户自定义消息,只能使用wParam LPARAM参数来传递消息附加数据信息。
3
)案例说明:
在对话框类中:
#define WM_GOODBYE WM_USER + 5//
定义自定义消息
m_pView->PostMessage(WM_GOODBYE, IDOK);//
View类发送WM_GOODBYE消息,附加消息IDOK存放在wParam 中。m_pView指向当前View类对象。
View 类对象中
afx_msg LRESULT OnGoodbye(WPARAM wParam, LPARAM lParam);
ON_MESSAGE(WM_GOODBYE, OnGoodbye)
LRESULT CEx07aView::OnGoodbye(WPARAM wParam, LPARAM lParam)
{
 return 0L;
}
4
技巧:在对话框类中重载构造函数,参数为CView*指针。再在对话框类中定义一个CView*指针数据成员。这样,如果在View类中通过传入this 指针来构造对话框对象的时候,对话框类中CView*指针数据成员可以在带参数为CView*指针重载构造函数里方便获取构造它的View类指针。

6 ClassWizard并不支持用户自定义消息的响应,所以当使用用户自定义消息编程的时候,必须自己编写自定义消息的处理代码。(三步,首先是消息响应 函数原型声明,其次消息映射,最后是编写消息响应函数代码。这里要注意:用户自定义消息的消息映射一定要加在BEGIN_MESSAGE_MAP(..)~~END_MESSAGE_MAP()之间,//{{AFX_MSG_MAP(CEx07aView)~~ //}}AFX_MSG_MAP注释宏对之 外)

7,对于无模式对话框一定要注意不要调用CDialog::OnOk或者CDialog::OnCancel函数,既在无模式对话框类 中必须重载这些虚函数;否则当使用ESC键,回车键或者用鼠标单击OK|CANCEL按钮的时候,会激发对应基类函数的调用,进而导致调用Windows EndDialog函数,EndDialog函数只适合于模式对话框。对于无模式对话框,必须调用DestroyWindow函数。
如果需要的话,还可调用Updatedata函数来将数据从对话框控件中传到类数据成员中。

8,Windows通用对话框:
共同特点:都从用户处获得消息,但并不对信息做处理。如:文件对话框只为程序提供路径名,字体对话框只是填充一个描叙字体的结构,并不创建字体。
所有的通用对话框类都从公有基类CCommonDialog派生而来。
COMDLG32
中类列表如下:
CColorDialog  
允许用户选择或创建颜色
CFileDialog  
允许用户打开或者保存一个文件
CFindReplaceDialog 
允许用户将一个字符串换成另一个字符串
CPageSetupDialog 
允许用户输入页面参数
CFontDialog  
允许用户从列出的可用字体中选择一种字体
CPrintDialog  
允许用户设置打印机并打印文档

9,注意:在Win32中,不能在标准文件对话框内部动态创建控件。(其它标准对话框中也应该如此吧)

10,嵌套对话框(这些内容熔入EX07B事例中讲解了,不打算重复,强烈建议看看和跟着做做,页码:P135-141。下面只对其中重要的函数做些说明笔记。)
利用MFC,从通用
1
CFileDialog::m_ofn
//m_ofn is a structure of type OPENFILENAME. Use this structure to initializethe appearance of a File Open or File Save As dialog box after it isconstructed but before it is displayed with the DoModal member function.
2
CFileDialog::CFileDialog
CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTRlpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );
//bOpenFileDialog
Set to TRUE to construct a File Open dialog box or FALSE to construct a FileSave As dialog box.
3
CFileDialog::DoModal
//Call this function to display the Windows common file dialog box and allowthe user to browse files and directories and enter a filename.
//If you want to initialize the various file dialog-box options by settingmembers of the m_ofn structure, you should do this before calling DoModal, butafter the dialog object is constructed.
//When the user clicks the dialog box’s OK or Cancel buttons, or selects theClose option from the dialog box’s control menu, control is returned to yourapplication. You can then call other member functions to retrieve the settingsor information the user inputs into the dialog box.
4
CFileDialog::GetPathName
//Call this function to retrieve the full path of the file entered in thedialog box. The path of the filename includes the file’s title plus the entiredirectory path.
5
)事例中注意设置自己创建的子对话框上的组筐控件的IDstc32。这样才保证文件通用对话框嵌入的位置在组筐所在的位置上,否则默认为自己创建的子对话框的同宽度。(stc32应该是与文件通用对话框相关联的,具体是如何关联的哦?)
6
事例中可见到这样的代码(GetParent()->GetDlgItem(IDOK)->SetWindowText("Delete");)来获取文件通用对话框上的控件指针,这里要理解为什么要用GetParent()函数来获得父窗口指针(因为事例中自己所创建的 对话框被设置成Child Style。)(Child style,Noneborder,Group box ID=stc32这些设置都是必不可少的,自己可以试着改变这些设置,看看效果印象也就深了)
7
)事例中CSpecialFileDialog::OnDelete() 函数中代码(GetParent()->GetDlgItem(0x480)->GetWindowText (m_strFileName);)通过获取文件通用对话框上文件名对应的编辑框的指针调用CWnd::GetWindowText函数来获取编辑框中的 文本保存在m_strFileName数据成员中。其中0x480应该是文件通用对话框上文件名对应的编辑框的ID
8
)事例中CSpecialFileDialog::OnDelete() 函数中代码(GetParent()->SendMessage(WM_COMMAND,IDCANCEL);)向文件通用对话框发送 IDCANCEL消息,该操作引起OnCancel函数的调用终止当前模式对话框同时使得CFileDialog::DoModal函数返回 IDCANCEL值。
MSDN

CDialog::OnCancel
Thedefault simply terminates a modal dialog box by calling EndDialog and causingDoModal to return IDCANCEL.
9
SDK函数:FindFirstFileDeleteFile
//FindFirstFile Searches a directory for a file whose name matches thespecified file name on the destination site identified by this object. Itexamines subdirectory names as well as file names.
//DeleteFile Deletes the given file from the destination site.
10
CFile::Remove
A static function
deletesthe file specified by the path. It will not remove a directory. (注意:用这函数删除的文件是不经过回收站的哦。)

11,在对话框标题栏添加图标:在对话框类OnInitDialog函数中添加如下代码。
 HICON hIcon =AfxGetApp()->LoadIcon(ID_MYICON);//ID_MYICON
是用资源编辑器创建的图标ID
 this->SetIcon(hIcon,TRUE);
 this->SetIcon(hIcon,FALSE);
注:这里带上this指针目的是为了强调必须使用目的对话框类对象指针调用SetIcon设置图标。
比如在书EX07B事例中,由于CSpecialFileDialog类设置为子窗口类,而且所关联的资源窗口没有Tittle Bar,要在父窗口文件通用对话框Tittle Bar上添加图标,必须获取父窗口文件通用对话框对象指针来调用SetIcon设置图标。
如在书EX07B事例中:在CSpecialFileDialog::OnInitDialog函数中添加如下代码,可设置文件通用对话框标题图标: 
 HICON hIcon =AfxGetApp()->LoadIcon(ID_MYICON);
 GetParent()->SetIcon(hIcon,TRUE);
 GetParent()->SetIcon(hIcon,FALSE);

 

原创粉丝点击