在 Delphi 2007 中,在鼠标移动事件中,我尝试使用以下方法更改鼠标光标:
procedure TFr_Board_Display.PaintBox_Proxy_BoardMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
if left_mouse_button_down then begin
if some_condition then begin
Cursor := crDrag;
end
else begin
Cursor := crNoDrop;
end;
end
else begin
if some_other_condition then begin
Cursor := crHandPoint;
end
else begin
Cursor := crDefault;
end;
end;
end;
例如。但是,当按下鼠标左键并移动鼠标时,光标不会更改为 crDrag 或 crNoDrop。代码被执行(例如 Cursor := crDrag;),但光标没有改变。当鼠标左键抬起时,我移动鼠标,光标变化没有问题。
(我最初尝试使用一些拖放事件和属性,但无法让所有内容按照我想要的方式工作。)
编辑:澄清所需的行为和格式化代码。
编辑:谢谢你,Gamecat,但我希望当鼠标左键按下时光标会发生变化,并且当鼠标移动时光标应该在 crDrag 和 crNoDrop 之间来回变化。
最佳答案
如果您在 OnMouseDown 中设置鼠标光标并在 OnMouseUp 中重置它,则一切正常:
procedure TForm4.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Cursor := crCross;
end;
procedure TForm4.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Cursor := crDefault; // Or you can restore a saved cursor.
end;
如果您希望鼠标光标在鼠标移动时使用react,请使用以下命令:
procedure TForm4.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then begin
if X<100 then
Screen.Cursor := crCross
else
Screen.Cursor := crHourGlass;
end else
Screen.Cursor := crDefault; // Or you can restore a saved cursor.
end;
procedure TForm4.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Screen.Cursor := crDefault; // Or you can restore a saved cursor.
end;
需要 MouseUp,否则如果光标悬停在控件上方,则不会变回来。
请务必在任何地方使用 Screen.Cursor。
关于delphi - 按下鼠标左键时如何更改鼠标光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/377105/