右键单击不会更新 TControlList 中的 ItemIndex
Right-click doesn't update ItemIndex in TControlList
我已将 TPopupMenu
添加到 TControlList
,右键单击时 ItemIndex 未更新以反映所单击的项目。有没有一种方法可以使右键单击以与左键单击类似的方式响应?
如果用户右键单击特定项目,PopupMenu 将与该项目相关联,而不是与当前获得焦点的项目相关联。
您可以使用 OnPopupMenu 事件中的 HotItemIndex 属性 并将其保存到变量中。然后对于弹出菜单项事件你可以使用它。
示例代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ControlList, Vcl.StdCtrls;
type
TForm1 = class(TForm)
ControlList1: TControlList;
PopupMenu1: TPopupMenu;
TestPopupMnu: TMenuItem;
Label1: TLabel;
Memo1: TMemo;
procedure ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas; ARect:
TRect; AState: TOwnerDrawState);
procedure ControlList1Click(Sender: TObject);
procedure TestPopupMnuClick(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
private
FPopupItemIndex : Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas;
ARect: TRect; AState: TOwnerDrawState);
begin
Label1.Caption := AIndex.ToString;
end;
procedure TForm1.ControlList1Click(Sender: TObject);
begin
Memo1.Lines.Add('Clicked item ' + ControlList1.ItemIndex.ToString);
end;
procedure TForm1.TestPopupMnuClick(Sender: TObject);
begin
Memo1.Lines.Add('Test PopupMenu item ' + FPopupItemIndex.ToString);
end;
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
FPopupItemIndex := ControlList1.HotItemIndex;
end;
end.
表单本身:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 299
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ControlList1: TControlList
Left = 24
Top = 20
Width = 317
Height = 200
ItemCount = 5
ItemMargins.Left = 0
ItemMargins.Top = 0
ItemMargins.Right = 0
ItemMargins.Bottom = 0
ParentColor = False
PopupMenu = PopupMenu1
TabOrder = 0
OnBeforeDrawItem = ControlList1BeforeDrawItem
OnClick = ControlList1Click
object Label1: TLabel
Left = 92
Top = 20
Width = 31
Height = 13
Caption = 'Label1'
end
end
object Memo1: TMemo
Left = 368
Top = 24
Width = 241
Height = 201
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object PopupMenu1: TPopupMenu
OnPopup = PopupMenu1Popup
Left = 436
Top = 128
object TestPopupMnu: TMenuItem
Caption = 'Test'
OnClick = TestPopupMnuClick
end
end
end
为了处理右键单击,您必须使用 OnMouseDown
或 OnMouseUp
事件而不是 OnClick
事件。
与仅检测左键单击的 OnClick
事件不同 OnMouseDown
和 OnMouseUp
事件能够检测所有鼠标点击(左、右、中)。
procedure TForm1.ControlList1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
begin
//Do what needs to be done on right click
end
else if Button = mbMiddle then
begin
//Do what needs to be done on middle click
end
end;
HotItemIndex
属性 可以通过使用以下
来检测哪个项目被右键单击
procedure TListingList.clListingsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if Button <> mbRight then
Exit;
if clListings.HotItemIndex <> -1 then
clListings.ItemIndex := clListings.HotItemIndex;
end;
这主要是可行的,但是如果您已经有一个 TPopupmenu
可见,那么 HotItemIndex
就是 -1
。这意味着连续右键单击不会弹出正确的项目 - 但我可以接受。我认为需要 MousePosToItemIndex
或 ItemIndexUnderMouse
方法才能正确解决此问题。
我已将 TPopupMenu
添加到 TControlList
,右键单击时 ItemIndex 未更新以反映所单击的项目。有没有一种方法可以使右键单击以与左键单击类似的方式响应?
如果用户右键单击特定项目,PopupMenu 将与该项目相关联,而不是与当前获得焦点的项目相关联。
您可以使用 OnPopupMenu 事件中的 HotItemIndex 属性 并将其保存到变量中。然后对于弹出菜单项事件你可以使用它。
示例代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ControlList, Vcl.StdCtrls;
type
TForm1 = class(TForm)
ControlList1: TControlList;
PopupMenu1: TPopupMenu;
TestPopupMnu: TMenuItem;
Label1: TLabel;
Memo1: TMemo;
procedure ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas; ARect:
TRect; AState: TOwnerDrawState);
procedure ControlList1Click(Sender: TObject);
procedure TestPopupMnuClick(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
private
FPopupItemIndex : Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas;
ARect: TRect; AState: TOwnerDrawState);
begin
Label1.Caption := AIndex.ToString;
end;
procedure TForm1.ControlList1Click(Sender: TObject);
begin
Memo1.Lines.Add('Clicked item ' + ControlList1.ItemIndex.ToString);
end;
procedure TForm1.TestPopupMnuClick(Sender: TObject);
begin
Memo1.Lines.Add('Test PopupMenu item ' + FPopupItemIndex.ToString);
end;
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
FPopupItemIndex := ControlList1.HotItemIndex;
end;
end.
表单本身:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 299
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ControlList1: TControlList
Left = 24
Top = 20
Width = 317
Height = 200
ItemCount = 5
ItemMargins.Left = 0
ItemMargins.Top = 0
ItemMargins.Right = 0
ItemMargins.Bottom = 0
ParentColor = False
PopupMenu = PopupMenu1
TabOrder = 0
OnBeforeDrawItem = ControlList1BeforeDrawItem
OnClick = ControlList1Click
object Label1: TLabel
Left = 92
Top = 20
Width = 31
Height = 13
Caption = 'Label1'
end
end
object Memo1: TMemo
Left = 368
Top = 24
Width = 241
Height = 201
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object PopupMenu1: TPopupMenu
OnPopup = PopupMenu1Popup
Left = 436
Top = 128
object TestPopupMnu: TMenuItem
Caption = 'Test'
OnClick = TestPopupMnuClick
end
end
end
为了处理右键单击,您必须使用 OnMouseDown
或 OnMouseUp
事件而不是 OnClick
事件。
与仅检测左键单击的 OnClick
事件不同 OnMouseDown
和 OnMouseUp
事件能够检测所有鼠标点击(左、右、中)。
procedure TForm1.ControlList1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbRight then
begin
//Do what needs to be done on right click
end
else if Button = mbMiddle then
begin
//Do what needs to be done on middle click
end
end;
HotItemIndex
属性 可以通过使用以下
procedure TListingList.clListingsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if Button <> mbRight then
Exit;
if clListings.HotItemIndex <> -1 then
clListings.ItemIndex := clListings.HotItemIndex;
end;
这主要是可行的,但是如果您已经有一个 TPopupmenu
可见,那么 HotItemIndex
就是 -1
。这意味着连续右键单击不会弹出正确的项目 - 但我可以接受。我认为需要 MousePosToItemIndex
或 ItemIndexUnderMouse
方法才能正确解决此问题。