TButton的下拉菜单

时间:2012-05-15 09:28:25

标签: delphi button drop-down-menu delphi-7

我正在尝试模拟TButton的下拉菜单,如下所示:

procedure DropMenuDown(Control: TControl; PopupMenu: TPopupMenu);
var
  APoint: TPoint;
begin
  APoint := Control.ClientToScreen(Point(0, Control.ClientHeight));
  PopupMenu.Popup(APoint.X, APoint.Y);
end;

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    DropMenuDown(Button1, PopupMenu1);
    // ReleaseCapture;
  end;
end;

问题是,当菜单下拉时,如果我再次点击该按钮,我希望菜单关闭,但它会再次下降。

我正在为通用Delphi TButton寻找解决方案具体,而不是任何第三方等效。

2 个答案:

答案 0 :(得分:5)

在审查了Whiler& amp;提供的解决方案之后弗拉德,并将它与WinSCP实现相同的方式进行比较,我目前正在使用以下代码:

unit ButtonMenus;
interface
uses
  Vcl.Controls, Vcl.Menus;

procedure ButtonMenu(Control: TControl; PopupMenu: TPopupMenu);

implementation

uses
  System.Classes, WinApi.Windows;

var
  LastClose: DWord;
  LastPopupControl: TControl;
  LastPopupMenu: TPopupMenu;

procedure ButtonMenu(Control: TControl; PopupMenu: TPopupMenu);
var
  Pt: TPoint;
begin
  if (Control = LastPopupControl) and (PopupMenu = LastPopupMenu) and (GetTickCount - LastClose < 100) then begin
    LastPopupControl := nil;
    LastPopupMenu := nil;
  end else begin
    PopupMenu.PopupComponent := Control;
    Pt := Control.ClientToScreen(Point(0, Control.ClientHeight));
    PopupMenu.Popup(Pt.X, Pt.Y);
    { Note: PopupMenu.Popup does not return until the menu is closed }
    LastClose := GetTickCount;
    LastPopupControl := Control;
    LastPopupMenu := PopupMenu;
  end;
end;

end.

除了在ButtonMenu()处理程序中调用onClick之外,它的优点是不需要对from进行任何代码更改:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ButtonMenu(Button1, PopupMenu1);
end;

答案 1 :(得分:4)

按照我们(Vlad&amp; I)的讨论,您使用变量来了解上次打开弹出窗口的时间,以选择是否显示弹出菜单或取消鼠标事件:

unit Unit4;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls;

type
  TForm4 = class(TForm)
    PopupMenu1: TPopupMenu;
    Button1: TButton;
    fgddfg1: TMenuItem;
    fdgdfg1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    cMenuClosed: Cardinal;

  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure DropMenuDown(Control: TControl; PopupMenu: TPopupMenu);
var
  APoint: TPoint;
begin
  APoint := Control.ClientToScreen(Point(0, Control.ClientHeight));
  PopupMenu.Popup(APoint.X, APoint.Y);
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
  DropMenuDown(Button1, PopupMenu1);
  cMenuClosed := GetTickCount;
end;

procedure TForm4.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and not ((cMenuClosed + 100) < GetTickCount) then
  begin
    ReleaseCapture;
  end;
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  cMenuClosed := 0;
end;

end.