下拉菜单TButton的 [英] Drop down menu for TButton

查看:168
本文介绍了下拉菜单TButton的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模拟一个下拉菜单,一个TButton,如下图所示:

I am trying to simulate a drop down menu for a TButton, as shown below:

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;

问题是,当菜单掉了下来,如果我按一下按钮,我再次想菜单关闭,而是再次滴下来。

The problem is that when the menu is dropped down, if I click the button again I would like the menu to close, but instead it drops down again.

我要寻找一个解决方案的专门作为通用德尔福 TButton的没有任何第三方的等价物。

I am looking for a solution specifically for generic Delphi TButton not any 3rd Party equivalent.

推荐答案

根据我们(弗拉德和放大器;我)的讨论,您可以使用一个变量来知道什么时候弹出后打开,如果你显示的弹出菜单或取消鼠标选择事件:

Following our (Vlad & I) discussion, you use a variable to know when the popup was last opened to choose if you display the popupmenu or cancel the mouse event:

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.

这篇关于下拉菜单TButton的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆