我正在尝试在光标的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位置)时,线条会不断地从屏幕上绘制和拉开。当移动速度相对较快时,您还会注意到光线“滞后”的行。
有没有人知道如何改善这种视觉效果?另一种技术/算法?某个专用组件?
答案 0 :(得分:2)
绘制是低优先级,仅在清空消息队列后才调度WM_PAINT。虽然已发布,但输入消息的优先级更高。因此,您观察到的滞后是正常行为。
如果你想避免它,你应该放弃无效,而是在你想要的时候画出你想要的东西。当然,擦除也是你的责任。为此,一种方法是在没有任何绘图的情况下捕获图像,然后在您想要擦除时粘贴它。使用表格上的按钮和类似控件可以改变它们的外观,这几乎是不可能的。另一种方法是跟踪儿童,大孩子控制区域将被移除的区域,然后让他们自己画画而不等待油漆循环。我希望这很复杂。此外,您的所有应用程序的性能都会受到影响。您可能稍后会问,“为什么我的鼠标指针会断断续续?”。
使用以下版本进行测试。它不是在移动鼠标时使矩形无效,而是直接绘制一个矩形。这意味着,对于每个鼠标移动通知,都会绘制一条线,而不是可以合并绘制消息的问题中的版本。子控件的失效仍然留给系统,显然,仍然可以观察 lag 行为,特别是在编辑控件上。我不知道任何修复。除此之外,表现对我的期望的影响较小。
当我尝试编译测试用例时,我注意到的一件事是,顺畅行为最明显的障碍是在代码中添加了一个自己,即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;