这篇博客详细介绍了如何使用 Win32 API 在 Windows 上创建多个窗口。通过包含必要的头文件、定义全局变量和窗口过程函数、注册窗口类并创建多个窗口,最终实现了在屏幕上随机分布多个窗口的效果。每个窗口的中心都有一个信息图标。博客还提供了完整的代码示例和运行结果展示。
如果你对 Windows 编程感兴趣,这篇博客将是一个很好的入门指南。你可以学习到如何处理窗口消息、绘制图标以及创建多个窗口等基本操作。希望这些内容对你有所帮助!如果你有任何问题或需要进一步的帮助,请随时告诉我。
说明
- 依赖库:本文使用了 Win32 API 库,包含在
windows.h
头文件中。 - 编译环境:确保你的编译器支持 Win32 API,例如 Visual Studio 或 MinGW。
步骤:
步骤1:包含必要的头文件
#include <windows.h>
这个头文件包含了我们需要的大部分Win32 API函数和类型。
步骤2:定义全局变量
HINSTANCE hInstance;
LPCSTR szClassName = "MyWindowClass";
hInstance
是当前程序的实例句柄,szClassName
是我们将要注册的窗口类的名称。
步骤3:定义窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// Draw an icon at the center of the window
int x = (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXICON)) / 2;
int y = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYICON)) / 2;
DrawIcon(hdc, x, y, LoadIcon(NULL, IDI_INFORMATION));
EndPaint(hwnd, &ps);
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
这个函数处理窗口接收到的所有消息。在这个函数中,我们处理了 WM_CLOSE
、WM_DESTROY
和 WM_PAINT
三种消息。
步骤4:定义 WinMain 函数
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
hInstance = hInst;
// Register the window class
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = szClassName;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if (!RegisterClass(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Create multiple windows
for (int i = 0; i < 3; ++i) { // Change 3 to the desired number of windows
int windowWidth = 640;
int windowHeight = 480;
int randX = rand() % (GetSystemMetrics(SM_CXSCREEN) - windowWidth);
int randY = rand() % (GetSystemMetrics(SM_CYSCREEN) - windowHeight);
HWND hwnd = CreateWindow(szClassName, "My Window", WS_OVERLAPPEDWINDOW, randX, randY,
windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
}
// Message loop
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
在 WinMain
函数中,我们首先注册窗口类,然后创建多个窗口,并进入消息循环。
完整代码如下:
#include <windows.h>
HINSTANCE hInstance;
LPCSTR szClassName = "MyWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// Draw an icon at the center of the window
int x = (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXICON)) / 2;
int y = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYICON)) / 2;
DrawIcon(hdc, x, y, LoadIcon(NULL, IDI_INFORMATION));
EndPaint(hwnd, &ps);
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
hInstance = hInst;
// Register the window class
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = szClassName;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if (!RegisterClass(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Create multiple windows
for (int i = 0; i < 100; ++i) { // Change 3 to the desired number of windows
int windowWidth = 100;
int windowHeight = 120;
int randX = rand() % (GetSystemMetrics(SM_CXSCREEN) - windowWidth);
int randY = rand() % (GetSystemMetrics(SM_CYSCREEN) - windowHeight);
HWND hwnd = CreateWindow(szClassName, "My Window", WS_OVERLAPPEDWINDOW, randX, randY,
windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
}
// Message loop
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
运行结果
运行上述代码后,你将看到多个窗口随机分布在屏幕上,每个窗口的中心都有一个信息图标。
其他文章推荐
- 使用Python和Selenium爬取QQ新闻热榜-CSDN博客
- 使用 Python 并发获取系统进程信息-CSDN博客
- 使用Python 进行文本情感分析-CSDN博客
- 使用Python从.exe文件中提取图标-CSDN博客
- 在 Python 中编写一个简单的文件搜索工具-CSDN博客
相关类型推荐
- Win32 API 编程指南
- Windows 编程实例教程
扩展
- 窗口样式和属性:了解更多关于窗口样式(如无边框窗口、工具窗口等)和属性(如透明度、置顶等)的设置。
- 消息处理:深入学习如何处理更多的窗口消息,如键盘输入、鼠标事件等。
- 多线程编程:在创建多个窗口时,如何使用多线程来提高程序的响应速度和性能。
- 自定义控件:学习如何创建和使用自定义控件,如按钮、文本框等。
Windows 操作推荐
- Win32 API 编程指南:全面介绍 Win32 API 的使用,包括窗口管理、图形绘制、文件操作等。
- Windows 编程实例教程:通过实例学习 Windows 编程的各个方面,从基础到高级应用。
- Windows 界面设计:学习如何设计和实现用户友好的 Windows 界面,包括布局、配色、交互设计等。
- 高级 Windows 编程:深入探讨 Windows 编程中的高级技术,如钩子、DLL、COM 等。
结论
以上就是如何在 Windows 上创建多个窗口的方法。通过这些步骤,你可以创建多个窗口,并在每个窗口中展示内容或与用户交互。
总结
通过本文,我们学习了如何使用 Win32 API 在 Windows 上创建多个窗口。我们首先定义了窗口过程函数,然后在 WinMain
函数中注册窗口类并创建多个窗口。最后,我们进入消息循环,处理窗口消息。