预注册ATL窗口类 [英] Pre-registering an ATL window class

查看:118
本文介绍了预注册ATL窗口类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目使用ATL和WTL的组合,并且已经从CWindowImpl派生了自己的类,看起来像这样:

I am using a combination of the ATL and WTL for a project and have derived my own class from CWindowImpl, which looks something like this:

class CMyControl : public CWindowImpl<CMyControl>
{
public:
    DECLARE_WND_CLASS(_T("MyClassName"))
    ...
    BEGIN_MSG_MAP(CMyControl)
        ...
    END_MSG_MAP()
};

这一切都很好,如果我使用CMyControl::Create创建控件的实例,那么它在后台运行良好,CWindowImpl::Create函数将注册Win32类(在这种情况下称为MyClassName ).

This is all good, and if I use CMyControl::Create to create an instance of the control, then it works fine as under the hood, the CWindowImpl::Create function will register the Win32 class (in this case called MyClassName).

但是,正是这种行为-创建实例时正在注册Win32类-令我头疼.我希望能够预先注册该类,因此可以将类名与另一个第三方库一起使用,该库将使用Win32 CreateWindowEx调用创建窗口,但是我找不到一种简单的方法来执行此操作.当前,我通过使用static作为CreateWindowEx类名称来解决此问题,然后使用CMyWindow::SubclassWindow将我的类附加到它,但这是一个麻烦.

However, it is this behaviour - the Win32 class being registered when an instance is created - that is causing me a headache. I want to be able to register the class up-front so I can use the class name with another 3rd-party library that will create the window using the Win32 CreateWindowEx call, but I can't find a simple way to do this. Currently I workaround this by using static as the CreateWindowEx class name and then use CMyWindow::SubclassWindow to attach my class to it, but this is a kludge.

有人知道如何在不实际创建窗口的情况下注册CWindowImpl派生类,因此我可以将类名成功传递给CreateWindowEx吗?我以为有一种使用ATL窗口执行此操作的标准方法,因为我不是第一个遇到此问题的人.

Does anyone know how to register a CWindowImpl derived class without actually creating a window, so I can pass the class name to CreateWindowEx successfully? I would of thought there was a standard way to do this with ATL windows as I can't be the first to come across this issue.

推荐答案

您尝试执行的操作无效.这是因为ATL/WTL窗口的创建必须经过ATL类.该类通过一个窗口 thunk 来注册其 this ptr. >.该重击变为WNDPROC,并用对象实例的 this ptr替换WNDPROC的HWND参数.

What you are trying to do won't work. This is because the creation of the ATL/WTL Window must go through the ATL class. The class registers its this ptr with a window thunk. This thunk becomes the WNDPROC and replaces the HWND parameter of the WNDPROC with the this ptr of the object instance.

因此,简而言之,如果您知道ATL窗口在幕后的工作方式,那么您就不会尝试这样做.如果您能够注册窗口类,则CreateWindowEx调用将成功创建窗口.但是,不会创建WNDPROC thunk,并且将没有对象实例与您的窗口关联,并且不会调用任何消息处理程序.相反,请查看是否可以使用CWindowImpl :: Create创建窗口,并在创建ATL控件后将其传递给您的第3方库.

So in short, if you knew how ATL windowing worked under the hood, you would not endeavor to try this. If you were able to register the window class, the CreateWindowEx call would succeed in creating the window. However the WNDPROC thunk would not be created and there would be no object instance to associate your window with and none of your message handlers would called. Instead, see if you can create your window using the CWindowImpl::Create and pass your 3rd party library the hwnd of an ATL control once it is created.

这篇关于预注册ATL窗口类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆