-8

我为 Win32 做了一个类,但我不知道为什么会发生这个错误。

这是代码:

主文件

using namespace std;
#include "WindowManager.h"

#define   WIDTH     700
#define   HEIGHT    500
#define   VERSION   0.1

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
{
    WindowManager window("TalkToMe", 100, 100, WIDTH, HEIGHT);

    while(window.isWindowOpen())
    {
        window.PekMessage();
    }

    return 0;
}

窗口管理器.h

#pragma once

#include <Windows.h>

class WindowManager
{
private:
    MSG msg;
    HWND window;

    int stat;
public:
    WindowManager(LPCTSTR title,int x, int y, int width, int height);
    ~WindowManager();
    LRESULT CALLBACK WindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2);

    inline bool isWindowOpen()   { return stat != -1; }
    int  getStat()               { return stat; }
    void PekMessage();
};

窗口管理器.cpp

#include "WindowManager.h"

void WindowManager::PekMessage()
{
    if(PeekMessage(&msg, window, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

LRESULT CALLBACK WindowManager::WindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
{
    switch(uint_Message)
    {
        case 16: // exit button
            stat = -1;
        break;
    }

    return DefWindowProc(winhan,uint_Message,parameter1,parameter2);
}

WindowManager::WindowManager(LPCTSTR title,int x, int y, int width, int height)
{
    stat = 0;

    WNDCLASSEX wnd;
    wnd.cbSize = sizeof(wnd);
    wnd.style  = CS_HREDRAW | CS_VREDRAW;
    wnd.lpfnWndProc = WindowProcedure;
    wnd.cbClsExtra  = 0;
    wnd.cbWndExtra  = 0;
    wnd.hInstance   = GetModuleHandle(NULL);
    wnd.hIcon       = NULL;
    wnd.hCursor     = LoadCursor(NULL,IDC_ARROW);
    wnd.hbrBackground = GetSysColorBrush(NULL);
    wnd.lpszClassName = "TalkToMe";
    wnd.lpszMenuName  = NULL;
    wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&wnd);

    window = CreateWindowEx(WS_EX_CONTROLPARENT, wnd.lpszClassName, title, 
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, x, y, width, height,NULL, NULL, 
        GetModuleHandle(NULL), NULL);
}

WindowManager::~WindowManager()
{
    DestroyWindow(window);
}

这是奇怪的构建失败:

1>------ Build started: Project: Client, Configuration: Debug Win32 ------
1>  WindowManager.cpp
1>c:\users\user\documents\visual studio 2012\projects\talktome\talktome\windowmanager.cpp(31): error C3867: 'WindowManager::WindowProcedure': function call missing argument list; use '&WindowManager::WindowProcedure' to create a pointer to member
1>c:\users\user\documents\visual studio 2012\projects\talktome\talktome\windowmanager.cpp(31): error C2440: '=' : cannot convert from 'LRESULT (__stdcall WindowManager::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'
1>          There is no context in which this conversion is possible
1>  Generating Code...
1> Compiling...
1>  Main.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

3 回答 3

4

这是可以预料的。WindowManager::WindowManager有一个隐藏的this参数,因此它不遵守wnd.lpfnWndProc.

要解决此问题,您需要定义一个辅助函数,如下所示:

LRESULT CALLBACK HelperWindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
{
    if (uint_Message == WM_CREATE)
    {
       /* Retrieve "this" sent through CreateWindowEx */
       WindowManager *wm = (WindowManager *)(((LPCREATESTRUCT) lParam)->lpCreateParams);
       /* And store it as window-private date */
       SetWindowLongPtr(winhan, GWLP_USERDATA, wm);
    }

    /* Retrieve WindowManager from window-private data */
    WindowManager *wm = (WindowManager *)GetWindowLongPtr(winhan, GWLP_USERDATA);
    /* Forward window message to WindowManager */
    wm->WindowProcedure(winhan, uint_Message, parameter1, parameter2);
}

WindowManager::WindowManager()
{
    ...
    /* use helper window procedure */
    wnd.lpfnWndProc = HelperWindowProcedure;
    ...
    /* send "this" through WM_CREATE */
    window = CreateWindowEx(WS_EX_CONTROLPARENT, wnd.lpszClassName, title, 
       WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, x, y, width, height,NULL, NULL, 
       GetModuleHandle(NULL), this);

}
于 2012-09-05T15:59:20.177 回答
1

正如 user1202136 所提到的,C++ 类方法具有隐式参数,无论何时调用它都会传递给函数。它保存指向类实例(this指针)的指针,并允许您访问类变量。为了解决这个问题,您可以将WindowProcedure方法设为静态并GWL_USERDATA在窗口内存中使用来存储您的类实例(this指针)。查找GetWindowLongPtr更多信息。创建窗口时,您需要this通过参数传递指针,并在处理消息时lpParam使用它来存储它。SetWindowLongPtrWM_CREATE

于 2012-09-05T16:08:58.623 回答
-3

检查解决方案:

wnd.lpfnWndProc = (WNDPROC)WindowProcedure;

或者

LRESULT WNDPROC WindowManager::WindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
于 2012-09-05T16:05:46.950 回答