gpt4 book ai didi

delphi - 在鼠标位置绘制组件时闪烁

转载 作者:行者123 更新时间:2023-12-03 18:35:12 28 4
gpt4 key购买 nike

我正在尝试在光标的 X 位置绘制一条垂直线,该垂直线将随鼠标移动。这条线必须绘制在我表单上所有组件的“顶部”。为此,我使用了此处提供的一段代码:https://stackoverflow.com/a/4481835 .

这是完整形式的代码:

    unit UDemo;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, AdvSmoothTimeLine, ImgList, StdCtrls, ComCtrls, ExtCtrls,
System.ImageList, Vcl.AppEvnts;

type
TForm235 = class(TForm)
ImageList1: TImageList;
Panel1: TPanel;
DateTimePicker1: TDateTimePicker;
Edit1: TEdit;
Button1: TButton;
ComboBox1: TComboBox;
ApplicationEvents1: TApplicationEvents;
Button2: TButton;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Panel8: TPanel;
Panel9: TPanel;
Panel10: TPanel;
Panel11: TPanel;
Panel12: TPanel;
procedure FormCreate(Sender: TObject);

procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
FSelecting : Boolean;
FSelectRect : TRect;
FFixedLineX : Integer;
FDragLineX : Integer;
FMousePt, FOldPt: TPoint;
procedure WM_PAINT(var Msg: TWmPaint); message WM_PAINT;
public
{ Public declarations }
end;

var
Form235: TForm235;

implementation

{$R *.dfm}


procedure TForm235.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
R: TRect;
Pt: TPoint;
begin
if Msg.message = WM_MOUSEMOVE then begin

// assume no drawing (will test later against the point).
// also, below RedrawWindow will cause an immediate WM_PAINT, this will
// provide a hint to the paint handler to not to draw anything yet.
FMousePt := Point(-1, -1);


// first, if there's already a previous rectangle, invalidate it to clear
if (FOldPt.X > 0) and (FOldPt.Y > 0) then begin
R := Rect(FOldPt.X -1, 0, FOldPt.X + 1, self.Height);
InvalidateRect(Handle, @R, True);

// invalidate childs
// the pointer could be on one window yet parts of the rectangle could be
// on a child or/and a parent, better let Windows handle it all
RedrawWindow(Handle, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
end;


// is the message window our form?
if Msg.hwnd = Handle then
// then save the bottom-right coordinates
FMousePt := SmallPointToPoint(TSmallPoint(Msg.lParam))
else begin
// is the message window one of our child windows?
if GetAncestor(Msg.hwnd, GA_ROOT) = Handle then begin
// then convert to form's client coordinates
Pt := SmallPointToPoint(TSmallPoint(Msg.lParam));
windows.ClientToScreen(Msg.hwnd, Pt);
FMousePt := ScreenToClient(Pt);
end;
end;

// will we draw? (test against the point)
if PtInRect(ClientRect, FMousePt) then begin
R := Rect(FMousePt.X - 1, 0, FMousePt.X +1, self.Height);
InvalidateRect(Handle, @R, False);
end;
end;
end;

procedure TForm235.WM_PAINT(var Msg: TWmPaint);
var
DC: HDC;
Rgn: HRGN;
begin
inherited;

if (FMousePt.X > 0) and (FMousePt.Y > 0) then begin
// save where we draw, we'll need to erase before we draw an other one
FOldPt := FMousePt;

// get a dc that could draw on child windows
DC := GetDCEx(Handle, 0, DCX_PARENTCLIP);

// don't draw on borders & caption
Rgn := CreateRectRgn(ClientRect.Left, ClientRect.Top,
ClientRect.Right, ClientRect.Bottom);
SelectClipRgn(DC, Rgn);
DeleteObject(Rgn);

// draw a red rectangle
SelectObject(DC, GetStockObject(DC_BRUSH));
SetDCBrushColor(DC, ColorToRGB(clBlack));
FillRect(DC, Rect(FMousePt.X - 1, 0, FMousePt.X +1, self.Height ), 0);

ReleaseDC(Handle, DC);
end;
end;




procedure TForm235.FormCreate(Sender: TObject);
begin
FSelectRect := TRect.Create(TPoint.Create(self.Left, self.Top));
end;


procedure TForm235.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
FSelectRect.Bottom := self.Height;
FSelectRect.Right := X;
FDragLineX := X;

self.Repaint;

end;

end.

它就像我想要的那样工作,除了一件事。当您左右移动鼠标(以及改变 X 位置)时,这条线会在屏幕上不断地被绘制和不被绘制而闪烁。当移动相对较快时,您还可以注意到“滞后”光标的行。

有谁知道如何改善这种视觉效果?另一种技术/算法?某处的专用组件?

最佳答案

绘画是低优先级的,只有在消息队列清空后才会调度 WM_PAINT。尽管已发布,但输入消息具有更高的优先级。因此,您观察到的滞后是正常行为。

如果你想避免这种情况,你应该放弃无效,而是在你想要的时候画你想要的东西。当然,删除也将是您的责任。为此,一种方法是在没有任何绘图的情况下捕获图像,然后在您想要删除时将其粘贴。表单上的按钮和类似控件可以改变它们的外观,这几乎是不可能的。另一种方法可能是跟踪子区域,大子控件将删除线的位置,然后让它们自己绘制而不等待绘制周期。我希望这会很复杂。此外,您的所有应用程序的性能都会受到影响。您可能稍后会问,“为什么我的鼠标指针会卡顿?”。

使用以下版本进行测试。它不是在鼠标移动时使矩形无效,而是直接绘制一个矩形。这意味着,对于每个鼠标移动通知,都会绘制一条线,而不是在可以合并绘制消息的问题中的版本。子控件的无效仍然留给系统,并且值得注意的是,仍然可以观察到滞后行为,尤其是在编辑控件上。我不知道有什么解决办法。除此之外,性能对我的期望的不利影响较小。

当我尝试编译您的测试用例时,我注意到一件事,流畅行为最明显的障碍是您自己添加到代码中,即 Repaint来电OnMouseMove .你必须删除它,我不知道你为什么认为你需要它。

procedure TForm235.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
R: TRect;
Pt: TPoint;
DC: HDC;
Rgn: HRGN;
begin
if Msg.message = WM_MOUSEMOVE then begin
FMousePt := Point(-1, -1);
if (FOldPt.X > 0) and (FOldPt.Y > 0) then begin
R := Rect(FOldPt.X -1, 0, FOldPt.X + 1, self.Height);
InvalidateRect(Handle, @R, True);
RedrawWindow(Handle, @R, 0, RDW_INVALIDATE or RDW_UPDATENOW or RDW_ALLCHILDREN);
end;
if Msg.hwnd = Handle then
FMousePt := SmallPointToPoint(TSmallPoint(Msg.lParam))
else begin
if GetAncestor(Msg.hwnd, GA_ROOT) = Handle then begin
Pt := SmallPointToPoint(TSmallPoint(Msg.lParam));
winapi.windows.ClientToScreen(Msg.hwnd, Pt);
FMousePt := ScreenToClient(Pt);
end;
end;
if PtInRect(ClientRect, FMousePt) then begin
R := Rect(FMousePt.X - 1, 0, FMousePt.X +1, self.Height);
FOldPt := FMousePt;
DC := GetDCEx(Handle, 0, DCX_PARENTCLIP);
Rgn := CreateRectRgn(ClientRect.Left, ClientRect.Top,
ClientRect.Right, ClientRect.Bottom);
SelectClipRgn(DC, Rgn);
DeleteObject(Rgn);
SelectObject(DC, GetStockObject(DC_BRUSH));
SetDCBrushColor(DC, ColorToRGB(clBlack));
FillRect(DC, Rect(FMousePt.X - 1, 0, FMousePt.X +1, self.Height ), 0);
ReleaseDC(Handle, DC);
end;
end;
end;

procedure TForm235.WMPaint(var Message: TWMPaint);
begin
inherited;
end;

关于delphi - 在鼠标位置绘制组件时闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242064/

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