gpt4 book ai didi

delphi - MouseDown 时光标没有变化

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

我有一个包含 2 个事件的 TPanel:

procedure TForm1.Panel1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Panel1.Cursor := crSizeAll;
end;

procedure TForm1.Panel1MouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Panel1.Cursor := crDefault;
end;

当我单击面板(MouseDown 事件)时,光标不会更改为 crSizeAll。我做错了什么以及如何解决这个问题?

最佳答案

你做得很好。只需一个简单的技巧即可捕获鼠标输入。一旦您在 TPanel 上按下鼠标左键 (LMB),该面板就会取消鼠标操作。

之后,您的代码 Panel1.Cursor := crSizeAll; 开始运行。正如您在 VCL 源代码中看到的,TControl 向自身发送消息 CM_CURSORCHANGED 以设置所需的光标类型。由于TWinControl 基于TControl,因此它处理此消息并检查是否已捕获鼠标输入。如果不是这样,则 TWinControl 向自身发送消息 WM_SETCURSOR 以设置新光标。

但是微软明确表示消息WM_SETCURSOR有一点限制:

Sent to a window if the mouse causes the cursor to move within a window and mouse input is not captured.

因此,如果捕获了鼠标输入,则无法更改光标的类型。您可以做的一件事是调用 ReleaseCapture:

procedure TForm1.Panel1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
Panel1.Cursor := crSizeAll;
end;

但我不建议这样做,因为它打破了鼠标输入的范例(在我看来,一旦按下 LMB,那么它应该保持按下状态,直到由于重要原因释放(例如单击按钮),但是您可以自行决定如何使用 ReleaseCapture)。

重要!
用户Zig执行的测试证明,使用ReleaseCapture方法是不合适的,因为如果鼠标光标移出TPanel,它会禁止生成OnMouseUp事件 并且 LMB 已发布。

有用信息:

  1. WM_SETCURSOR message
  2. SetCapture function
  3. GetCapture function
  4. ReleaseCapture function

关于delphi - MouseDown 时光标没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53783776/

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