在类中包装窗口过程 [英] Wrapping up a Window Procedure in a class

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

问题描述

我想为透明的 WinAPI 标签创建一个简单的 C++ 类(使用标准的静态"类实现).为了实现这一点,我创建了一个静态控件并将其窗口过程覆盖为以下一个:

I want to create a simple C++ class for transparent WinAPI labels (implemented using the standard "static" class). To accomplish this, I create a static control and override its Window Procedure to the following one:

LRESULT CALLBACK WndProc_Override(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
    if (Message == WM_PAINT)
    {
        RECT rc;
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rc);
        SetBkMode(hdc, TRANSPARENT);
        DrawText(hdc, Text, strlen(Text), &rc, DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    }

    return CallWindowProc(WndProc_Original, hwnd, Message, wparam, lparam);
}

代码取自 Edward Clements 对这个问题的出色回答:​​C++ Win32 Static控制透明背景

The code was taken from Edward Clements' excellent answer to this question: C++ Win32 Static Control Transparent Background

我想把它包装在一个 C++ 类中,这样我就不必为我想在我的程序中使用的每个标签创建一个新的窗口过程.我希望覆盖的窗口过程是一个成员函数,然后使用 this 来使用标签实例的存储数据(要显示的文本,如下所示:DrawText(hdc, this->;文本, strlen(this->Text), &rc, DT_CENTER | DT_VCENTER);)

I wanted to wrap it up in a C++ class so I don't have to create a new Window Procedure for each label I want to use in my program. I wanted the overridden Window Procedure to be a member function which would then use this to use the label instance's stored data (the text to display, like so: DrawText(hdc, this->Text, strlen(this->Text), &rc, DT_CENTER | DT_VCENTER);)

然而,成员函数要求将 this 作为第一个参数传递,而由于这是一个回调,我无法控制 Windows 如何调用该函数.

However, member functions require that this be passed as the first parameter, whereas since this is a callback, I have no control over how Windows calls the function.

有没有其他方法可以将它包装在一个紧凑的类中,这样我就不必为每个标签创建单独的窗口过程?

推荐答案

您可以在文本字段的用户数据中存储一个指针:

You can store a pointer in the textfield's user data:

myTextField->hWnd = CreateWindow( /*parameters*/ );
SetWindowLongPtr(myTextField->hWnd, GWLP_USERDATA, (LONG_PTR)myTextField);

在你的窗口过程中:

LRESULT CALLBACK WndProc_Override(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
    MyTextFieldClass * myTextField = (MyTextFieldClass *)GetWindowLongPtr(hWnd,GWLP_USERDATA);
    /* rest of method */
}

如果你需要在窗口构建期间访问你的类,你可以这样做:

And if you need to have access to your class during the window construction, you can do it this way:

HWND myTextField = CreateWindow( /*normal parameters*/, myTextField);

并在 WM_CREATE 期间检索它,如下所示:

And retrieve it during WM_CREATE like this:

LRESULT CALLBACK WndProc_Override(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
     if (WM_CREATE == Message)
     {
          LONG_PTR myPtr = (LONG_PTR)((CREATESTRUCT *)lparam)->lpCreateParams;
          SetWindowLongPtr(hwnd, GWLP_USERDATA, myPtr);
          MyTextfieldClass * myTextField = (MyTextfieldClass *)myPtr;
     }
}

这篇关于在类中包装窗口过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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