c++ - "How can I make a WNDPROC or DLGPROC a member of my C++ class?"中的 LNK2001

标签 c++ class constructor wndproc

VS10:MCBS:你好, 鉴于此discussionHow can I make a WNDPROC or DLGPROC a member of my C++ class? 中尝试 Raymond Chen 的方法的 Hello World 实现时遇到问题使用 Pudeyev 的 code for "Hello World" :

error LNK2001: unresolved external symbol "private: long __thiscall
BaseWnd::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" 
(?WndProc@BaseWnd@@AAEJPAUHWND__@@IIJ@Z)

代码如下:

// ...Written by Oleg Pudeyev, (c) 2003
#include <windows.h>
HINSTANCE appHinst;

class BaseWnd
{
public:
BaseWnd();

// This is the static callback that we register
static LRESULT CALLBACK s_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

// The static callback recovers the "this" pointer and then calls this member function.
LRESULT BaseWnd::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

};

BaseWnd::BaseWnd(void)
{
    WNDCLASSW wc = {
        // Request redraws as we're centering the string
        CS_HREDRAW|CS_VREDRAW,
        BaseWnd::s_WndProc,
        // No per-class data
        0,
        // No per-window data
        0,
        appHinst,
        LoadIcon(0, IDI_APPLICATION),
        LoadCursor(0, IDC_ARROW),
        HBRUSH(COLOR_BACKGROUND),
        0,
        L"BaseWnd"
    };
RegisterClassW(&wc);
HWND hwnd = CreateWindowW(L"BaseWnd", L"Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, appHinst, this);
ShowWindow(hwnd, SW_SHOW);
}

LRESULT BaseWnd::WndProc(HWND hwnd, UINT Umsg, WPARAM wParam, LPARAM lParam)
{
switch (Umsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
RECT r;
GetClientRect(hwnd, &r);
SetBkMode(ps.hdc, TRANSPARENT);
DrawTextW(ps.hdc, L"Hello, World!", -1, &r, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
EndPaint(hwnd, &ps);
break;

default:
// Use default handling for messages we don't process
return DefWindowProc(hwnd, Umsg, wParam, lParam);
}
return 0;
}

LRESULT CALLBACK BaseWnd::s_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BaseWnd *pThis; // our "this" pointer will go here

if (uMsg == WM_NCCREATE) {
// Recover the "this" pointer which was passed as a parameter to CreateWindow(Ex).
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pThis = static_cast<BaseWnd*>(lpcs->lpCreateParams);
// Put the value in a safe place for future use
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
} else {
// Recover the "this" pointer from where our WM_NCCREATE handler stashed it.
pThis = reinterpret_cast<BaseWnd*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}

if (pThis) {
// Now that we have recovered our "this" pointer, let the member function finish the job.
//Removal of this line removes the LNK2001
return pThis->WndProc(hwnd, uMsg, wParam, lParam);
}

// "this" pointer unknown, so just do the default thing. Hopefully, didn't need to customize behavior yet.
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPTSTR cmdline, int showcmd)
{
appHinst = hinst;
BaseWnd p;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

BaseWnd 构造函数的调用方式或类设置是否有问题 - 或者也许,即 Raymond 文章上的日期,无法应对的 c++11 修订版?

编辑:将 WndProc 替换为绝对限定符 BaseWnd::WndProc,并将有问题的静态回调设为回调。太好了,我们可以处理的代码的其他问题。
Edit2:拼图的最后一 block 是将 Pudeyev 的原始 CS_HREDRAW|CS_VREDRAW 返回到 WNDCLASS 结构。

最佳答案

显然我认为范围的定义是无。

LRESULT CALLBACK WndProc
{
 ↓
LRESULT CALLBACK BaseWnd::WndProc
{

关于c++ - "How can I make a WNDPROC or DLGPROC a member of my C++ class?"中的 LNK2001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111009/

相关文章:

python - unittest.TestCase 的子类的子类,具有不同的 setUpClass 类方法

javascript - 如何通过构造函数将一个对象设置在另一个对象下

c++ - 映射/设置迭代器不是可取消引用的 C++ 映射

c++ - Const Ref to const 指针

java - Java中将类限制为一定数量的对象

php - 类构造函数有什么作用?

c++ - 关于类定义的问题

c++ - 第一个包无法使用非阻塞 tcp 套接字发送

c++ - 具有 'good' 性能的 32 位 Windows 应用程序 : storing buffers in 64-bit executable,

python - 使用 Python GUI (PySide) 元素的 OOP 技术