用C++windows窗口应用程序写一段代码,调试时显示封面界面,然后按下F1、F2、F3、F4、F5时,系统分别切换五张图片
时间: 2023-07-10 07:32:25 浏览: 118
vc++ 写的一个软键盘,界面可以自己去换张图片的
4星 · 用户满意度95%
好的,以下是一份简单的代码实现:
```c++
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口类
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MyWindowClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// 注册窗口类
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "窗口类注册失败!", "错误", MB_ICONERROR | MB_OK);
return 0;
}
// 创建窗口
HWND hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"MyWindowClass",
"图片切换程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
MessageBox(NULL, "窗口创建失败!", "错误", MB_ICONERROR | MB_OK);
return 0;
}
// 显示封面界面
HDC hdc = GetDC(hWnd);
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "cover.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC, hBmp);
BitBlt(hdc, 0, 0, 600, 400, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
ReleaseDC(hWnd, hdc);
// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBmps[5];
static int idx = 0;
switch (msg)
{
case WM_CREATE:
// 加载五张图片
hBmps[0] = (HBITMAP)LoadImage(NULL, "pic1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hBmps[1] = (HBITMAP)LoadImage(NULL, "pic2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hBmps[2] = (HBITMAP)LoadImage(NULL, "pic3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hBmps[3] = (HBITMAP)LoadImage(NULL, "pic4.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hBmps[4] = (HBITMAP)LoadImage(NULL, "pic5.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
break;
case WM_KEYDOWN:
// 根据按键切换图片
switch (wParam)
{
case VK_F1:
idx = 0;
break;
case VK_F2:
idx = 1;
break;
case VK_F3:
idx = 2;
break;
case VK_F4:
idx = 3;
break;
case VK_F5:
idx = 4;
break;
default:
break;
}
// 绘制图片
HDC hdc = GetDC(hWnd);
HDC hMemDC = CreateCompatibleDC(hdc);
SelectObject(hMemDC, hBmps[idx]);
BitBlt(hdc, 0, 0, 600, 400, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
ReleaseDC(hWnd, hdc);
break;
case WM_DESTROY:
// 释放图片资源
for (int i = 0; i < 5; i++)
{
DeleteObject(hBmps[i]);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
```
在该程序中,我们使用了 `LoadImage` 函数来加载图片资源,并且在窗口创建时显示了封面界面。在消息处理函数中,我们根据按键消息(即 `WM_KEYDOWN`)切换图片,并且使用 `BitBlt` 函数将图片绘制到窗口上。
请将上述代码保存为 `main.cpp` 文件,同时将 `cover.bmp`、`pic1.bmp`、`pic2.bmp`、`pic3.bmp`、`pic4.bmp`、`pic5.bmp` 这六个文件放在同一目录下。然后使用 Visual Studio 等 IDE 编译并运行该程序,即可看到效果。
阅读全文