且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MFC中通过句柄设置窗口的属性

更新时间:2022-05-15 15:32:10

这取决于你所说的属性...



如果您的意思是Win32窗口属性(窗口属性(Windows ) [ ^ ])然后你可以查看上面的参考资料。



如果你的意思是存储在Win32窗口结构中的数据或 WNDCLASS 结构然后你需要 Set / GetWindowLongPtrPtr 或32位等价物。



如果您指的是与给定窗口句柄关联的MFC C ++对象的成员,您可以使用 CWnd :: FromHandle 来获取来自关联窗口句柄的 CWnd 指针。但是必须有一个与句柄相关联的MFC C ++对象 - 即你已经创建了一个 CWnd 派生对象,名为 Create 在那上面或者使用了一些其他巴洛克式的MFC创建窗口的方法(或者对它进行子类化,但希望是另一个故事)。如果没有与窗口句柄关联的MFC对象,它似乎会起作用,但会出现严重错误。



因此,如果你需要一个 CWnd 稍后执行某些操作的指针在创建MFC对象时隐藏地址,并且不依赖于以后能够恢复它。
It depends what you mean by "properties"...

IF you mean Win32 window properties (Window Properties (Windows)[^]) then you can look at the reference above.

IF you mean data stored in the Win32 window structure or WNDCLASS structure then you'll need Set/GetWindowLongPtrPtr or the 32 bit equivalent.

IF you mean the members of an MFC C++ object associated with a given window handle you can use CWnd::FromHandle to get a CWnd pointer from the associated window handle. However there has to be an MFC C++ object associated with the handle - i.e. you've created a CWnd derived object, called Create on that or used some other baroque MFC method of creating the window (or subclassing it, but that's hopefully another story). If there's no MFC object associated with the window handle it'll appear to work but will go horribly wrong.

So the upshot of this lot is if you need a CWnd pointer to do something later stash the address when you create the MFC object and don't rely on being able to recover it later.