用C++windows窗口应用程序写一段代码,调试时显示封面界面,然后按下F1、F2、F3、F4、F5时,系统分别跳转五张图片
时间: 2023-07-10 19:32:03 浏览: 120
好的,以下是用C++ Windows窗口应用程序实现你的要求的代码:
首先,在窗口的资源文件中添加六张图片资源,其中一张为封面图片,其余五张为要跳转的图片。
然后在窗口类的头文件中定义五个变量,分别表示五张图片的句柄。
接着,在窗口的消息处理函数中,捕捉键盘按键消息,并根据按下的键盘键码,分别显示对应的图片。
代码如下所示:
```cpp
// 窗口类头文件
class MyWindow {
public:
MyWindow();
~MyWindow();
void Show();
private:
HBITMAP m_hBmpCover; // 封面图片句柄
HBITMAP m_hBmp1; // 图片1句柄
HBITMAP m_hBmp2; // 图片2句柄
HBITMAP m_hBmp3; // 图片3句柄
HBITMAP m_hBmp4; // 图片4句柄
HBITMAP m_hBmp5; // 图片5句柄
HWND m_hWnd;
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnPaint(HWND hWnd);
void OnKeyDown(HWND hWnd, WPARAM wParam);
};
// 窗口类实现文件
MyWindow::MyWindow() {
m_hBmpCover = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_COVER)); // 加载封面图片资源
m_hBmp1 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_IMAGE1)); // 加载图片1资源
m_hBmp2 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_IMAGE2)); // 加载图片2资源
m_hBmp3 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_IMAGE3)); // 加载图片3资源
m_hBmp4 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_IMAGE4)); // 加载图片4资源
m_hBmp5 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_IMAGE5)); // 加载图片5资源
}
MyWindow::~MyWindow() {
DeleteObject(m_hBmpCover); // 释放封面图片资源
DeleteObject(m_hBmp1); // 释放图片1资源
DeleteObject(m_hBmp2); // 释放图片2资源
DeleteObject(m_hBmp3); // 释放图片3资源
DeleteObject(m_hBmp4); // 释放图片4资源
DeleteObject(m_hBmp5); // 释放图片5资源
}
void MyWindow::Show() {
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"MyWindowClass";
RegisterClass(&wc);
m_hWnd = CreateWindow(L"MyWindowClass", L"My Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, this);
ShowWindow(m_hWnd, SW_SHOWDEFAULT);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK MyWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
MyWindow* pThis = NULL;
if (uMsg == WM_NCCREATE) {
pThis = static_cast<MyWindow*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
pThis->m_hWnd = hWnd;
}
else {
pThis = reinterpret_cast<MyWindow*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
if (pThis) {
switch (uMsg) {
case WM_PAINT:
pThis->OnPaint(hWnd);
break;
case WM_KEYDOWN:
pThis->OnKeyDown(hWnd, wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void MyWindow::OnPaint(HWND hWnd) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 绘制封面图片
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmpCover);
BITMAP bmpCover;
GetObject(m_hBmpCover, sizeof(BITMAP), &bmpCover);
BitBlt(hdc, 0, 0, bmpCover.bmWidth, bmpCover.bmHeight, hdcMem, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
void MyWindow::OnKeyDown(HWND hWnd, WPARAM wParam) {
switch (wParam) {
case VK_F1: // 显示图片1
{
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmp1);
BITMAP bmp1;
GetObject(m_hBmp1, sizeof(BITMAP), &bmp1);
BitBlt(hdc, 0, 0, bmp1.bmWidth, bmp1.bmHeight, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
}
break;
case VK_F2: // 显示图片2
{
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmp2);
BITMAP bmp2;
GetObject(m_hBmp2, sizeof(BITMAP), &bmp2);
BitBlt(hdc, 0, 0, bmp2.bmWidth, bmp2.bmHeight, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
}
break;
case VK_F3: // 显示图片3
{
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmp3);
BITMAP bmp3;
GetObject(m_hBmp3, sizeof(BITMAP), &bmp3);
BitBlt(hdc, 0, 0, bmp3.bmWidth, bmp3.bmHeight, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
}
break;
case VK_F4: // 显示图片4
{
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmp4);
BITMAP bmp4;
GetObject(m_hBmp4, sizeof(BITMAP), &bmp4);
BitBlt(hdc, 0, 0, bmp4.bmWidth, bmp4.bmHeight, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
}
break;
case VK_F5: // 显示图片5
{
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, m_hBmp5);
BITMAP bmp5;
GetObject(m_hBmp5, sizeof(BITMAP), &bmp5);
BitBlt(hdc, 0, 0, bmp5.bmWidth, bmp5.bmHeight, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdc);
DeleteDC(hdcMem);
}
break;
default:
break;
}
}
```
在上面的代码中,我们在窗口的消息处理函数中处理了键盘按键消息,并根据按下的键盘键码,分别显示了对应的图片。其中,我们使用了Windows GDI函数库中的函数来显示图片。具体地,我们使用了HDC、HBITMAP、BITMAP等数据类型和CreateCompatibleDC、SelectObject、GetObject、BitBlt等函数来完成图片的显示。
希望这个代码对你有帮助!
阅读全文