用WinAPI实现一个图片查看器:一个打开按钮,点击后打开一个打开文件对话框,打开选中的png文件显示在屏幕上,并可以用鼠标滚轮调整大小
时间: 2024-04-12 07:31:18 浏览: 155
鼠标滚轮放大缩小图片,仿Windows图片查看器-易语言
你可以使用WinAPI来实现一个图片查看器。下面是一个简单的示例代码,它包含一个打开按钮,点击后会打开一个打开文件对话框,选中的png文件将显示在屏幕上,并且可以使用鼠标滚轮调整大小。
```cpp
#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 wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "ImageViewer";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, "窗口类注册失败!", "错误", MB_ICONERROR);
return -1;
}
// 创建窗口
HWND hWnd = CreateWindow("ImageViewer", "图片查看器", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(NULL, "窗口创建失败!", "错误", MB_ICONERROR);
return -1;
}
// 显示窗口
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 hBitmap = NULL;
switch (msg)
{
case WM_CREATE:
{
// 创建打开按钮
HWND hOpenButton = CreateWindow("BUTTON", "打开", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 10, 80, 30, hwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
break;
}
case WM_COMMAND:
{
// 处理按钮点击事件
if (lParam && LOWORD(wParam) == BN_CLICKED)
{
// 创建打开文件对话框
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "PNG 图片\0*.png\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = sizeof(szFileName);
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "png";
if (GetOpenFileName(&ofn))
{
// 加载选中的png图片
hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap)
{
// 调整窗口大小与图片大小一致
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
RECT rc;
GetClientRect(hwnd, &rc);
SetWindowPos(hwnd, NULL, 0, 0, bm.bmWidth, bm.bmHeight, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 重绘窗口
InvalidateRect(hwnd, NULL, TRUE);
}
else
{
MessageBox(hwnd, "无法加载图片!", "错误", MB_ICONERROR);
}
}
}
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (hBitmap)
{
// 绘制图片
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
RECT rc;
GetClientRect(hwnd, &rc);
StretchBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, ps.rcPaint.right, ps.rcPaint.bottom, SRCCOPY);
SelectObject(hdcMem, hOldBitmap);
DeleteDC(hdcMem);
}
EndPaint(hwnd, &ps);
break;
}
case WM_MOUSEWHEEL:
{
// 处理鼠标滚轮事件
if (hBitmap)
{
short zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
RECT rc;
GetClientRect(hwnd, &rc);
if (zDelta > 0)
{
// 放大
rc.right += 10;
rc.bottom += 10;
}
else
{
// 缩小
rc.right -= 10;
rc.bottom -= 10;
}
// 设置新的窗口大小
SetWindowPos(hwnd, NULL, 0, 0, rc.right, rc.bottom, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 重绘窗口
InvalidateRect(hwnd, NULL, TRUE);
}
break;
}
case WM_DESTROY:
{
// 释放资源
if (hBitmap)
{
DeleteObject(hBitmap);
}
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
```
这是一个简单的图片查看器示例,仅供参考。你可以根据自己的需求进行修改和扩展。
阅读全文