教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

VC++技术内幕(windows编程篇)(8)

来源:网络收集 时间:2026-07-12
导读: 2,两种发送Windows消息: CWnd::SendMessage//立刻导致对窗口控制函数的调用 CWnd::PostMessage//将消息放进Windows消息队列。对消息的处理可能被滞后。 具体: 1)LRESULT SendMessage( UINT message, WPARAM wPa

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应该是与文件通用对话框相关联的,具体是如何关联 …… 此处隐藏:3449字,全部文档内容请下载后查看。喜欢就下载吧 ……

VC++技术内幕(windows编程篇)(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/592946.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)