gpt4 book ai didi

delphi - 如何及时重新绘制 Canvas ?

转载 作者:行者123 更新时间:2023-12-02 13:35:35 24 4
gpt4 key购买 nike

问题是:我在桌面上绘制一些矩形,当鼠标移动时(矩形大小增加)我没有滞后、伪影等,一切都很好: enter image description here

但是当我将矩形大小调整为低于原来的大小时,我得到了以下结果: enter image description here

红色矩形是真正的矩形,所有其他都是错误。

完美的解决方案是重新绘制 Canvas ,但我不能在鼠标移动时一直这样做。

鼠标移动后绝对停止时是否有解决方案?

更新

代码:

    unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;

type
TForm2 = class(TForm)
Timer1: TTimer;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
isDown: Boolean;
downX, downY: Integer;
public
{ Public declarations }
Bild: TBitMap;
end;

implementation

{表单 Prop :边框style= bsNoneAlphaBlend 真实,150透明颜色 = true, clBlack}

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
Bild := TBitMap.Create;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
Bild.Free;
end;

procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
isDown := true;
downX := X;
downY := Y;
end;

procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
const
cVal = 4;
begin
if isDown then
begin
Self.Canvas.Lock;
Self.Repaint;
Self.Canvas.Pen.Color := clNone;
Self.Canvas.Pen.Width := 1;

Self.Canvas.Pen.Style := psDot;
//Self.Canvas.Pen.Mode := pmNotCopy;
Self.Canvas.Brush.Color := clGreen;
Self.Canvas.Rectangle(downX, downY, X, Y);
Self.Canvas.Pen.Style := psSolid;
Self.Canvas.Brush.Color := clNone;
Self.Canvas.Unlock;
{ Self.Canvas.Rectangle(downX - cVal, downY - cVal, downX + cVal, downY + cVal);
Self.Canvas.Rectangle(X - cVal, Y - cVal, X + cVal, Y + cVal);
Self.Canvas.Rectangle(X - cVal, downY - cVal, X + cVal, downY + cVal);
Self.Canvas.Rectangle(downX - cVal, Y - cVal, downX + cVal, Y + cVal);

Self.Canvas.Rectangle(downX - cVal, (downY + Y) div 2 - cVal, downX + cVal,
(downY + Y) div 2 + cVal);
Self.Canvas.Rectangle(X - cVal, (downY + Y) div 2 - cVal, X + cVal,
(downY + Y) div 2 + cVal);

Self.Canvas.Rectangle((downX + X) div 2 - cVal, downY - cVal,
(downX + X) div 2 + cVal, downY + cVal);
Self.Canvas.Rectangle((downX + X) div 2 - cVal, Y - cVal, (downX + X) div 2 + cVal,
Y + cVal); }
end;
end;

function CaptureRect(aRect: TRect; out aBmp: TBitmap): Boolean;
var
ScreenDC: HDC;
begin
Result := False;
try
with aBmp, aRect do
begin
Width := Right - Left;
Height := Bottom - Top;
ScreenDC := GetDC(0);
try
BitBlt(Canvas.Handle, 0, 0, Width, Height, ScreenDC, Left, Top, SRCCOPY);
finally
ReleaseDC(0, ScreenDC);
end;
end;
except
end;
end;

procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
r: TRect;
begin
isDown := false;
r.Left := downX;
r.Top := downY;
r.Right := X;
r.Bottom := Y;
CaptureRect(r, Bild);
Self.Close;
end;

end.

最佳答案

你的问题是你画错了地方。在 OnMouseMove 事件处理程序中停止绘画。将绘画代码移至绘画处理程序。例如表单的 OnPaint 处理程序。

然后,在 OnMouseMove 事件处理程序以及 OnMouseDownOnMouseUp 中,调用表单上的 Invalidate ,或 Win32 InvalidateRect 函数,强制执行绘制周期。

关于delphi - 如何及时重新绘制 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com