子窗口如何响应其父窗口的变化
在 Win32 应用程序中,当子窗口被放置到不同的父窗口中时,是否有 Windows 消息或其他一些通知将被发送到子窗口
In a Win32 app is there a Windows message or some other notification that will get sent to a child window when it is placed into a different parent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很容易在 Windows 窗体应用程序中进行测试。这就是我所看到的:
WM_SHOWWINDOW 是检查父级是否更改的好时机。不能 100% 确定这是否是 WF 代码处理更改的父级的副作用,但可能性相当高。否则没有专门的消息,假设程序已经知道,因为它显式调用了 SetParent 或 SetWindowLongPtr。
This is easy to test in a Windows Forms app. This is what I saw:
WM_SHOWWINDOW would be a good time to check if the parent changed. Not 100% sure if this is a side effect of the WF code taking care of the changed parent, the odds are fairly high. There is otherwise no dedicated message for it, the assumption is that the program already knows since it called SetParent or SetWindowLongPtr explicitly.
没有专门针对此的单一通知。然而,一些框架,如 Borland 的 VCL,将窗口包装在类中,因此当类对象从一个父级移动到另一个父级时,会发出自己的通知(例如,VCL 有
CM_CONTROLLISTCHANGING
、CM_CONTROLLISTCHANGE
和CM_CONTROLCHANGE
通知)。您能否提供更多有关通过检测父窗口的更改到底想要完成什么操作的信息?
There is no single notification specifically for this. However, some frameworks, like Borland's VCL, wrap windows in classes, and thus issue their own notifications when the class objects are moved around from one parent to another (for example, VCL has
CM_CONTROLLISTCHANGING
,CM_CONTROLLISTCHANGE
, andCM_CONTROLCHANGE
notifications).Can you provide more information about what exactly you want to accomplish by detecting a change in parent window?
有点...我在使用 Windows 之间的消息传递和侦听它们的线程之前已经完成了此操作。请记住,您不想从创建它的线程之后的任何线程修改 UI...
以下是父窗口的一些示例代码,它会收到其子窗口之一的更改通知。当做你正在谈论的事情时,同样的原则也适用。当子窗口打开时,父窗口并没有真正发送消息(确实如此,但我忘记了当时它的想法)......自从我不得不做这样的事情以来已经过去了 10 年......但下面的代码是为带有网格的父窗口和打开的子“添加/编辑”窗口而设计的,当您添加或编辑项目时,它将更新模态编辑窗口后面的父网格。这是在 MFC 中设计的,所以你想象一下,你只需要在函数调用中添加一些 HWND 变量即可使其在 Win32 下工作,因为 Win32 和 MFC 是如此相互关联。
首先,在父窗口中,我设置一个线程:
}
然后,在子窗口中,当需要更新时,我会向父窗口发送一条消息。我通过创建一个仅发送消息的线程来完成此操作,以便对话框的其余部分将继续运行...(或者在您的情况下,您可以直接向子 Windows HWND 发送消息以使其更新.. .)
然后,当用户在对话框中选择“下一步”而不是“确定”或“取消”时,所有这一切都开始启动...
好吧,希望这会对您有所帮助,或者给您一些想法...
Sort-of... I've done this before using messaging between Windows, and a thread to listen on them. Remember, you do NOT want to modify the UI from any thread then the one that CREATED it...
Here is some example code of a Parent Window, which gets notified of a change by one of its children Windows. The same principle applies when doing what you are talking about. The parent Windows isn't really pumping messages while the child is open, (it IS, but I forget what is going through its mind at the time)... It's been 10 years since I had to do anything like this... But the code below is designed for a parent Window with a grid, and a child "Add/Edit" window that opens, and when you add or edit an item, it will update the parent GRID behind the Modal Edit window. This was designed in MFC, so you an imagine, you just need to add some HWND vars to the function calls to make it work under Win32, since Win32 and MFC are so inter-related.
First, in the parent Window, I setup a thread:
}
Then, in the child Window, I would send a message back to the parent, when it was time to update. I did this by creating a thread that simply sent the message, so that the rest of the dialog would continue to function... (Or in your case, you could send a message directly to the Child Windows HWND to make it update...)
Then, all this was set into motion when the user selected "NEXT" on the dialog, instead of OK, or CANCEL...
Well, hopefully this will help you some, or give you some ideas...