我能找到的所有GDIPlus演示代码都是没有无效绘制的。因此,当使用TImage在TScrollbox上进行MouseMove绘制时,如何在GDIPlus API中无效化一个矩形?
这是它的外观:
使用Delphi 2010和http://www.progdigy.com中的GDIPlus库。
function NormalizeRect ( R: TRect ): TRect;
begin
// This routine normalizes a rectangle. It makes sure that the Left,Top
// coords are always above and to the left of the Bottom,Right coords.
with R do
begin
if Left > Right then
if Top > Bottom then
Result := Rect ( Right, Bottom, Left, Top )
else
Result := Rect ( Right, Top, Left, Bottom )
else if Top > Bottom then
Result := Rect ( Left, Bottom, Right, Top )
else
Result := Rect ( Left, Top, Right, Bottom );
end;
end;
procedure TFormMain.Image1MouseDown ( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
if Line1.Down then
begin
GPPointStart := MakePoint ( X, Y );
end;
end;
procedure TFormMain.Image1MouseMove ( Sender: TObject; Shift: TShiftState; X, Y: Integer );
var
graphics: TGPGraphics;
pen: TGPPen;
SolidBrush: TGPSolidBrush;
rgbTriple: windows.RGBTRIPLE;
iRect: TRect;
begin
if Line1.Down then
begin
if ssLeft in Shift then
begin
iRect := NormalizeRect ( Rect ( X, Y, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height ) );
InvalidateRect ( ScrollBox1.Handle, @iRect, TRUE );
graphics := TGPGraphics.Create ( Image1.Picture.Bitmap.Canvas.Handle );
graphics.Flush ( FlushIntentionFlush );
GPPointEnd := MakePoint ( X, Y );
rgbTriple := ColorToRGBTriple ( ColorBox1.Selected );
pen := TGPPen.Create ( MakeColor ( StrToInt ( Alpha1.Text ), rgbTriple.rgbtRed, rgbTriple.rgbtGreen, rgbTriple.rgbtBlue )
);
pen.SetWidth ( StrToInt ( Size1.Text ) );
graphics.DrawLine ( pen, GPPointStart.X, GPPointStart.Y, GPPointEnd.X, GPPointEnd.Y );
graphics.Free;
Image1.Refresh;
end;
end;
end;
这是它的外观:
![在此输入图片描述](https://istack.dev59.com/3MxXc.webp)
TScrollBox
没有画布可以绘制。考虑到内容的滚动,这甚至不是一个好主意。我猜你是想说不要绘制到图像上,而是绘制到位图上,对吗? - TLama