用WinAPI实现一个图片查看器:一个file菜单,点击后有以下几个副菜单:关于(用对话框显示程序信息),打开(清空已打开的图片,并打开一个打开文件对话框,打开选中的png文件显示在屏幕上,并可以用鼠标滚轮调整大;另有一个edit菜单:点击后用颜色选择对话框选择画笔颜色对图片进行编辑,编辑完后点击保存按钮保存
时间: 2024-04-16 20:28:56 浏览: 109
商业编程-源码-一个用C写的播放mp3文件的程序.zip
首先,我们需要创建一个窗口,并添加一个菜单栏。然后,在菜单栏中添加 "File" 和 "Edit" 两个主菜单,以及对应的子单项。
下面是一个使用 Win 实现图片查看器的示例代码:
```cpp
#include <windows.h>
// 全局变量
HWND g_hWnd;
HBITMAP g_hBitmap;
COLORREF g_brushColor = RGB(0, 0, 0);
// 函数声明
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void OpenImage();
void EditImage();
// WinAPI 程序入口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
const char szClassName[] = "ImageViewer";
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = szClassName;
RegisterClass(&wc);
// 创建窗口
g_hWnd = CreateWindow(szClassName, "Image Viewer", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
// 创建菜单栏
HMENU hMenu = CreateMenu();
HMENU hFileMenu = CreateMenu();
HMENU hEditMenu = CreateMenu();
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, "File");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hEditMenu, "Edit");
AppendMenu(hFileMenu, MF_STRING, 1, "Open");
AppendMenu(hFileMenu, MF_STRING, 2, "About");
AppendMenu(hEditMenu, MF_STRING, 3, "Edit");
SetMenu(g_hWnd, hMenu);
// 显示窗口
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 1: // File->Open
OpenImage();
break;
case 2: // File->About
MessageBox(hWnd, "Image Viewer\nVersion 1.0", "About", MB_OK | MB_ICONINFORMATION);
break;
case 3: // Edit
EditImage();
break;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 绘制图片
if (g_hBitmap)
{
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, g_hBitmap);
BITMAP bitmap;
GetObject(g_hBitmap, sizeof(BITMAP), &bitmap);
StretchBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
// 释放资源
if (g_hBitmap)
DeleteObject(g_hBitmap);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 打开图片
void OpenImage()
{
OPENFILENAME ofn = { 0 };
char szFileName[MAX_PATH] = "";
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = g_hWnd;
ofn.lpstrFilter = "PNG Files\0*.png\0All Files\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
if (GetOpenFileName(&ofn))
{
// 清空已打开的图片
if (g_hBitmap)
{
DeleteObject(g_hBitmap);
g_hBitmap = NULL;
}
// 加载新图片
g_hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
// 刷新窗口
InvalidateRect(g_hWnd, NULL, TRUE);
}
}
// 编辑图片
void EditImage()
{
CHOOSECOLOR cc = { 0 };
COLORREF custColors[16] = { 0 };
cc.lStructSize = sizeof(cc);
cc.hwndOwner = g_hWnd;
cc.lpCustColors = custColors;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc))
{
g_brushColor = cc.rgbResult;
}
}
```
这段代码创建了一个简单的图片查看器窗口,并实现了 "File" 和 "Edit" 两个主菜单以及对应的子菜单项。当用户点击 "Open" 子菜单项时,会弹出文件选择对话框,选择一个 PNG 文件后,该文件会显示在窗口中。当用户点击 "Edit" 子菜单项时,会弹出颜色选择对话框,选择一个颜色后,该颜色将作为画笔颜色用于编辑图片。
请注意,这只是一个示例代码,可能需要根据实际需求进行适当修改和扩展。
阅读全文