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

查看:26
本文介绍了预注册 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::Createcode> 函数将注册 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 类——让我头疼.我希望能够预先注册该类,以便我可以将类名与另一个 3rd 方库一起使用,该库将使用 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 Windows 执行此操作,因为我不是第一个遇到此问题的人.

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 Window 的创建必须经过 ATL 类.该类使用窗口thunkthis ptr>.这个 thunk 成为 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 控件的 hwnd 传递给您的第 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天全站免登陆