问题描述
我遵循了一些教程,以将WndProc作为成员函数包含在主窗口的类中,但是,我的程序不断收到未处理的异常,并且我无法弄清楚是什么原因引起的.我试图尽最大努力了解文档 我的能力,但我想我缺少了一些东西.我在WndProc中的switch语句中发现程序达到默认情况时崩溃.我在该区域中有一个消息框来表明这一点.我的假设是这有一些 与传递给:: DefWindowProc(...)的hwnd指针有关.
I followed some tutorials to get WndProc included as a member function to my main window's class, however, my program keep getting an un-handled exception, and I can't figure out what is causing it. I tried to understand the documentation to the best of my ability, but I think I am missing something. I have identified the program crashing when it reaches the default case in my switch statement inside my WndProc. I have message boxes in the area to indicate this. My assumptions are that this has something to do with the hwnd pointer passed to ::DefWindowProc(...).
下面是我的代码.请帮助我了解我在做什么错.将我指向文档或其他内容.
Below is my code. Please help me to understand what I am doing wrong. Point me to documentation or something.
#include <windows.h>
class DXWindow
{
public:
DXWindow(HINSTANCE hInstance, LPCTSTR className);
~DXWindow();
WNDCLASSEX* get_WNDCLASSEX();
bool RegClassEx();
bool CrteWindowEx(HINSTANCE hInstance, LPCTSTR className, int nCmdShow);
private:
WNDCLASSEX* wcex;
HWND hWnd;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
#include "DXWindow.h"
#include <stdlib.h>
#include <stdio.h>
DXWindow::DXWindow(HINSTANCE hInstance, LPCTSTR className)
{
this->wcex->cbSize = sizeof(WNDCLASSEX);
this->wcex->style = CS_HREDRAW | CS_VREDRAW;
this->wcex->lpfnWndProc = StaticWndProc;
this->wcex->cbClsExtra = 0;
this->wcex->cbWndExtra = 0;
this->wcex->hInstance = hInstance;
this->wcex->hIcon = 0;
this->wcex->hCursor = 0;
this->wcex->hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
this->wcex->lpszMenuName = 0;
this->wcex->lpszClassName = className;
this->wcex->hIconSm = 0;
}
DXWindow::~DXWindow()
{
}
WNDCLASSEX* DXWindow::get_WNDCLASSEX()
{
return this->wcex;
}
LRESULT CALLBACK DXWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
char status1[512];
sprintf_s(status1, "WMCreate Called");
MessageBox(0, status1, status1, MB_OK);
}
default:
{
char status1[512];
sprintf_s(status1, "Default case reached");
MessageBox(0, status1, status1, MB_OK);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
return 0;
}
LRESULT CALLBACK DXWindow::StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DXWindow* pDXWindow = (DXWindow*)GetWindowLong(hWnd, GWL_USERDATA);
return pDXWindow->WndProc(hWnd, uMsg, wParam, lParam);
}
bool DXWindow::RegClassEx()
{
if(!::RegisterClassEx(wcex))
{
char error[512];
sprintf_s(error, "Failed to register class");
MessageBox(0, error, error, MB_OK);
exit(0);
}
else
{
char sucess[512];
sprintf_s(sucess, "Registerd dxwindow");
MessageBox(0, sucess, sucess, MB_OK);
}
return TRUE;
}
bool DXWindow::CrteWindowEx(HINSTANCE hInstance, LPCTSTR className, int nCmdShow)
{
hWnd = ::CreateWindowEx(
WS_EX_CLIENTEDGE,
className,
"DXWindow",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
200,
200,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
{
char error[512];
sprintf_s(error, "Failed to create DXWindow");
MessageBox(0, error, error, MB_OK);
exit(0);
}
else
{
char sucess[512];
sprintf_s(sucess, "Created dxwindow");
MessageBox(0, sucess, sucess, MB_OK);
}
ShowWindow(hWnd, nCmdShow);
return TRUE;
}
#include <windows.h>
#include "DXWindow.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
{
DXWindow* dxwnd = new DXWindow(hInstance, "DXWindow");
dxwnd->RegClassEx();
dxwnd->CrteWindowEx(hInstance, "DXWindow", nCmdShow);
return 0;
}
推荐答案
使用 WNDCLASSEX wcex 或将其本地放置在 DXWindow中和 RegisterClassEx 也在其中.
Use WNDCLASSEX wcex or place it locally in DXWindowand RegisterClassEx also in it.
这篇关于WndProc作为成员函数崩溃.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!