请教个MFC问题

dismissme 2008-04-03 10:11:49
请教 为什么这个创建窗口程序, 当我关闭程序时 CPU总是100%啊?

#include<stdio.h>
#include<windows.h>

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
);

LRESULT CALLBACK wpProc(
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, // pointer to command line
int nCmdShow // show state of window
)
{
WNDCLASS wndclass;

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
wndclass.hIcon = LoadIcon(NULL, IDI_ERROR);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = wpProc;
wndclass.lpszClassName = "wp";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClass(&wndclass);

HWND hwnd;
hwnd = CreateWindow("wp", "brace", WS_OVERLAPPEDWINDOW, 0, 0, 600, 600, 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 wpProc(
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 szCh[20];
sprintf(szCh, "helloworld", wParam);
MessageBox(hwnd, szCh, "wp", 0);
break;

case WM_LBUTTONDOWN:
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 10, 20, "this first lesson", strlen("this first lesson"));
ReleaseDC(hwnd, hdc);
break;

case WM_PAINT:
HDC hdc1;
PAINTSTRUCT ps;
hdc1 = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

return 0;
}
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dismissme 2008-04-04
  • 打赏
  • 举报
回复
恩 3楼的说的对 谢谢大家啊
hastings 2008-04-03
  • 打赏
  • 举报
回复

while(GetMessage(&msg, hwnd, 0, 0))
改为:
while(GetMessage(&msg, NULL, 0, 0))
txk1452 2008-04-03
  • 打赏
  • 举报
回复
switch(uMsg)
{
case WM_CHAR:
char szCh[20];
sprintf(szCh, "helloworld", wParam);
MessageBox(hwnd, szCh, "wp", 0);
return 0;

case WM_LBUTTONDOWN:
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 10, 20, "this first lesson", strlen("this first lesson"));
ReleaseDC(hwnd, hdc);
return 0;

case WM_PAINT:
HDC hdc1;
PAINTSTRUCT ps;
hdc1 = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
txk1452 2008-04-03
  • 打赏
  • 举报
回复
WM_CLOSE

Return Values
If an application processes this message, it should return zero.


case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧