我一直在跟踪this线程以了解如何将wndproc函数用作成员函数,但是我一直在读取违规访问信息(虽然它不会告诉我程序中的确切行,但是它位于xmemory0 const uintptr_t _Ptr_container = _Ptr_user[-1];
的第120行显示Unhandled exception thrown: read access violation. _Ptr_user was 0x7.
我可能正在做一些我不应该做的时髦的事情。有人看到明显的东西吗?
代码:
LRESULT CALLBACK wndproc::WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
wndproc* pThis; //wndproc is the class name
bool checked = true;
HWND text, button, selection1, selection2;
//hwnd is parent window
switch (msg) {
case WM_CREATE: {
pThis = static_cast<wndproc*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<::LONG_PTR>(pThis));
text = CreateWindow("STATIC", "Please select options from below:", WS_VISIBLE | WS_CHILD, 20, 20, 300, 25, hwnd, NULL, NULL, NULL);
button = CreateWindow("BUTTON", "Submit", WS_VISIBLE | WS_CHILD | WS_BORDER, 500, 500, 80, 25, hwnd, (HMENU)0, NULL, NULL);
selection1 = CreateWindow("button", "Scan?", WS_VISIBLE | WS_CHILD | BS_CHECKBOX | WS_OVERLAPPED, 500, 460, 350, 20, hwnd, (HMENU)1001, NULL, NULL);
CheckDlgButton(hwnd, 1001, BST_CHECKED);
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
vector<string> *strings = reinterpret_cast<vector<string>*>(lpcs->lpCreateParams);
int j = 40;
pThis->checkVectorSize = strings->size();
for (int i = 1; i != strings->size() + 1; i++)
{
CreateWindowA("button", (*strings)[i - 1].c_str(),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX | WS_OVERLAPPED,
20, j, 185, 35, hwnd, (HMENU)i,
NULL, NULL);
CheckDlgButton(hwnd, 0, BST_UNCHECKED);
j = j + 30;
}
//SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<::LONG_PTR>(strings));
break;
}
case WM_COMMAND: {
pThis = reinterpret_cast<wndproc*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
int i = wParam;
if (i == 0) //LOWORD(wParam)
{
for (int j = 0; j != pThis->checkVectorSize; j++)
{
if (IsDlgButtonChecked(hwnd, j + 1) == true)
{
pThis->check.push_back(j);
}
}
if (IsDlgButtonChecked(hwnd, 1001) == true)
{
pThis->scan = true;
}
else
{
pThis->scan = false;
}
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
checked = IsDlgButtonChecked(hwnd, i);
if (checked) {
CheckDlgButton(hwnd, i, BST_UNCHECKED);
}
else {
CheckDlgButton(hwnd, i, BST_CHECKED);
}
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
编辑:包装器功能:
void wndproc::select(vector<string>& return)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASSW wc = { 0 };
MSG msg;
wc.lpszClassName = L"Selection1";
wc.hInstance = hInstance;
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 255));
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.style = CS_DBLCLKS;
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"Selection",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, &return);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
答案 0 :(得分:2)
非静态成员函数与非成员函数不相同。非静态成员函数需要调用一个对象,非成员函数不需要该对象。
如果您不使用任何其他(非静态)成员函数或成员变量,则可以使函数static
正常运行。
考虑到您将“ this”以其他方式传递给函数,另一种可能的解决方案与上述解决方案部分相同,并带有第二个非静态成员函数。
类似
class wndproc
{
...
static LRESULT WndProcWrapper(...)
{
wndproc *pThis;
// Set pThis...
// Call *real* window procedure
return pThis->WndProc(...);
}
private:
LRESULT WndProc(...)
{
...
}
};
然后使wndproc::WndProcWrapper
成为窗口过程。