未知窗口创建错误? [英] Unknow Window Creation Error ?
问题描述
#include < Windows.h >
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
const LPWSTR szClassName = L 类名跨度>;
const LPWSTR szTitle = L Window Tile 跨度>;
int HEIGHT = 600 ;
int WIDTH = 800 ;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd = NULL;
MSG msg = { 0 };
wc.cbSize = sizeof (WNDCLASSEX);
wc.hInstance = hInstance;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hbrBackground =(HBRUSH)GetStockObject(NULL_BRUSH);
wc.hCursor = LoadCursor(hInstance,IDC_CROSS);
wc.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
wc.hIconSm = LoadIcon(NULL,MAKEINTRESOURCE(IDI_WINLOGO));
wc.lpfnWndProc = WinProc;
wc.lpszClassName = szClassName;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(& wc))
{
MessageBox(NULL,
L 班级注册失败,
L 错误:1,
NULL);
return 1 ;
}
hWnd = CreateWindow(
szClassName,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
500 , 100 ,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
L 调用CreateWindow失败!,
L 错误:2,
NULL);
return 1 ;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(& msg,NULL, 0 , 0 ))
{
TranslateMessage(& msg);
DispatchMessage(& msg);
}
return ( int )msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch ( uMsg)
{
case WM_DESTROY:
PostQuitMessage( 0 ) ;
return 0 ;
break ;
case WM_CHAR:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage( 0 );
return 0 ;
break ;
}
默认:
DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0 ;
}
好的,现在当我调试时,它无法创建窗口,我知道这是一个不起眼的但我真的不知道如何解决它,尝试从msdn复制CreateWindow()函数(没有帮助),所以也许它在WNDCLASSEX结构中(注册时没有任何错误..),...任何人都有任何想法?
尝试用DefWindowProc(hWnd,uMsg,wParam,lParam);cs> 返回 DefWindowProc(hWnd,uMsg,wParam,lParam);
(或设置LRESULT值并返回结尾处WinProc方法)
问候,
Ian。
WinProc应该从DefWindowProc返回值。
请修改WinProc中的默认情况
默认:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
这里Window Procedure没有从默认处理程序返回值,并且总是返回0.它创建了错误。
#include <Windows.h>
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
const LPWSTR szClassName = L"ClassName";
const LPWSTR szTitle = L"Window Tile";
int HEIGHT = 600;
int WIDTH = 800;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd = NULL;
MSG msg = {0};
wc.cbSize = sizeof ( WNDCLASSEX);
wc.hInstance = hInstance;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject( NULL_BRUSH );
wc.hCursor = LoadCursor ( hInstance, IDC_CROSS );
wc.hIcon = LoadIcon ( NULL, MAKEINTRESOURCE(IDI_APPLICATION) );
wc.hIconSm = LoadIcon ( NULL, MAKEINTRESOURCE(IDI_WINLOGO) );
wc.lpfnWndProc = WinProc;
wc.lpszClassName = szClassName;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if ( !RegisterClassEx( &wc ) )
{
MessageBox(NULL,
L"Class Registration Failed",
L"Error:1",
NULL);
return 1;
}
hWnd = CreateWindow(
szClassName,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
L"Call to CreateWindow failed!",
L"Error: 2",
NULL);
return 1;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch( uMsg )
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_CHAR:
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
break;
}
default:
DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
Ok, now when I debug, it fails to create the window, I know this is a nooby one but I really have no idea how to solve it, tryed to copy the CreateWindow() function from msdn(didn''t help), so maybe its in WNDCLASSEX structure (didn''t get any error when registering it..) , ... Anyone has any idea?
Try replacing "DefWindowProc(hWnd,uMsg,wParam,lParam);with
return DefWindowProc(hWnd,uMsg,wParam,lParam);
(or set a LRESULT value and return that at the end of the WinProc method)
Regards,
Ian.
WinProc should return the value from DefWindowProc.
Please modify default case in WinProc
default: return DefWindowProc(hWnd,uMsg,wParam,lParam);
Here Window Procedure is not returning the value from default handler, and always returns 0. It created the error.
这篇关于未知窗口创建错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!