DestroyWindow函数解析

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消息。

 

转载:DestroyWindow函数解析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值