Code Bye

窗口过程和对话框过程中的WM_PAINT的差别

//在窗口过程中
#include<windows.h>
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT(“HELLO”);
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = wndproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT(“窗口注册失败!”), TEXT(“警告”), MB_OK);
return(0);
}
hwnd = CreateWindow
(szAppName,
TEXT(“THE HELLO”),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
return(0);
case WM_PAINT:
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);//形成死循环!
return(0);
case WM_DESTROY:
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, message, wParam, lParam));
}
//在对话框中
#include<windows.h>
#include”resource.h”
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
BOOL    CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);//对话框过程
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT(“About1”);
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = wndproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, szAppName);//加载图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;//加载菜单
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT(“窗口注册失败!”), TEXT(“警告”), MB_OK);
return(0);
}
hwnd = CreateWindow
(szAppName,
TEXT(“对话框”),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;
switch (message)
{
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
return(0);
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_APP_ABOUT:
DialogBox(hInst,TEXT(“ABOUTBOX”),hwnd,AboutDlgProc);
return(0);
}
return(0);
case WM_DESTROY:
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, message, wParam, lParam));
}
BOOL    CALLBACK AboutDlgProc(HWND hwndDlg, UINT meassage, WPARAM wParam, LPARAM lParam{
switch (meassage)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK://点击对话框上的OK按钮
case IDCANCEL://弹出对话框时按下ESC
EndDialog(hwndDlg,0);
return(TRUE);
}
break;
case WM_PAINT:
InvalidateRect(hwndDlg, NULL, TRUE);
UpdateWindow(hwndDlg);//  会循环多次但不会形成死循环!
break;
}
return (FALSE);
}
以上两个WM_PAINT里一样的代码  但结果却不同  新手不知道为什么  求指导?
解决方案

40

学会使用Spy++软件。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明窗口过程和对话框过程中的WM_PAINT的差别