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;