vc++高手来看下给分100 ----注册窗口类失败

代码如下#include "windows.h"#include "stdio.h"LRESULT CALLBACK WinSunProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter);int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state){ WNDCLASS wndclass; wndclass.cbClsExtra=0; wndclass.cbClsExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_CROSS); wndclass.hIcon=LoadIcon(NULL,IDI_ERROR); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WinSunProc; wndclass.lpszClassName="error"; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW | CS_VREDRAW; int n=RegisterClass(&wndclass); if(n==0) { MessageBox (NULL,TEXT("注册窗口失败了"), "error",MB_ICONERROR); return 0;} HWND hwnd; hwnd=CreateWindow("error","first window",WS_OVERLAPPEDWINDOW,0,0,200,300,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}LRESULT CALLBACK WinSunProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter){ switch(uMsg) { case WM_CHAR: char s[20]; sprintf(s,"this char is %d",wParam); MessageBox(hwnd,s,"first window",0); break; case WM_LBUTTONDOWN: MessageBox(hwnd,"您按了鼠标左键","first window",MB_YESNO); HDC hdc; hdc=GetDC(hwnd); TextOut(hdc,0,50,"您按了鼠标左键",strlen("您按了鼠标左键")); ReleaseDC(hwnd,hdc); break; case WM_PAINT: HDC hDc; PAINTSTRUCT ps; hDc=BeginPaint(hwnd,&ps); TextOut(hDc,0,0,"我的第一个程序",strlen("我的第一个程序")); EndPaint(hwnd,&ps); break; case WM_CLOSE: if(IDYES==MessageBox(hwnd,"是否关闭窗口","first window",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0;}
2026年09月26日 07:08
有2个网友回答
网友(1):

把注册窗口过程改为如下:
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WinSunProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_FFD);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_FFD;
wcex.lpszClassName = "error";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
if(!RegisterClassEx(&wcex))
{
MessageBox (NULL,TEXT("注册窗口失败了"),
"error",MB_ICONERROR);

return 0;

}

完整程序:
#include "windows.h"
#include "stdio.h"
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WinSunProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_FFD);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_FFD;
wcex.lpszClassName = "error";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
if(!RegisterClassEx(&wcex))
{
MessageBox (NULL,TEXT("注册窗口失败了"),
"error",MB_ICONERROR);

return 0;

}

HWND hwnd;
hwnd=CreateWindow("error","first window",WS_OVERLAPPEDWINDOW,0,0,200,300,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char s[20];
sprintf(s,"this char is %d",wParam);
MessageBox(hwnd,s,"first window",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"您按了鼠标左键","first window",MB_YESNO);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"您按了鼠标左键",strlen("您按了鼠标左键"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,0,0,"我的第一个程序",strlen("我的第一个程序"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否关闭窗口","first window",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);

break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

网友(2):

看不懂,真排卸