简体   繁体   中英

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:

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. 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. I'd rather change the window class to "CCustomView", but still have it have the same behaviours as the window class created above. 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. The message name should contain a GUID to make it unique
  • In Application A, use
    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.
  • The receiving application(s) B now have the window handle of the sender, and can establish a connection (eg by
    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.


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. 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
  • Use DefWindowProc as WNDPROC (that's what MFC does, MFC's WNDPROC is set when creating the window)
  • Use AfxRegisterClass or RegisterClass to register the window class. 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. Otherwise they are roughly equivalent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM