編寫程序對GDI和GDI+繪制進行了比較,經過比較,GDI相對GDI+還是有一些性能優勢的。
同時比較了每次繪制創建TGPGraphics對象和共用一個TGPGraphics對象的情況,兩者性能相差不大,幾可忽略。
1.用GDI繪制5K次----耗時約為19s200ms
procedure TForm8.WMPaint(var Message: TWMPaint);
var
ps: PAINTSTRUCT;
LClientRect: TGPRect;
LGraph: TGPGraphics;
LBrush: TGPBrush;
LBr: HGDIOBJ;
begin
if m_nCount < 5000 then
begin
//創建緩存
BeginPaint(Handle, ps);
if not Assigned(m_memDC) then
m_memDC := TMemoryDC.Create(ps.hdc);
m_memDC.SetBounds(ps.hdc, Self.ClientRect);
//用GDI+繪制
// LBrush := TGPSolidBrush.Create(aclRed);
// LClientRect := MakeRect(Self.ClientRect);
// m_memDC.Graph.FillRectangle(LBrush, LClientRect);
// LBrush.Free;
//用GDI繪制
LBr := CreateSolidBrush(clRed);
FillRect(m_memDC.DC, Self.ClientRect, LBr);
DeleteObject(LBr);
//緩沖去拷貝
m_memDC.Blt(ps.hdc);
EndPaint(Handle, ps);
Message.Result := 0;
Inc(m_nCount);
end
else
begin
BeginPaint(Handle, ps);
EndPaint(Handle, ps);
Message.Result := 0;
end;
end;
2.用GDI+繪制5K次----耗時約為19s600ms
procedure TForm8.WMPaint(var Message: TWMPaint);
var
ps: PAINTSTRUCT;
LClientRect: TGPRect;
LGraph: TGPGraphics;
LBrush: TGPBrush;
LBr: HGDIOBJ;
begin
if m_nCount < 5000 then
begin
//創建緩存
BeginPaint(Handle, ps);
if not Assigned(m_memDC) then
m_memDC := TMemoryDC.Create(ps.hdc);
m_memDC.SetBounds(ps.hdc, Self.ClientRect);
//用GDI+繪制
LGraph := TGPGraphics.Create(m_memDC.DC);
LBrush := TGPSolidBrush.Create(aclRed);
LClientRect := MakeRect(Self.ClientRect);
LGraph.FillRectangle(LBrush, LClientRect);
LGraph.Free;
LBrush.Free;
//用GDI繪制
// LBr := CreateSolidBrush(clRed);
// FillRect(m_memDC.DC, Self.ClientRect, LBr);
// DeleteObject(LBr);
//緩沖去拷貝
m_memDC.Blt(ps.hdc);
EndPaint(Handle, ps);
Message.Result := 0;
Inc(m_nCount);
end
else
begin
BeginPaint(Handle, ps);
EndPaint(Handle, ps);
Message.Result := 0;
end;
end;
3.用GDI+繪制5K次(不重復創建TGPGraphics)----耗時約為19s500ms
procedure TForm8.WMPaint(var Message: TWMPaint);
var
ps: PAINTSTRUCT;
LClientRect: TGPRect;
LGraph: TGPGraphics;
LBrush: TGPBrush;
LBr: HGDIOBJ;
begin
if m_nCount < 5000 then
begin
//創建緩存
BeginPaint(Handle, ps);
if not Assigned(m_memDC) then
m_memDC := TMemoryDC.Create(ps.hdc);
m_memDC.SetBounds(ps.hdc, Self.ClientRect);
//用GDI+繪制
LBrush := TGPSolidBrush.Create(aclRed);
LClientRect := MakeRect(Self.ClientRect);
m_memDC.Graph.FillRectangle(LBrush, LClientRect);
LBrush.Free;
//用GDI繪制
// LBr := CreateSolidBrush(clRed);
// FillRect(m_memDC.DC, Self.ClientRect, LBr);
// DeleteObject(LBr);
//緩沖去拷貝
m_memDC.Blt(ps.hdc);
EndPaint(Handle, ps);
Message.Result := 0;
Inc(m_nCount);
end
else
begin
BeginPaint(Handle, ps);
EndPaint(Handle, ps);
Message.Result := 0;
end;
end;
