GDI+ 常用的

Graphics类是GDI+的基础类,


构造函数:传递HDC的句柄、窗口句柄m_hWnd


画圆形

DrawEllipse

矩形

DrawRectangle

饼图

DrawPie

画图

Image img(L"D:\\1.jpg");
graphics.DrawImage(&img,0,0,rc.right-rc.left,rc.bottom - rc.top);

写字

Gdiplus::Font font(L"黑体",64,Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);  

RectF rectf;
Status status = graphics.MeasureString(sTitle.c_str(),-1,&font,start,&rectf);
if (status == S_OK)
{

StringFormat sf;// =  new StringFormat();
sf.SetTrimming(StringTrimmingEllipsisPath);
graphics.DrawString(sTitle.c_str(),sTitle.length(),&font,rectf,&sf,&hatchBrush);

}


填充矩形、饼图、圆形

FillRectangle \ FillEllipse \ FillPie



使用内存DC防止闪烁

步骤(1)

case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd,&rc);


int saveDcId  = SaveDC(hdc);


HDC hMemDc = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom - rc.top);
SelectObject(hMemDc,hBitmap);


Graphics graphics(hMemDc);

。。。。。。。。。。。。。。。。。

BitBlt(hdc,0,0,rc.right-rc.left,rc.bottom - rc.top,hMemDc,0,0,SRCCOPY);
RestoreDC(hdc,saveDcId);
DeleteObject(hBitmap);
DeleteObject(hMemDc);


EndPaint(hWnd, &ps);
}

步骤(2)

WM_ERASEBKGND 消息直接return 0 ;



代码:

case WM_PAINT:
		{
			hdc = BeginPaint(hWnd, &ps);
			RECT rc;
			GetClientRect(hWnd,&rc);

			int saveDcId  = SaveDC(hdc);

			HDC hMemDc = CreateCompatibleDC(hdc);
			HBITMAP hBitmap = CreateCompatibleBitmap(hdc,rc.right-rc.left,rc.bottom - rc.top);
			SelectObject(hMemDc,hBitmap);

			Graphics graphics(hMemDc);		

			Image img(L"D:\\1.jpg");
			graphics.DrawImage(&img,0,0,rc.right-rc.left,rc.bottom - rc.top);

			Pen pen (Color(100,255,0,255));
			graphics.DrawLine(&pen,100,100,300,100);

			graphics.DrawRectangle(&pen,200,2,50,50);

			SolidBrush solidBrush(Color(255,0,25,255));
			graphics.FillRectangle(&solidBrush,300,200,60,60);

			HatchBrush hatchBrush(HatchStyleDarkHorizontal,Color(100,0,255,0),Color(100,255,0,0));
			graphics.FillEllipse(&hatchBrush,300,300,80,80);


			Point pa(0,0);
			Point pb(((rc.right-rc.left)/2),((rc.bottom - rc.top)));
			LinearGradientBrush linearGradientBrush(pa,pb,Color(100,0,255,0),Color(100,255,0,0));
			graphics.FillRectangle(&linearGradientBrush,0,0,(rc.right-rc.left)/2,(rc.bottom - rc.top));

			Point pa1((rc.right-rc.left)/2,0);
			Point pb2((rc.right-rc.left),rc.bottom - rc.top);
			LinearGradientBrush linearGradientBrush1(pa1,pb2,Color(100,255,0,0),Color(100,0,255,0));
			graphics.FillRectangle(&linearGradientBrush1,(rc.right-rc.left)/2,0,(rc.right-rc.left)/2,(rc.bottom - rc.top));



			wstring sTitle = L"打算打算";

			PointF start(0.0,0.0);
			
			

			Gdiplus::Font font(L"黑体",64,Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);  

			RectF rectf;
			Status status = graphics.MeasureString(sTitle.c_str(),-1,&font,start,&rectf);
			if (status == S_OK)
			{

				StringFormat sf;// =  new StringFormat();
				sf.SetTrimming(StringTrimmingEllipsisPath);
				
				graphics.DrawString(sTitle.c_str(),sTitle.length(),&font,rectf,&sf,&hatchBrush);

			}

			BitBlt(hdc,0,0,rc.right-rc.left,rc.bottom - rc.top,hMemDc,0,0,SRCCOPY);
			RestoreDC(hdc,saveDcId);
			DeleteObject(hBitmap);
			DeleteObject(hMemDc);

			EndPaint(hWnd, &ps);
		}
		
		break;
	case WM_ERASEBKGND:
		{
			return 0;
		}
		break;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值