vc++ windows显示图片

本文详细介绍了如何使用VC++和GDI+库在Windows环境中创建一个窗口,加载并显示图片。代码展示了从初始化GDI+到处理WM_PAINT消息以调整图片大小和位置的全过程。
摘要由CSDN通过智能技术生成

vc++ windows显示图片

#include <windows.h>
#include <gdiplus.h>

#pragma comment(lib, "Gdiplus.lib")
#include <tchar.h>
using namespace Gdiplus;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 初始化GDI+库
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    // 创建窗口
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WindowProc, 0, 0, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImageDisplay"), NULL };
    RegisterClassEx(&wc);
    HWND hwnd = CreateWindow(wc.lpszClassName, _T("Image Display"), WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, wc.hInstance, NULL);
    if (!hwnd)
    {
        MessageBox(NULL, _T("窗口创建失败!"), _T("错误"), MB_ICONERROR);
        return 1;
    }

    // 显示窗口
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // 进入消息循环
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 清理GDI+资源
    GdiplusShutdown(gdiplusToken);

    return static_cast<int>(msg.wParam);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static Image* pImage = nullptr;

    switch (uMsg)
    {
    case WM_CREATE:
        // 加载图片
        pImage = Image::FromFile(L"E:\\data\\body_pose\\0130\\20240130\\ref\\01301\\frame_0001.jpg");
        if (pImage->GetLastStatus() != Ok)
        {
            MessageBox(hwnd, _T("图片加载失败!"), _T("错误"), MB_ICONERROR);
            return -1;
        }

        break;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        Graphics graphics(hdc);
        if (pImage)
        {// 获取控件的尺寸
            RECT clientRect;
            GetClientRect(hwnd, &clientRect);
            int destWidth = clientRect.right;
            int destHeight = clientRect.bottom;

            // 计算缩放比例,以使图片填充控件
            float scaleX = static_cast<float>(destWidth) / pImage->GetWidth();
            float scaleY = static_cast<float>(destHeight) / pImage->GetHeight();
            float scale = min(scaleX, scaleY);

            int scaledWidth = static_cast<int>(pImage->GetWidth() * scale);
            int scaledHeight = static_cast<int>(pImage->GetHeight() * scale);

            // 居中显示图片
            int xOffset = (destWidth - scaledWidth) / 2;
            int yOffset = (destHeight - scaledHeight) / 2;

            graphics.DrawImage(pImage, xOffset, yOffset, scaledWidth, scaledHeight);
        }
        
        EndPaint(hwnd, &ps);
    }
        break;
    

    case WM_DESTROY:
        if (pImage)
        {
            delete pImage;
        }
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值