GDI+ DrawLine 什么都不画
GDI+ DrawLine draws nothing
在 Delphi 中开始使用 GDI+。需要画几条平滑的直线。例如试图在表格上画对角线(从左上角到右下角)但什么也没有出现。该代码有什么问题(Delphi XE3,Windows 10 x64)?
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.GDIPAPI, Winapi.GDIPOBJ;
type
TForm2 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormPaint(Sender: TObject);
var
graphics: TGPGraphics;
gpPen: TGPPen;
begin
graphics := TGPGraphics.Create(Self.Canvas.Handle);
try
graphics.SetSmoothingMode(SmoothingModeAntiAlias8x8);
gpPen := TGPPen.Create(clBlue, 3);
try
graphics.DrawLine(gpPen, 0, 0, ClientWidth, ClientHeight);
finally
gpPen.Free;
end;
finally
graphics.Free;
end;
end;
procedure TForm2.FormResize(Sender: TObject);
begin
Repaint;
end;
end.
问题是颜色。
这是 GDI+ colour, which is not the same thing as a TColor
which is essentially a Win32 COLORREF
。
您传递了 clBlue = [=16=]FF0000
,现在(错误)解释为(alpha、红色、绿色、蓝色)= ($00, $FF, $00, $00)。由于alpha值为0,线条完全透明
如果你这样做
gpPen := TGPPen.Create(clBlue or $FF000000, 3);
相反,您得到完全不透明。但是你得到的是红色,而不是蓝色,因为 TColor
是 [=18=]BBGGRR
而不是 [=19=]RRGGBB
。所以如果你这样做
gpPen := TGPPen.Create($FF0000FF, 3);
你得到了你想要的蓝色。
也许更好的方法是使用 MakeColor
函数:
gpPen := TGPPen.Create(MakeColor(0, 0, $FF), 3)
或ColorRefToARGB
:
gpPen := TGPPen.Create(ColorRefToARGB(clBlue), 3)
在 Delphi 中开始使用 GDI+。需要画几条平滑的直线。例如试图在表格上画对角线(从左上角到右下角)但什么也没有出现。该代码有什么问题(Delphi XE3,Windows 10 x64)?
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.GDIPAPI, Winapi.GDIPOBJ;
type
TForm2 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormPaint(Sender: TObject);
var
graphics: TGPGraphics;
gpPen: TGPPen;
begin
graphics := TGPGraphics.Create(Self.Canvas.Handle);
try
graphics.SetSmoothingMode(SmoothingModeAntiAlias8x8);
gpPen := TGPPen.Create(clBlue, 3);
try
graphics.DrawLine(gpPen, 0, 0, ClientWidth, ClientHeight);
finally
gpPen.Free;
end;
finally
graphics.Free;
end;
end;
procedure TForm2.FormResize(Sender: TObject);
begin
Repaint;
end;
end.
问题是颜色。
这是 GDI+ colour, which is not the same thing as a TColor
which is essentially a Win32 COLORREF
。
您传递了 clBlue = [=16=]FF0000
,现在(错误)解释为(alpha、红色、绿色、蓝色)= ($00, $FF, $00, $00)。由于alpha值为0,线条完全透明
如果你这样做
gpPen := TGPPen.Create(clBlue or $FF000000, 3);
相反,您得到完全不透明。但是你得到的是红色,而不是蓝色,因为 TColor
是 [=18=]BBGGRR
而不是 [=19=]RRGGBB
。所以如果你这样做
gpPen := TGPPen.Create($FF0000FF, 3);
你得到了你想要的蓝色。
也许更好的方法是使用 MakeColor
函数:
gpPen := TGPPen.Create(MakeColor(0, 0, $FF), 3)
或ColorRefToARGB
:
gpPen := TGPPen.Create(ColorRefToARGB(clBlue), 3)