我有一個滾動條,滑鼠事件分配給 onChange、onMouseWheel 和 onm ouseUp。onChange 和 wheel 事件作業正常,但 onm ouseUp 事件不會觸發。在除錯時深入到 TControl 方法,我注意到事件變數 (FOnMouseUp) 為零。事件是在IDE中分配的,我把它放在表單的onCreate事件中,另外我在創建表單后嘗試在其他各個地方分配它,但無濟于事。是什么賦予了?
這是一個簡單的可重現示例,其中所有三個滾動條滑鼠事件都不會觸發:
`TForm4 = class(TForm)
ScrollBar1: TScrollBar;
Label1: TLabel;
procedure ScrollBar1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure ScrollBar1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ScrollBar1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ScrollBar1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.fmx}
procedure TForm4.ScrollBar1Change(Sender: TObject);
begin
Label1.Text := 'onChange: ' Screen.MousePos.Y.ToString;
end;
procedure TForm4.ScrollBar1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
Label1.Text := 'mousedown: ' Y.ToString;
end;
procedure TForm4.ScrollBar1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
begin
Label1.Text := 'mousemove: ' Y.ToString;
end;
procedure TForm4.ScrollBar1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
Label1.Text := 'mouseUP: ' Y.ToString;
end;
end.`
和 .FMX:
`object Form4: TForm4
Left = 0
Top = 0
Caption = 'Form4'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
DesignerMasterStyle = 0
object ScrollBar1: TScrollBar
SmallChange = 0.000000000000000000
Orientation = Vertical
Position.X = 616.000000000000000000
Position.Y = 8.000000000000000000
Size.Width = 18.000000000000000000
Size.Height = 449.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
OnChange = ScrollBar1Change
onm ouseDown = ScrollBar1MouseDown
onm ouseMove = ScrollBar1MouseMove
onm ouseUp = ScrollBar1MouseUp
end
object Label1: TLabel
Position.X = 568.000000000000000000
Position.Y = 152.000000000000000000
Text = 'Label1'
TabOrder = 1
end
end`
uj5u.com熱心網友回復:
原因是滾動條包含子物件,例如軌道、拇指和最小和最大按鈕。回應滑鼠事件的是這些物件,而不是父物件。所以解決方案是將滑鼠事件設定為這些物件。問題是這些物件是受保護的,因此您必須創建一個新的滾動條類來設定這些事件。TScrollBar 建構式中尚不存在子物件,因此我發現分配它們的最佳位置是在第一個繪制事件上。
幾周前我問了幾乎完全相同的問題。在這里查看我自己的答案。
FMX:TScrollBar MouseDown 和 MouseUp 事件未觸發
這是您的示例,現在可以使用了。我還用 4 個標簽替換了您的一個標簽,以便更輕松地查看哪些事件被呼叫。
回應滑鼠事件的新滾動條類:
unit ScrollBarMouse;
interface
uses
System.Classes, System.UITypes, FMX.StdCtrls, FMX.Types;
type
// A scroll bar that responds to mouse events
TScrollBarMouse = class(TScrollBar)
private
FMouseEventsSet : Boolean;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
end;
implementation
constructor TScrollBarMouse.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMouseEventsSet := False;
end;
procedure TScrollBarMouse.Paint;
begin
inherited;
// Track and Buttons are not assigned in constructor, so set mouse events on first paint
if not FMouseEventsSet and Assigned(Track) and Assigned(Track.Thumb)
and Assigned(MinButton) and Assigned(MaxButton) then begin
Track.OnMouseDown := onm ouseDown;
Track.OnMouseUp := onm ouseUp;
Track.OnMouseMove := onm ouseMove;
Track.Thumb.OnMouseDown := onm ouseDown;
Track.Thumb.OnMouseUp := onm ouseUp;
Track.Thumb.OnMouseMove := onm ouseMove;
MinButton.OnMouseDown := onm ouseDown;
MinButton.OnMouseUp := onm ouseUp;
MinButton.OnMouseMove := onm ouseMove;
MaxButton.OnMouseDown := onm ouseDown;
MaxButton.OnMouseUp := onm ouseUp;
MaxButton.OnMouseMove := onm ouseMove;
FMouseEventsSet := True;
end;
end;
end.
表格單位:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, ScrollBarMouse;
type
TForm4 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure ScrollBar1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure ScrollBar1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ScrollBar1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure ScrollBar1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ScrollBar1 : TScrollBarMouse;
end;
var
Form4: TForm4;
implementation
{$R *.fmx}
procedure TForm4.FormCreate(Sender: TObject);
begin
// Create the scroll bar object
ScrollBar1 := TScrollBarMouse.Create(Self);
with ScrollBar1 do begin
Parent := Self;
Orientation := TOrientation.Vertical;
Position.X := 616;
Position.Y := 8;
Size.Width := 18;
Size.Height := 449;
onm ouseDown := ScrollBar1MouseDown;
onm ouseUp := ScrollBar1MouseUp;
onm ouseMove := ScrollBar1MouseMove;
OnChange := ScrollBar1Change;
end;
end;
procedure TForm4.ScrollBar1Change(Sender: TObject);
begin
Label1.Text := 'onChange: ' IntToStr(Round(Screen.MousePos.Y));
end;
procedure TForm4.ScrollBar1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
Label2.Text := 'mousedown: ' IntToStr(Round(Y));
end;
procedure TForm4.ScrollBar1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
begin
Label3.Text := 'mousemove: ' IntToStr(Round(Y));
end;
procedure TForm4.ScrollBar1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
Label4.Text := 'mouseUP: ' IntToStr(Round(Y));
end;
end.
表單(滾動條被洗掉,因為它是在運行時創建的):
object Form4: TForm4
Left = 0
Top = 0
Caption = 'Form4'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 0
object Label1: TLabel
Position.X = 424.000000000000000000
Position.Y = 144.000000000000000000
Size.Width = 121.000000000000000000
Size.Height = 17.000000000000000000
Size.PlatformDefault = False
Text = 'Label1'
TabOrder = 3
end
object Label2: TLabel
Position.X = 424.000000000000000000
Position.Y = 168.000000000000000000
Size.Width = 121.000000000000000000
Size.Height = 17.000000000000000000
Size.PlatformDefault = False
Text = 'Label1'
TabOrder = 2
end
object Label3: TLabel
Position.X = 424.000000000000000000
Position.Y = 192.000000000000000000
Size.Width = 121.000000000000000000
Size.Height = 17.000000000000000000
Size.PlatformDefault = False
Text = 'Label1'
TabOrder = 1
end
object Label4: TLabel
Position.X = 424.000000000000000000
Position.Y = 216.000000000000000000
Size.Width = 121.000000000000000000
Size.Height = 17.000000000000000000
Size.PlatformDefault = False
Text = 'Label1'
TabOrder = 0
end
end
這是使用 Delphi 10.4 構建的,并在 Windows 10 中運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315609.html
上一篇:決議FirebaseJSON陣列
