最近看孙鑫的VC教程,跟着写了下面的窗口代码,但是运行后占用100%,请大家指教。
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(
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 wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_QUESTION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindowProc;
wndcls.lpszClassName="classname";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass (&wndcls);
HWND hWnd;
hWnd=CreateWindow("classname","chuangkoubiaoti",WS_OVERLAPPEDWINDOW,100,100,100,100,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg,hWnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
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 *szchar;
sprintf(szchar,"char is %d",wParam);
MessageBox(hwnd,szchar,"biaoti",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"you have pressed lbutton!","tishi",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,12,42,"ni an xia le zuo jian.",strlen("ni an xia le zuo jian."));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hdc2;
PAINTSTRUCT ps;
hdc2=BeginPaint(hwnd,&ps);
TextOut(hdc2,0,0,"chuang kou chong hui le;",strlen("chuang kou chong hui le;"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"你是否要关闭窗口?","TISHI",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)