[英]How can I modify this MFC-based code snippet to use a window class name of my own choice?
I am looking at some MFC/C++ CView object subclass like this: 我正在看一些像这样的MFC / C ++ CView对象子类:
BOOL CCustomView::CreateView(DWORD dwStyle,
CDocument * pDocument,
CWnd * pParent,
String title)
{
...
CString className = AfxRegisterWndClass(CS_DBLCLKS,
::LoadCursor(NULL, IDC_IBEAM));
return Create(className, title, dwStyle,
rect, pParent, -1, &context);
}
What I don't like about this, although maybe it's normal for MFC application programming, is that the runtime Window Class Name, is not a name of my own choosing. 我不喜欢这个,虽然MFC应用程序编程可能是正常的,但是运行时窗口类名称不是我自己选择的名称。 If later, I wanted to find this window, from another Win32 application, and find the window by window class name, I'd have to use the ugly "Afx:123:39843:39843" strings, and actually, I don't know if those window class names can be counted on to not change.
如果以后,我想从另一个Win32应用程序找到这个窗口,并按窗口类名找到窗口,我必须使用丑陋的“Afx:123:39843:39843”字符串,实际上,我不知道知道是否可以指望那些窗口类名称不改变。 I'd rather change the window class to "CCustomView", but still have it have the same behaviours as the window class created above.
我宁愿将窗口类更改为“CCustomView”,但仍然具有与上面创建的窗口类相同的行为。 How do I do that?
我怎么做?
There are better ways to solve your problem. 有更好的方法来解决您的问题。 The typical protocol I use is:
我使用的典型协议是:
RegisterWindowMessage
in both applications. RegisterWindowMessage
注册窗口消息。 The message name should contain a GUID to make it unique PostMessage(HWND_BROADCAST, registeredMsg, idIWantToFindYou, HWNDofA)
idIWantTofindYou
to distinguish between different commands for your message. idIWantTofindYou
,您应该限制注册的邮件数量,使用idIWantTofindYou
来区分邮件的不同命令。 PostMessage(HWNDofA, registeredMessage, idHereIsMyHWnd, HWNDofB)
The upside of this mechanism is not running into problems with unresponsive programs. 这种机制的好处是没有遇到无响应程序的问题。 However, the "connection" isn't immediate, so you have to change your program flow.
但是,“连接”不是立即的,因此您必须更改程序流程。 Alternatively, you can use
EnumWindows
and SendMessageTimeout
to probe all top-level windows. 或者,您可以使用
EnumWindows
和SendMessageTimeout
来探测所有顶级窗口。
If you need to use the window class: 如果需要使用窗口类:
The class name assigned by MFC is only so window classes with the same attributes get reused. MFC分配的类名只会重用具有相同属性的窗口类。 I am not aware of any problems with using your own window classes.
我不知道使用自己的窗口类有任何问题。
So the following should work: 所以以下应该有效:
WNDCLASS
or WNDCLASSEX
with the required attributes WNDCLASS
或WNDCLASSEX
DefWindowProc
as WNDPROC
(that's what MFC does, MFC's WNDPROC
is set when creating the window) DefWindowProc
作为WNDPROC
(这就是MFC所做的,在创建窗口时设置MFC的WNDPROC
) AfxRegisterClass
or RegisterClass
to register the window class. AfxRegisterClass
或RegisterClass
注册窗口类。 AfxRegisterClass
checks if the class is already registered and if the class is registered from a DLL, it will unregister the class when the DLL is unloaded. AfxRegisterClass
检查该类是否已经注册,如果该类是从DLL注册的,它将在卸载DLL时取消注册该类。 Otherwise they are roughly equivalent.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.