1.5 配置GDI+、加载PNG

1.5.1 配置GDI+环境

1.在你将要使用GDI+的工程中,完成初始化工作,将以下代码加入StdAfx.h头文件中:

#include"GdiPlus.h"

usingnamespace Gdiplus;

#pragmacomment(lib,"gdiplus.lib")

2.在程序初始化部分加入[BOOLxxxxApp::InitInstance()]

//gdi+用到的两个变量   装载gdi+

         GdiplusStartupInputm_gdiplusStartupInput;

         ULONG_PTR m_pGdiToken;

         GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);

3.在程序退出部分前加入[BOOLxxxxApp::InitInstance()]

GdiplusShutdown(m_pGdiToken);

 

如此环境变配置好了,然后我们加载PNG(注:如果PNG像素过大,可能加载不上)

1.5.2 加载PNG

方法一(控件式加载):

         CClientDC *pDC = newCClientDC(GetDlgItem(IDC_STATIC_PIC));

         CRect wrect;

         GetDlgItem(IDC_STATIC_PIC)->GetWindowRect(&wrect);

         Graphics graphics(pDC->m_hDC); //Create a GDI+ graphics object

         Imageimage(L"res\\login1.png",FALSE); // Construct an image

         graphics.DrawImage(&image, 0, 0,image.GetWidth(), image.GetHeight());

         delete pDC;

 

方法二(直接加载):

                   CDC *pDC;

                   pDC =GetDC();

                   Graphics graphics(pDC->m_hDC);

                   Imageimage(L"res\\login1.png", FALSE);

                   graphics.DrawImage(&image,250,150, 48, 48);  //绘制背景

                   CDialogEx::OnPaint();

 

你可能感兴趣的:(mfc,png,对话框,图片加载,GDI+)