DestroyWindow函数解析
2016-07-26 09:16
399 查看
DestroyWindow
MSDN上的解释:
This function destroys the specified window. The function sends a WM_DESTROY message to the window to deactivate it and removes the keyboard focus from it. The function also destroys the window's menu, destroys timers, removes clipboard ownership, and breaks
the clipboard viewer chain (if the window is at the top of the viewer chain).
If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys
the parent or owner window.
DestroyWindow also destroys modeless dialog boxes created by theCreateDialog function.
归纳起来,主要有三层意思:
1、该函数销毁一个指定的窗口。
2、如果指定的窗口是一个父窗口,则该函数自动销毁与之管理的子窗口。
3、该函数也用于销毁用CreateDialog 函数创建的非模式对话框。
下面我们就来看看手动调用DestroyWindow后,该函数是如何销毁非模式对话框的,为了跟踪函数的执行流程,我们在关键位置设置了TRACE语句,其输出顺序为:
1、OnDestroy
2、OnNcDestroy
3、PostNcDestroy
由以上TRACE语句的输出顺序可以看出,手动调用DestroyWindow后,该函数执行过程大致为:
1、该函数先发送WM_DESTROY 消息;
2、OnDestroy函数响应此消息(输出OnDestroy);
3、OnDestroy函数发送WM_NCDESTROY消息;
4、OnNcDestroy函数响应此消息(输出OnNcDestroy);
5、OnNcDestroy函数最后调用PostNcDestroy函数;(输出PostNcDestroy)
在这里,请注意PostNcDestroy函数,该函数经常被用户重载在函数结尾加以下语句:delete this,以删除非模式对话框对象。(DestroyWindow只是销毁了非模式对话框(窗口),但并没有删除其对象。)
因此,综上所叙,通过DestroyWindow函数和delete this语句就到达了分别删除非模式对话框(窗口)及其对象的目的!
另外,通过跟踪程序发现,在模式对话框下,调用OnOk()或者OnCancel函数(EndDialog),会触发WM_DESTROY消息,从而会依次调用OnDestroy、OnNcDestroy、PostNcDestroy函数。TRACE语句按以下顺序输出:
1、OnOK
2、EndDialog
3、OnDestroy
4、OnNcDestroy
5、PostNcDestroy
但是在非模式对话框下,调用OnOk()或者OnCancel函数(EndDialog),则不会触发WM_DESTROY消息,从而不会调用上述函数。这可能就是很多书籍上强调的无模式对话框不要调用OnOk()或者OnCancel函数来销毁而要调用DestroyWindow函数来销毁的原因了。TRACE语句按以下顺序输出:
1、OnOK
2、EndDialog
总结:非模式对话框下OnOk()或者OnCancel函数(EndDialog)不会触发WM_DESTROY消息。
MSDN上的解释:
This function destroys the specified window. The function sends a WM_DESTROY message to the window to deactivate it and removes the keyboard focus from it. The function also destroys the window's menu, destroys timers, removes clipboard ownership, and breaks
the clipboard viewer chain (if the window is at the top of the viewer chain).
If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys
the parent or owner window.
DestroyWindow also destroys modeless dialog boxes created by theCreateDialog function.
归纳起来,主要有三层意思:
1、该函数销毁一个指定的窗口。
2、如果指定的窗口是一个父窗口,则该函数自动销毁与之管理的子窗口。
3、该函数也用于销毁用CreateDialog 函数创建的非模式对话框。
下面我们就来看看手动调用DestroyWindow后,该函数是如何销毁非模式对话框的,为了跟踪函数的执行流程,我们在关键位置设置了TRACE语句,其输出顺序为:
1、OnDestroy
2、OnNcDestroy
3、PostNcDestroy
由以上TRACE语句的输出顺序可以看出,手动调用DestroyWindow后,该函数执行过程大致为:
1、该函数先发送WM_DESTROY 消息;
2、OnDestroy函数响应此消息(输出OnDestroy);
3、OnDestroy函数发送WM_NCDESTROY消息;
4、OnNcDestroy函数响应此消息(输出OnNcDestroy);
5、OnNcDestroy函数最后调用PostNcDestroy函数;(输出PostNcDestroy)
在这里,请注意PostNcDestroy函数,该函数经常被用户重载在函数结尾加以下语句:delete this,以删除非模式对话框对象。(DestroyWindow只是销毁了非模式对话框(窗口),但并没有删除其对象。)
因此,综上所叙,通过DestroyWindow函数和delete this语句就到达了分别删除非模式对话框(窗口)及其对象的目的!
另外,通过跟踪程序发现,在模式对话框下,调用OnOk()或者OnCancel函数(EndDialog),会触发WM_DESTROY消息,从而会依次调用OnDestroy、OnNcDestroy、PostNcDestroy函数。TRACE语句按以下顺序输出:
1、OnOK
2、EndDialog
3、OnDestroy
4、OnNcDestroy
5、PostNcDestroy
但是在非模式对话框下,调用OnOk()或者OnCancel函数(EndDialog),则不会触发WM_DESTROY消息,从而不会调用上述函数。这可能就是很多书籍上强调的无模式对话框不要调用OnOk()或者OnCancel函数来销毁而要调用DestroyWindow函数来销毁的原因了。TRACE语句按以下顺序输出:
1、OnOK
2、EndDialog
总结:非模式对话框下OnOk()或者OnCancel函数(EndDialog)不会触发WM_DESTROY消息。
相关文章推荐
- easyui datagrid 分页保持checkbox选中状态
- 安卓开发中selector的用法
- /proc/$pid/status内容说明
- 2016.07.25回顾
- Hash表实现
- ThinkSNS中判断访问请求请求来源是否为手机的方法
- 奶牛的锻炼...dp
- linux命令小工具-cheat
- POJ 1001 高精度乘法
- unity调用java
- 浅谈原生javascript的select操作
- HTML CSS特殊字符总结
- Web.xml配置详解
- 使用 VisualVM 进行性能分析及调优
- 练习题GRYZ2015 足球联赛(重庆一中高2018级信息学竞赛测验4) 解题报告
- MSSQL Get Last Monday and Last Sunday
- PHP编程时的规范化命名
- UIToolbar
- 7.22BFS&DFS训练计划
- 八皇后