荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: EDE (Thinker醒客), 信区: SoftDev
标  题: [转]VC++技术内幕(第四版)笔记(第7章)
发信站: 荔园晨风BBS站 (Wed Mar  8 09:13:03 2006), 站内


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


1,[无模式对话框]在它处于激活状态下还允许用户在(同一个应用程序中)其它地方工作

   [通用对话框]则是C++和一组Windows的实用对话框之间的程序设计借口,包括File
Open,Page Setup,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 function
calls the window procedure directly and does not return until that window
procedure has processed the message. This is in contrast to the PostMessage
member function, which places the message into the window’s message queue and
returns immediately.

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

3,对话框实际上应该属于应用程序的主框架窗口,而不属于视图。(对话框默认弹出特性

(注:还未领悟,先留着。)

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

5,Windows 常量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参数来传递消息数据是最常用的手段(如:将鼠标的X,
Y坐标压缩进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 initialize
the appearance of a File Open or File Save As dialog box after it is
constructed but before it is displayed with the DoModal member function.
2)CFileDialog::CFileDialog
CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR
lpszFileName = 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 File
Save As dialog box.
3)CFileDialog::DoModal
//Call this function to display the Windows common file dialog box and allow
the user to browse files and directories and enter a filename.
//If you want to initialize the various file dialog-box options by setting
members of the m_ofn structure, you should do this before calling DoModal, but
after the dialog object is constructed.
//When the user clicks the dialog box’s OK or Cancel buttons, or selects the
Close option from the dialog box’s control menu, control is returned to your
application. You can then call other member functions to retrieve the settings
or information the user inputs into the dialog box.
4)CFileDialog::GetPathName
//Call this function to retrieve the full path of the file entered in the
dialog box. The path of the filename includes the file’s title plus the entire
directory path.
5)事例中注意设置自己创建的子对话框上的组筐控件的ID为stc32。这样才保证文件通用对
话框嵌入的位置在组筐所在的位置上,否则默认为自己创建的子对话框的同宽度。(stc32
应该是与文件通用对话框相关联的,具体是如何关联的哦?)
6)事例中可见到这样的代码(GetParent()->GetDlgItem(IDOK)->
SetWindowText("Delete");)来获取文件通用对话框上的控件指针,这里要理解为什么要用
GetParent()函数来获得父窗口指针(因为事例中自己所创建的对话框被设置成Child
Style。)(Child style,None border,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 (The default simply terminates a modal dialog box by calling
EndDialog and causing DoModal to return IDCANCEL.)
9)SDK函数:FindFirstFile,DeleteFile
//FindFirstFile Searches a directory for a file whose name matches the
specified file name on the destination site identified by this object. It
examines subdirectory names as well as file names.
//DeleteFile Deletes the given file from the destination site.
10)CFile::Remove
A static function ,deletes the 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);



--
            │
╭─-╮╭─-┤╭─-╮ 我不存在,存在的只有我的思想
│★ ││╭ ││★ │ 我要做一棵强壮的树,足以保护我爱的你April
├─-┘│╰ │├─-┘
╰─-╯╰─-┴╰─-╯


※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.111.118]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店