使用默认窗口处理函数,源码

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
//SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(,,));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx/, cy/);
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps);
return ;
default:
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

运行结果(只能使用Alt+F4关闭):

使用SetWindowLong替换窗户口处理函数,源码

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(,,));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx/, cy/);
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, cx / , cy / , cx, cy);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
return ;
default:
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

运行结果:

使用SetWindowLong接管原来窗口处理函数后,把WM_PAINT消息先在MyProc处理一次 ,然后交给原来的窗口处理函数再处理一次。但是发现并没有绘制出原来窗口处理函数绘制的图形

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(,,));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx/, cy/);
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, cx / , cy / , cx, cy);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
//return 0;
break;
default:
break;
}
return WindProc(hwnd, message, wParam, lParam);
}

经调试,发现WindProc函数中WM_PAINT消息,cx=0,cy=0。绘制了,只不过绘制的矩形坐标时( 0 0 0 0),修改如下

方式①  源码

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
InvalidateRect(hwnd, NULL, FALSE);
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx / , cy / );
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, cx / , cy / , cx, cy);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
break;
default:
break;
}
return WindProc(hwnd, message, wParam, lParam);
}

方式② 源码

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
RECT rect;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
rect.left = ;
rect.bottom = cy / ;
rect.right = cx / ;
rect.top = ;
InvalidateRect(hwnd, &rect, TRUE);
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(,,));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx/, cy/);
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, cx / , cy / , cx, cy);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
break;
default:
break;
}
return WindProc(hwnd, message, wParam, lParam);
}

运行结果:

借助SetWindowLong创建白板窗口

源码

 #include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg;
HBRUSH hBrush = CreateSolidBrush(RGB(, , )); WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = hBrush;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int cx, cy;
HDC hdc;
RECT rect;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
SetWindowLong(hwnd, GWL_STYLE, (LONG)WS_POPUP);
return ;
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
return ;
case WM_PAINT:
rect.left = ;
rect.bottom = cy / ;
rect.right = cx / ;
rect.top = ;
InvalidateRect(hwnd, &rect, TRUE);
hdc = BeginPaint(hwnd, &pt);
HBRUSH hBrush = CreateSolidBrush(RGB(,,));
SelectObject(hdc, hBrush);
Rectangle(hdc, , , cx/, cy/);
DeleteObject(hBrush);
EndPaint(hwnd, &pt);
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int cx, cy; switch (message)
{
case WM_SIZE:
cx = LOWORD(lParam);
cy = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
SelectObject(hdc, hBrush);
Rectangle(hdc, cx / , cy / , cx, cy);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
break;
default:
break;
}
return WindProc(hwnd, message, wParam, lParam);
}

运行结果

最新文章

  1. java对国际化的支持
  2. ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
  3. So easy Webservice 1.Socket建设web服务
  4. Java常用类库
  5. 【EF】 proxy
  6. javascript mvc
  7. &amp;nbsp; 与 空格的区别
  8. Vulkan Tutorial 05 物理设备与队列簇
  9. macos 命令行安装 ipa
  10. PostgreSQL自学笔记:3 数据库的基本操作
  11. Docker私有仓库实例
  12. ELK-Elasticsearch 安装启动
  13. Django-Oscar小记:如何使用高版本Django开发网页的SEO模块
  14. 微信OAuth2.0网页授权设置一个域名需多个域名使用的问题
  15. Golang实现二分查找法
  16. python+selenium自动化软件测试(第3章):unittes
  17. HTML相对路径相对目录--上级目录及下级目录的写法
  18. 《Android编程权威指南》
  19. Angular5 UI post 请求 输出 文件下载
  20. zabbix日常监控项TCP连接状态(六)

热门文章

  1. Can&#39;t accept UDP connections java.net.BindException: Address already in use_解决方案
  2. Brave浏览器
  3. web端调起Windows系统应用程序(exe执行文件),全面兼容所有浏览器
  4. iOS 13 DeviceToken获取发生变化
  5. windows下连接mysql提示1044-access denied for root&#39;&#39;@&#39;localhost&#39; to database
  6. 洛谷 题解 P1041 【传染病控制】
  7. Ansible-Ad_Hoc临时命令的使用
  8. DAO工具类的封装源码
  9. windows 下搭建go开发环境
  10. Authentication源码解析