gpt4 book ai didi

delphi - 使用 Delphi 在桌面上绘图以强调鼠标点击

转载 作者:行者123 更新时间:2023-12-02 02:11:38 30 4
gpt4 key购买 nike

如何在桌面窗口上绘制以在用户点击时绘制圆形动画...

我已经尝试了下面的代码,启动一个线程来绘制动画...

下面的代码可以工作,但有一些绘制问题:

unit UMouseEmphasizer;

interface

implementation

uses
Classes, Windows, Messages, Graphics, Forms;

type
TEmphasizePointDrawer = class(TThread)
private
fPoint: TPoint;
fCanvas: TCanvas;
protected
procedure Execute; override;
public
constructor Create(pt: TPoint); reintroduce;
destructor Destroy; override;
end;

constructor TEmphasizePointDrawer.Create(pt: TPoint);
begin
fPoint := pt;
fCanvas := TCanvas.Create;
fCanvas.Handle := GetDCEx(0, 0, DCX_PARENTCLIP);
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;

destructor TEmphasizePointDrawer.Destroy;
begin
ReleaseDC(0, fCanvas.Handle);
fCanvas.Free;
inherited;
end;

procedure TEmphasizePointDrawer.Execute;
const
INFLATE_DELTA = 10;
var
i: integer;
r: TRect;
begin
r := rect(0,0,0,0);
with fCanvas do
begin
Brush.Style := bsClear;
Pen.Style := psSolid;
Pen.Color := clRed;
Pen.Width := 2;

for i := 0 to 2 do
begin
r := rect(
fPoint.X - (i * INFLATE_DELTA),
fPoint.Y - (i * INFLATE_DELTA),
fPoint.X + (i * INFLATE_DELTA),
fPoint.Y + (i * INFLATE_DELTA)
);
Ellipse(r);

sleep(100);
end;
end;

InflateRect(r, 2, 2);
RedrawWindow(0, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
end;

function MouseHookHandler(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
pt: TPoint;
begin
Result := 0;
if nCode < 0 then
Exit;

pt := PMouseHookStruct(Data)^.pt;

case MsgID of
WM_LBUTTONUP:
TEmphasizePointDrawer.Create(pt);
end;
end;

var
gHook: HHOOK=0;

procedure HookMouse; stdcall;
begin
gHook := SetWindowsHookEx(WH_MOUSE, MouseHookHandler, HINSTANCE, 0);
end;

procedure UnhookMouse;
begin
UnhookWindowsHookEx(gHook);
gHook := 0;
end;

initialization
HookMouse;

finalization
UnhookMouse;

end.

最佳答案

我解决了这个问题:

procedure TEmphasizePointDrawer.Execute;
const
INFLATE_DELTA = 5;
COUNT = 3;
BORDER = 2;
var
i: integer;
r: TRect;
begin
with fCanvas do
begin
Brush.Style := bsClear;
Pen.Style := psSolid;
Pen.Color := clRed;
Pen.Width := BORDER;

for i := COUNT downto 0 do
begin
if i < COUNT then
begin
InflateRect(r, BORDER, BORDER);
RedrawWindow(0, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
sleep(0);
BitBlt(Handle, r.Left, r.Top, (r.Right - r.Left), (r.Bottom - r.Top), Handle, r.Left, r.Top, SRCCOPY);
end;

r := rect(
fPoint.X - (i * INFLATE_DELTA),
fPoint.Y - (i * INFLATE_DELTA),
fPoint.X + (i * INFLATE_DELTA),
fPoint.Y + (i * INFLATE_DELTA)
);

InflateRect(r, BORDER, BORDER);
RedrawWindow(0, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
sleep(0);
BitBlt(Handle, r.Left, r.Top, (r.Right - r.Left), (r.Bottom - r.Top), Handle, r.Left, r.Top, SRCCOPY);

InflateRect(r, -BORDER, -BORDER);
Ellipse(r);

sleep(50);
end;
end;

r := rect(
fPoint.X - (COUNT * INFLATE_DELTA) - BORDER,
fPoint.Y - (COUNT * INFLATE_DELTA) - BORDER,
fPoint.X + (COUNT * INFLATE_DELTA) + BORDER,
fPoint.Y + (COUNT * INFLATE_DELTA) + BORDER
);
RedrawWindow(0, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
end;

function MouseHookHandler(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
pt: TPoint;
begin
// draw only when over my application forms!!!
if (nCode < 0) or (FindControl(GetForegroundWindow()) = nil) then
begin
Result := CallNextHookEx(gHook, nCode, MsgID, Data);
Exit;
end;

pt := PMouseHookStruct(Data)^.pt;

case MsgID of
WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP:
TEmphasizePointDrawer.Create(pt);

end;
Result := 0;
end;

感谢您的回复!

关于delphi - 使用 Delphi 在桌面上绘图以强调鼠标点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12782290/

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