这个程序是在我找已删除代码时翻出来的,已被永久删除,现在找回来了,现在发出来,可能有用
使用win32,写的比较乱,请见谅
// 图片查看器2023.cpp : 定义应用程序的入口点。
//
#include <Windows.h>
#include <commdlg.h>
#include <gdiplus.h>
#include"Resource.h"
#pragma comment(lib, "gdiplus.lib")
// 全局变量
HWND g_hWnd;
Gdiplus::Image* g_pImage = nullptr;
INT g_nImageWidth = 0;
INT g_nImageHeight = 0;
#define IDM_FILE_ABOUT 1
#define IDM_FILE_OPEN 2
#define IDM_FILE_EXIT 3
// 函数声明
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL OpenImageFile(HWND hWnd);
void DrawImage(HDC hdc);
void Cleanup(); INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
// WinAPI程序入口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 初始化GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 创建窗口
WNDCLASSEX wcex;
memset(&wcex, 0, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"ImageViewerClass";
if (!RegisterClassEx(&wcex))
return 0;
g_hWnd = CreateWindow(L"ImageViewerClass", L"图片查看器", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (!g_hWnd)
return 0;
HMENU hMenu = CreateMenu();
HMENU hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, L"File");
{
AppendMenu(hSubMenu, MF_STRING, IDM_FILE_ABOUT, L"关于");
AppendMenu(hSubMenu, MF_STRING, IDM_FILE_OPEN, L"打开");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSubMenu, MF_STRING, IDM_FILE_EXIT, L"退出");
}
SetMenu(g_hWnd, hMenu);
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 清理资源
Cleanup();
// 关闭GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);
return (int)msg.wParam;
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
// 创建打开按钮
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDM_FILE_OPEN)
{
// 打开按钮被点击
OpenImageFile(hWnd);
}
if (LOWORD(wParam) == IDM_FILE_ABOUT)
{
MessageBox(0, L"图片查看器2023版,李某制作", L"关于", MB_OK | MB_ICONINFORMATION);
}
if (LOWORD(wParam) == IDM_FILE_EXIT) {
PostQuitMessage(0);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 绘制图片
DrawImage(hdc);
EndPaint(hWnd, &ps);
}
break;
case WM_MOUSEWHEEL:
// 鼠标滚轮事件
if (g_pImage)
{
// 根据滚轮的旋转方向调整图片大小
SHORT delta = GET_WHEEL_DELTA_WPARAM(wParam);
if (delta > 0)
{
g_nImageWidth += 10;
g_nImageHeight += 10;
}
else
{
g_nImageWidth -= 10;
g_nImageHeight -= 10;
}
// 重新绘制窗口
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case IDM_ABOUT:
DialogBox(0, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 打开图片文件
BOOL OpenImageFile(HWND hWnd)
{
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile) / sizeof(*szFile);
ofn.lpstrFilter = L"PNG Files\0*.png\0All Files\0*.*\0";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
// 清理旧的图片
Cleanup();
// 加载新的图片
g_pImage = Gdiplus::Image::FromFile(ofn.lpstrFile);
if (g_pImage)
{
g_nImageWidth = g_pImage->GetWidth();
g_nImageHeight = g_pImage->GetHeight();
// 重新绘制窗口
InvalidateRect(hWnd, NULL, TRUE);
}
return TRUE;
}
return FALSE;
}
// 绘制图片
void DrawImage(HDC hdc)
{
if (g_pImage)
{
Gdiplus::Graphics graphics(hdc);
// 绘制图片
graphics.DrawImage(g_pImage, 0, 0, g_nImageWidth, g_nImageHeight);
}
}
// 清理资源
void Cleanup()
{
delete g_pImage;
g_pImage = nullptr;
}
效果图