mfc 对话框显示PNG

使用GDI+可以轻松实现

首先在CxxApp.cpp中加载GDI+

BOOL CxxApp::InitInstance()
{
	// 如果一个运行在 Windows XP 上的应用程序清单指定要
	// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
	//则需要 InitCommonControlsEx()。否则,将无法创建窗口。
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// 将它设置为包括所有要在应用程序中使用的
	// 公共控件类。
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinAppEx::InitInstance();

	AfxEnableControlContainer();

	//装载GDI+

	GdiplusStartupInput m_gdiplusStartupInput;ULONG_PTR m_pGdiToken;
	GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小,则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串,
	// 例如修改为公司或组织名
	...
	// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
	//  而不是启动应用程序的消息泵。

	//卸载GDI+
	GdiplusShutdown(m_pGdiToken);
	return FALSE;
}

在CxxDlg的Onpaint中实现显示

void CShowPngDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDC *pDC;

		pDC =GetDC();

		CRect  rect;

		GetClientRect(&rect); 

		Graphics graphics( pDC->m_hDC);     
		Image image(_T("D:\\Documents\\Visual Studio 2008\\Projects\\ShowPng\\ShowPng\\res\\light.png"), FALSE);     
		graphics.DrawImage(&image,250, 60,image.GetWidth(),image.GetHeight());  //绘制背景


		//CPaintDC dc(this);        
		//if(!img.IsNull()) img.Destroy();
		//img.Load(_T("D:\\Documents\\Visual Studio 2008\\Projects\\ShowPng\\ShowPng\\res\\light.png"));                           //load载入,否则不显示
		//TransparentPNG(&img);                               //调用透明化处理函数
		//if(!img.IsNull()) 
		//	img.Draw(dc.m_hDC, 200, 50);            
		   
		
		CDialog::OnPaint();
	}
}

显示效果如下:

猜你喜欢

转载自blog.csdn.net/xgrdszdx/article/details/81117942