如何使表单或窗口控件正常重建? [英] How do I make form or windowed control RecreateWnd-aware properly?

查看:172
本文介绍了如何使表单或窗口控件正常重建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,窗口控件(包括表单)的窗口句柄被认为是易失性的。也就是说,设置一些属性可能会导致控件破坏其窗口句柄,然后重新创建(有一堆该技术的示例,搜索 StdCtrls for 因此,当我需要在创建之后注册我的HWND,并在销毁之前取消注册,我应该覆盖相应的方法对。



简单看看控件表单给了我3对,所有这些都是虚拟的:




  • CreateHandle / DestroyHandle

  • CreateWindowHandle / DestroyWindowHandle

  • CreateWnd / DestroyWnd



有更多的对吗?我应该覆盖哪一个表单?而对于一般窗口控件,哪一对?

解决方案

覆盖 CreateWnd() DestroyWnd()在大多数情况下。覆盖 DestroyWnd()执行窗口取消注册,如果 csRecreating 标志存在于$ code> ControlState 属性,并调用继承的 DestroyWnd()(调用 DestroyWindowHandle() code>)来破坏HWND。覆盖 CreateWnd()调用继承的 CreateWnd()(调用 CreateWindowHandle() / code>)创建HWND,加载和丢弃临时依赖于窗口的数据(如果有的话已保存),然后执行窗口注册。



覆盖 CreateWindowHandle() DestroyWindowHandle()当您需要通过其他方式获取/释放HWND Win32 CreateWindow / Ex() DestroyWindow()调用。例如, TForm 覆盖这些以通过发送 WM_MDICREATE WM_MDIDESTROY来创建/释放MDI子窗口消息到其 ClientHandle 窗口。



我从未见过 CreateHandle() DestroyHandle()覆盖任何有用的东西。



有关详细信息,请参阅此讨论:



CreateWnd和CreateWindowHandle之间有什么区别?


As you know, window handles of windowed controls (this includes forms) are considered volatile. That is, setting some properties may cause control to destroy its window handle and then recreate (there is a bunch of examples of that technique, search StdCtrls for RecreateWnd).

So, when I need to register my HWND with OS after creation and unregister it before destruction I should override corresponding method pair. Brief look to Controls and Forms gave me 3 pairs, all of them virtual:

  • CreateHandle/DestroyHandle
  • CreateWindowHandle/DestroyWindowHandle
  • CreateWnd/DestroyWnd

Are there any more pairs? Which pair should I override for a form? And Which pair for general windowed control?

解决方案

Override CreateWnd() and DestroyWnd() in most situations. Override DestroyWnd() to do window unregistrations, temporarily save any window-dependent data if the csRecreating flag is present in the ControlState property, and call the inherited DestroyWnd() (which calls DestroyWindowHandle()) to destroy the HWND. Override CreateWnd() to call the inherited CreateWnd() (which calls CreateWindowHandle()) to create the HWND, load and discard temporary window-dependent data if any was previously saved, and do window registrations.

Override CreateWindowHandle() and DestroyWindowHandle() when you need to obtain/free an HWND through other means than normal Win32 CreateWindow/Ex() and DestroyWindow() calls. For example, TForm overrides these to create/free MDI child windows by sending WM_MDICREATE and WM_MDIDESTROY messages to its ClientHandle window.

I have never seen CreateHandle() and DestroyHandle() overridden for anything useful.

See this discussion for more details:

What's the difference between CreateWnd and CreateWindowHandle?

这篇关于如何使表单或窗口控件正常重建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆