VC利用GDI+显示透明的PNG图片

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

//加入的头文件
#include <comdef.h>//初始化一下com口
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#endif
#pragma comment(lib,"gdiplus.lib")

2.在程序初始化部分加入

//gdi+用到的两个变量   装载gdi+
    GdiplusStartupInput m_gdiplusStartupInput;
    ULONG_PTR m_pGdiToken;
    GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);

3.在程序退出部分前加入
    //卸载gdi+
    GdiplusShutdown(m_pGdiToken);

5在Project->stting->Link->Object/libary中加入gdiplus.lib

这样你的工程就可以使用GDI+了:

void CPNGDlg::OnButton1()
{
pDC =GetDC();
Graphics graphics( pDC->m_hDC);
Image image(L"walkman.png", FALSE);
graphics.DrawImage(&image, 10,10);
}


GDI+是Windows XP中的一个子系统,它主要负责在显示屏幕和打印设备输出有关信息,它是一组通过C++类实现的应用程序编程接口。顾名思义,GDI+是以前版本GDI的继承者,出于兼容性考虑,Windows XP仍然支持以前版本的GDI,但是在开发新应用程序的时候,开发人员为了满足图形输出需要应该使用GDI+,因为GDI+对以前的Windows版本中GDI进行了优化,并添加了许多新的功能。

 

不要再使用OleLoadPicture了,那会让你看起来很愚蠢。同样功能的函数,你看看GDI+会有几行:

HRESULT ShowPic(const TCHAR* lpstrFile, HDC hDC, int X, int Y, int iWidth, int iHeight)
{
_ASSERT(lpstrFile);
USES_CONVERSION;

// Create bitmap object and load image
auto_ptr<Bitmap> pBmp(new Bitmap(CT2CW(lpstrFile)));
if(!pBmp.get() || Ok != pBmp->LastStatus())
return E_FAILED;

// Draw bitmap on DC
Graphic grp(hDC);
if(Ok != grp.DrawImage(pBmp.get(), X, Y, iWidth, iHeight))
return E_FAILED;
return S_OK;
}

 

Graphics mygraphics(hdc);//创建一个Graphics对象
    SolidBrush brush(Color(100,0,0,255));//创建一个笔刷 第一项是透明度 后三项是RGB值
    FontFamily fontFamily(L"Latha");选择一种字体
    Font font(&fontFamily,1,FontStyleRegular,UnitInch);
    PointF pointF(5,5);

    mygraphics.DrawString(L"Hello Word!",-1,&font,pointF,&brush);

 

设置窗口透明:

//设置窗口扩展风格
 SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,
  GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE) | 0x80000);
 typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
 FSetLayeredWindowAttributes SetLayeredWindowAttributes ;
 HINSTANCE hInst = LoadLibrary("User32.DLL");
 SetLayeredWindowAttributes = (FSetLayeredWindowAttributes)
 GetProcAddress(hInst,"SetLayeredWindowAttributes");
 if(SetLayeredWindowAttributes)
 SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);
 FreeLibrary(hInst);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值