简体   繁体   English

如何修改此基于MFC的代码段以使用我自己选择的窗口类名?

[英]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: 我使用的典型协议是:

  • Register a window message using RegisterWindowMessage in both applications. 在两个应用程序中使用RegisterWindowMessage注册窗口消息。 The message name should contain a GUID to make it unique 消息名称应包含GUID以使其唯一
  • In Application A, use 在应用程序A中,使用
    PostMessage(HWND_BROADCAST, registeredMsg, idIWantToFindYou, HWNDofA)
    to publish your window handle to all top level windows. 你的窗口句柄发布到所有顶级窗口。 since there's more than one use for registered messages, and you should limit the number of messages you register, use idIWantTofindYou to distinguish between different commands for your message. 由于注册邮件的使用idIWantTofindYou ,您应该限制注册的邮件数量,使用idIWantTofindYou来区分邮件的不同命令。
  • The receiving application(s) B now have the window handle of the sender, and can establish a connection (eg by 接收应用程序B现在具有发送者的窗口句柄,并且可以建立连接(例如,通过
    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. 或者,您可以使用EnumWindowsSendMessageTimeout来探测所有顶级窗口。


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: 所以以下应该有效:

  • Fill a WNDCLASS or WNDCLASSEX with the required attributes 使用所需属性填充WNDCLASSWNDCLASSEX
  • Use DefWindowProc as WNDPROC (that's what MFC does, MFC's WNDPROC is set when creating the window) 使用DefWindowProc作为WNDPROC (这就是MFC所做的,在创建窗口时设置MFC的WNDPROC
  • Use AfxRegisterClass or RegisterClass to register the window class. 使用AfxRegisterClassRegisterClass注册窗口类。 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.

相关问题 本地化基于MFC的DLL? - Localize an MFC-based DLL? Visual C ++中基于MFC的项目编译错误 - MFC-based project compilation error in Visual C++ MFC项目组合:如何将不带文档类的SDI应用程序添加到MDI应用程序?我应该使用子窗口吗?[MFC] - MFC Project Combination: How to add a SDI application without document class to a MDI application?should I use child window?[MFC] 为什么我不能将“游戏 Window 类”设置为我的 class 名称? - Why can I not set "Game Window Class" as my class name? 在没有弹出对话框的情况下中止基于 MFC 的 C++ - Aborting an MFC-based C++ without a dialog pop-up 如何在我自己的类Vector中使用Move构造函数而不是Move赋值运算符? - How can I use Move constructor instead of Move assignment operator in my own class Vector? 无法使用我的代码修改我拥有的 Process DACL,但 Process Hacker 可以 - Cannot modify Process DACL that I own with my code but Process Hacker can 如何提高此MFC代码的性能? - How can I increase performance on this MFC code? 如何在自己的代码中使用_DEBUG_ERROR? - How do I use _DEBUG_ERROR in my own code? 我自己的C ++框架(类似于MFC),我可以创建子窗口,但是在WM_CREATE时无法创建编辑框 - My Own C++Framework (MFC-like), i can create child window, but cant create edit box when WM_CREATE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM