源码资源网 发表于 2025-1-9 16:42:24

MFC对话框中显示PNG图片

MFC对话框中显示PNG图片
在MFC对话框中显示PNG图片需要使用GDI+库来处理PNG格式的图像。以下是一个简单的步骤指南,展示如何在MFC对话框中加载和显示PNG图片。

### 步骤 1: 添加GDI+支持

1. **包含GDI+头文件**:
   在你的MFC项目中,确保包含GDI+的头文件。通常在你的对话框类的实现文件(例如 `YourDialog.cpp`)中添加:

   ```cpp
   #include <gdiplus.h>
   using namespace Gdiplus;
   ```

2. **链接GDI+库**:
   在项目属性中,确保链接GDI+库。可以在项目的属性页中,选择“链接器” -> “输入” -> “附加依赖项”,添加 `gdiplus.lib`。

### 步骤 2: 初始化GDI+

在你的对话框类的构造函数中,初始化GDI+:

```cpp
class CYourDialog : public CDialogEx
{
public:
    CYourDialog(CWnd* pParent = nullptr); // 标准构造函数
    ~CYourDialog();

protected:
    virtual BOOL OnInitDialog();
    virtual void OnPaint();
    void LoadPNGImage();

private:
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Image* m_pImage; // 用于存储PNG图像的指针
};
```


在构造函数中初始化GDI+:

```cpp
CYourDialog::CYourDialog(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_YOUR_DIALOG, pParent), m_pImage(nullptr)
{
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
}

CYourDialog::~CYourDialog()
{
    delete m_pImage;
    GdiplusShutdown(gdiplusToken);
}
```


### 步骤 3: 加载和显示PNG图像

在对话框的 `OnInitDialog` 方法中加载PNG图像:

```cpp
BOOL CYourDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    LoadPNGImage(); // 加载PNG图像

    return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CYourDialog::LoadPNGImage()
{
    m_pImage = new Image(L"your_image.png"); // 替换为你的PNG文件路径
}
```


### 步骤 4: 在 `OnPaint` 方法中绘制图像

重写 `OnPaint` 方法,以便在对话框中绘制PNG图像:

```cpp
void CYourDialog::OnPaint()
{
    CPaintDC dc(this); // 用于绘制的设备上下文

    if (m_pImage != nullptr)
    {
      Graphics graphics(dc.m_hDC);
      graphics.DrawImage(m_pImage, 10, 10); // 在指定位置绘制图像
    }
}
```


### 步骤 5: 编译和运行

确保你的PNG文件路径正确,然后编译并运行你的MFC应用程序。你应该能够在对话框中看到加载的PNG图像。

### 注意事项

1. **图像路径**: 确保PNG文件的路径正确。如果PNG文件在项目目录中,可以使用相对路径。
2. **GDI+ 初始化和清理**: 确保在构造函数中初始化GDI+,在析构函数中清理。
3. **图像大小**: 如果图像过大,可能需要调整绘制的位置和大小。

通过上述步骤,你应该能够在MFC对话框中成功显示PNG图片。


static/image/hrline/1.gif


当然,可以将显示PNG图片的功能封装成一个类,以便更好地管理和复用代码。下面是一个简单的实现示例,展示如何创建一个`CPngImage`类,用于加载和显示PNG图片。

### 1. 创建 `CPngImage` 类

首先,创建一个新的头文件 `PngImage.h` 和相应的实现文件 `PngImage.cpp`。

#### PngImage.h

```cpp
#pragma once
#include <gdiplus.h>

using namespace Gdiplus;

class CPngImage {
public:
    CPngImage();
    ~CPngImage();

    bool LoadImageFromFile(const wchar_t* filePath);
    void DrawImage(CDC* pDC, int x, int y);
    void ReleaseImage();

private:
    Image* m_pImage; // 用于存储PNG图像的指针
};
```


#### PngImage.cpp

```cpp
#include "PngImage.h"

CPngImage::CPngImage() : m_pImage(nullptr) {
}

CPngImage::~CPngImage() {
    ReleaseImage();
}

bool CPngImage::LoadImageFromFile(const wchar_t* filePath) {
    ReleaseImage(); // 释放之前的图像
    m_pImage = new Image(filePath);
    return m_pImage && m_pImage->GetLastStatus() == Ok; // 检查图像是否加载成功
}

void CPngImage::DrawImage(CDC* pDC, int x, int y) {
    if (m_pImage != nullptr) {
      Graphics graphics(pDC->m_hDC);
      graphics.DrawImage(m_pImage, x, y);
    }
}

void CPngImage::ReleaseImage() {
    delete m_pImage;
    m_pImage = nullptr;
}
```


### 2. 在对话框中使用 `CPngImage` 类

在你的对话框类中使用`CPngImage`类来加载和显示PNG图像。

#### YourDialog.h

```cpp
#pragma once
#include "PngImage.h"

class CYourDialog : public CDialogEx {
public:
    CYourDialog(CWnd* pParent = nullptr); // 标准构造函数
    ~CYourDialog();

protected:
    virtual BOOL OnInitDialog();
    virtual void OnPaint();

private:
    CPngImage m_PngImage; // PNG图像对象
};
```


#### YourDialog.cpp

```cpp
#include "pch.h"
#include "YourApp.h"
#include "YourDialog.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CYourDialog::CYourDialog(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_YOUR_DIALOG, pParent) {
}

CYourDialog::~CYourDialog() {
}

BOOL CYourDialog::OnInitDialog() {
    CDialogEx::OnInitDialog();

    // 加载PNG图像
    if (!m_PngImage.LoadImageFromFile(L"your_image.png")) { // 替换为你的PNG文件路径
      AfxMessageBox(L"加载图像失败!");
    }

    return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CYourDialog::OnPaint() {
    CPaintDC dc(this); // 用于绘制的设备上下文
    m_PngImage.DrawImage(&dc, 10, 10); // 在指定位置绘制图像
}
```


### 3. 编译和运行

确保你的PNG文件路径正确,然后编译并运行你的MFC应用程序。你应该能够在对话框中看到加载的PNG图像。

### 总结

通过将PNG图像的加载和绘制功能封装到`CPngImage`类中,你可以更好地管理图像资源,并在不同的对话框或窗口中复用这一功能。这样做使代码结构更清晰,易于维护。



static/image/hrline/1.gif


要在 `CPngImage` 类中添加一个选项,以便支持从资源中加载PNG图片,可以使用Windows API函数 `FindResource` 和 `LoadResource` 来实现。以下是如何修改 `CPngImage` 类以支持从资源中加载PNG图片的步骤。

### 1. 修改 `CPngImage` 类

在 `PngImage.h` 中,添加一个新的方法 `LoadImageFromResource`,用于从资源加载PNG图片。

#### PngImage.h

```cpp
#pragma once
#include <gdiplus.h>

using namespace Gdiplus;

class CPngImage {
public:
    CPngImage();
    ~CPngImage();

    bool LoadImageFromFile(const wchar_t* filePath);
    bool LoadImageFromResource(HMODULE hModule, const wchar_t* resourceName);
    void DrawImage(CDC* pDC, int x, int y);
    void ReleaseImage();

private:
    Image* m_pImage; // 用于存储PNG图像的指针
};
```


#### PngImage.cpp

在 `PngImage.cpp` 中实现 `LoadImageFromResource` 方法。

```cpp
#include "PngImage.h"
#include <windows.h>

CPngImage::CPngImage() : m_pImage(nullptr) {
}

CPngImage::~CPngImage() {
    ReleaseImage();
}

bool CPngImage::LoadImageFromFile(const wchar_t* filePath) {
    ReleaseImage(); // 释放之前的图像
    m_pImage = new Image(filePath);
    return m_pImage && m_pImage->GetLastStatus() == Ok; // 检查图像是否加载成功
}

bool CPngImage::LoadImageFromResource(HMODULE hModule, const wchar_t* resourceName) {
    HRSRC hResource = FindResource(hModule, resourceName, RT_RCDATA);
    if (hResource == nullptr) {
      return false; // 找不到资源
    }

    HGLOBAL hLoadedResource = LoadResource(hModule, hResource);
    if (hLoadedResource == nullptr) {
      return false; // 加载资源失败
    }

    void* pResourceData = LockResource(hLoadedResource);
    DWORD resourceSize = SizeofResource(hModule, hResource);

    // 将资源数据写入到内存流中
    IStream* pStream = nullptr;
    CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    ULONG bytesWritten;
    pStream->Write(pResourceData, resourceSize, &bytesWritten);

    // 创建图像
    pStream->Seek(Gdiplus::Gdiplus::StreamSeekOrigin::STREAM_SEEK_SET, 0, NULL);
    m_pImage = new Image(pStream);
    pStream->Release();

    return m_pImage && m_pImage->GetLastStatus() == Ok; // 检查图像是否加载成功
}

void CPngImage::DrawImage(CDC* pDC, int x, int y) {
    if (m_pImage != nullptr) {
      Graphics graphics(pDC->m_hDC);
      graphics.DrawImage(m_pImage, x, y);
    }
}

void CPngImage::ReleaseImage() {
    delete m_pImage;
    m_pImage = nullptr;
}
```


### 2. 在对话框中使用 `LoadImageFromResource`

在你的对话框类中,你可以选择从文件或资源加载PNG图像。以下是如何在 `CYourDialog` 类中实现这一点。

#### YourDialog.h

```cpp
#pragma once
#include "PngImage.h"

class CYourDialog : public CDialogEx {
public:
    CYourDialog(CWnd* pParent = nullptr); // 标准构造函数
    ~CYourDialog();

protected:
    virtual BOOL OnInitDialog();
    virtual void OnPaint();

private:
    CPngImage m_PngImage; // PNG图像对象
};
```


#### YourDialog.cpp

在 `OnInitDialog` 方法中,你可以选择从文件或资源加载图像。例如,假设你有一个名为 `IDR_MYPNG` 的PNG资源。

```cpp
#include "pch.h"
#include "YourApp.h"
#include "YourDialog.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CYourDialog::CYourDialog(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_YOUR_DIALOG, pParent) {
}

CYourDialog::~CYourDialog() {
}

BOOL CYourDialog::OnInitDialog() {
    CDialogEx::OnInitDialog();

    // 加载PNG图像,从文件或资源
    // 从文件加载
    // if (!m_PngImage.LoadImageFromFile(L"your_image.png")) {

    // 从资源加载
    if (!m_PngImage.LoadImageFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MYPNG))) {
      AfxMessageBox(L"加载图像失败!");
    }

    return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CYourDialog::OnPaint() {
    CPaintDC dc(this); // 用于绘制的设备上下文
    m_PngImage.DrawImage(&dc, 10, 10); // 在指定位置绘制图像
}
```


### 3. 总结

通过以上修改,`CPngImage` 类现在支持从文件和资源加载PNG图像。你可以根据需要选择加载方式。确保在资源文件中添加PNG图片,并为其分配一个资源ID(如 `IDR_MYPNG`)。这样,你就可以在MFC对话框中灵活地使用PNG图片,无论是从文件还是资源。

页: [1]
查看完整版本: MFC对话框中显示PNG图片

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。