使用gdi+
注意: debug 方式编译会报错,要release
1. 下载 gdi 类库
2. 把 所有头文件和 lib dll 放到项目文件夹下
3.在 stdafx.h 下 加入
#define ULONG_PTR ULONG // gdi+ 相关(这个加上,不然编译报错不通过)
#include "gdi/GdiPlus.h" // gdi+ 相关
#pragma comment(lib, "Gdiplus.lib") // gdi+ 相关
using namespace Gdiplus; // gdi+ 相关
4. 在 APP 的 h文件 中 ,声明变量(初始化gdi+环境和退出销毁时用到) 添加类成员 ULONG_PTR m_gdiplusToken; 又或者在在 APP 的 cpp 中,声明为全局
5. 在 APP 的 cpp 中, BOOL C***App::InitInstance() 函数开头加入代码初始化 gdi+ (位置不对会无法显示图片)
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
6. 利用类向导ClassWizard, 向 APP 中添加方法(重载) ExitInstance()
int C***App::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
7. 显示图片
CClientDC *pDC = new CClientDC(GetDlgItem(IDC_STATIC_SHOW)); // IDC_STATIC_SHOW 为 Static Text 控件的ID
CRect rect;
GetDlgItem(IDC_STATIC_SHOW)->GetWindowRect(&rect);
Graphics graphics(pDC->m_hDC); // Create a GDI+ graphics object
Image im(L"d:\\p.png");
graphics.DrawImage(&im, 0, 0, im.GetWidth(), im.GetHeight());
delete pDC;
浙公网安备 33010602011771号