GDI+ 的使用

http://blog.csdn.net/cashey1991/article/details/7407425

http://blog.csdn.net/zhongbin104/article/details/8730935


CXXXApp 头文件中添加:

#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")

private:
ULONG_PTR m_gdiplusToken;


CXXXApp 源文件构造函数中添加:

Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);


CXXXApp 源文件析构函数中添加:

Gdiplus::GdiplusShutdown(m_gdiplusToken);

整个工程都可以使用GDI++了,不需要每个类中都添加一遍


画png资源:

BOOL ImageFromIDResource(UINT nID, LPCTSTR sTR, Gdiplus::Image *&pImg) {
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nID), sTR); // type
if(!hRsrc) return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if(!lpRsrc) return FALSE;
// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, lpRsrc, len);
GlobalUnlock(m_hMem);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
// load from stream
pImg = Gdiplus::Image::FromStream(pstm);
// free/release stuff
pstm->Release();
FreeResource(lpRsrc);
GlobalFree(m_hMem);
return TRUE;
}


绘制中添加:

1.画png

Button, Picture Control 改为 Owner Draw 类型,

Button 不能实现半透效果,但是能显示图形,可以使用 SolidBrush 来填充背景,实现假透明

Picture 可以实现半透,但是,默认不响应鼠标点击事件,可以添加 PretranslateMessage 或者 OnLButtonDown 之类的方法来处理点击事件

点击事件中添加

CPoint ptCursor;
GetCursorPos(&ptCursor);
CRect rc;
GetDlgItem(IDC_???)->GetWindowRect(&rc);
if(rc.PtInRect(ptCursor)) {
TRACE(L"LButtonDown\n");
}


OnDrawItem 中添加

CRect rect = lpDrawItemStruct->rcItem;
Graphics g(lpDrawItemStruct->hDC);
Image *pimage; // Construct an image
ImageFromIDResource(IDB_PNG_CLOSE, _T("PNG"), pimage);
g.DrawImage(pimage, 0, 0, rect.Width(), rect.Height());


2.图形或文字

Gdiplus::SolidBrush brush(0);
Graphics g(lpDrawItemStruct->hDC);
Gdiplus::GraphicsPath path;
g.SetSmoothingMode(SmoothingModeAntiAlias);


brush.SetColor(Gdiplus::Color(0xff, 0xff, 0x00, 0x00));//背景圆
path.Reset();
path.AddPie(0, 0, 20, 20, 0, 360);
g.FillPath(&brush, &path);

brush.SetColor(Gdiplus::Color(0x00, 0xbc, 0xd4));
Gdiplus::FontFamily fontFamily(L"Arial");
Gdiplus::Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
StringFormat format;//设置水平垂直居中
format.SetAlignment(StringAlignmentCenter);
format.SetLineAlignment(StringAlignmentCenter);
Gdiplus::PointF origin(20, 20);
g.DrawString(L"hao", -1, &font, origin, &format, &brush);

Visual C++6.0使用GDI+的一般方法 1. 载解压GDI+开发包; 2. 正确设置include & lib 目录; 3. stdafx.h 添加: #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include 4. 程序中添加GDI+的包含文件gdiplus.h以及附加的类库gdiplus.lib。 通常gdiplus.h包含文件添加在应用程序的stdafx.h文件中,而gdiplus.lib可用两种进行添加: 第一种是直接在stdafx.h文件中添加下列语句: #pragma comment( lib, "gdiplus.lib" ) 另一种方法是: 在VC.net中添加库文件在:项目菜单->属性->链接器->输入 举个例子: (1)在应用程序项目的应用类中,添加一个成员变量,如下列代码: ULONG_PTR m_gdiplusToken; 其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。 (2)在应用类中添加ExitInstance的重载,并添加下列代码用来关闭GDI+: int CGDITestApp::ExitInstance() { Gdiplus::GdiplusShutdown(m_gdiplusToken); return CWinApp::ExitInstance(); } (3)在应用类的InitInstance函数中添加GDI+的初始化代码: 注意:下面这些GDI+的初始化代码必须放在m_pMainWnd->UpdateWindow();之前。 CWinApp::InitInstance(); Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); (4)在需要绘图的窗口或视图类中添加GDI+的绘制代码。 下面分别就单文档和基于对话框应用程序为例,说明使用GDI+的一般过程和方法。 1. 在单文档应用程序中使用GDI+ 在上面的过程中,我们就是以一个单文档应用程序Ex_GDIPlus作为示例的。下面列出第4步所涉及的代码: void CGDITestView::OnDraw(CDC* pDC) { CGDITestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here usingnamespace Gdiplus; Graphics graphics(pDC->m_hDC); Pen newPen(Color(255,0,0),3); HatchBrush newBrush(HatchStyleCross,Color(255,0,255,0),Color(255,0,0,255));//创建一个填充画刷,前景色为绿色,背景色为蓝色 graphics.DrawRectangle(&newPen,50,50,100,60);// 在(50,50)处绘制一个长为100,高为60的矩形 graphics.FillRectangle(&newBrush,50,50,100,60); // 在(50,50)处填充一个长为100,高为60的矩形区域 } 编译并运行,结果如图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值