【问题标题】:How to create popup menu with scroll bar that also supports sub-menus如何创建带有滚动条的弹出菜单也支持子菜单
【发布时间】:2017-01-12 16:28:25
【问题描述】:

我想在我现有的 Delphi 应用程序的弹出菜单中添加滚动条(和/或滚轮支持),因为它们通常高于屏幕,并且内置滚动功能不够好。 How to make a popup menu with scrollbar? 对我来说是一个很好的解决方案,只是它不支持我绝对需要的子菜单。该解决方案的作者自去年 7 月以来一直没有在 StackOverflow 上,所以我认为他不会回复我的评论。谁能看到如何修改该代码以添加对子菜单的支持?万一这很重要,我需要它与 Delphi 2007 一起工作。

【问题讨论】:

  • 我认为 TListBox 是添加滚动条的原因。据我所知,TMainMenu 没有滚动条。
  • 这是上面的标志。找到比弹出菜单更好的 UI 范例!
  • 忘记我之前的 cmets。如果您正在考虑推出自己的解决方案,您可以查看免费软件 TVirtualTreeView 作为起点 - 请参阅 soft-gems.net/index.php/controls/virtual-treeview
  • 谢谢。那么,您认为不可能在链接的解决方案中添加对子菜单的支持吗?这将是我的首选方法。
  • 该代码基于创建一个带有伪装成菜单的列表框的表单(因此提供滚动条)。没有办法让列表框弹出子菜单或子列表框,所以你必须自己想出一个。虽然 a)如果您有那么大的弹出菜单,您的 UI 需要进行一些认真的重新设计(我的用户会反抗),并且 b)如果您想打破标准 UI 控件以在您的超大菜单中工作,您应该准备卷起袖子写代码来做这件事,而不是指望别人为你做。

标签: delphi


【解决方案1】:

我分享了@KenWhite 对用户如何接收巨大菜单的保留意见。因此,向他和以下可能冒犯的读者道歉;=)

无论如何,我希望下面的代码显示,原则上,它是直截了当的 创建一个基于 TPopUpMenu 的 TreeView(参见例程 PopUpMenuToTree),它反映了 PopUpMenu 的结构,包括子项, 并利用 TreeView 的自动垂直滚动条。在代码中, PopUpMenu 恰好与 TreeView 在同一窗体上,但这仅适用于 紧凑,当然——PopUpMenu 可以完全是另一种形式。

正如评论中提到的,我个人认为这样的事情是基于 TVirtualTreeView (http://www.soft-gems.net/index.php/controls/virtual-treeview) 因为它比标准的 TTreeView 更具可定制性。

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    TreeView1: TTreeView;  //  alClient-aligned
    Start1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
  private
  protected
    procedure MenuItemClick(Sender : TObject);
    procedure PopUpMenuToTree(PopUpMenu : TPopUpMenu; TreeView : TTreeView);
  public
  end;

var
  Form1: TForm1;

[...]

procedure TForm1.FormCreate(Sender: TObject);
var
  Item,
  SubItem : TMenuItem;
  i,
  j : Integer;
begin
  //  (Over)populate a PopUpMenu
  for i := 1 to 50 do begin
    Item := TMenuItem.Create(PopUpMenu1);
    Item.Caption := 'Item ' + IntToStr(i);
    Item.OnClick := MenuItemClick;
    PopUpMenu1.Items.Add(Item);
    for j := 1 to 5 do begin
      SubItem := TMenuItem.Create(PopUpMenu1);
      SubItem.Caption := Format('Item %d Subitem %d ', [i, j]);
      SubItem.OnClick := MenuItemClick;
      Item.Add(SubItem);
    end;
  end;

  //  Populate a TreeView from the PopUpMenu
  PopUpMenuToTree(PopUpMenu1, TreeView1);
end;

procedure TForm1.MenuItemClick(Sender: TObject);
var
  Item : TMenuItem;
begin
  if Sender is TMenuItem then
    Caption := TMenuItem(Sender).Caption + ' clicked';
end;

procedure TForm1.PopUpMenuToTree(PopUpMenu: TPopUpMenu;
  TreeView: TTreeView);
//  Populates the TreeView with the Items in the PopUpMenu
var
  i : Integer;
  Item : TMenuItem;
  RootNode : TTreeNode;

  procedure AddItem(Item : TMenuItem; ParentNode : TTreeNode);
  var
    Node : TTreeNode;
    j : Integer;
  begin
    Node := TreeView.Items.AddChildObject(ParentNode, Item.Caption, Item);
    for j := 0 to Item.Count - 1 do begin
      AddItem(Item.Items[j], Node);
    end;
  end;

begin
  TreeView.Items.BeginUpdate;
  TreeView.Items.Clear;
  try
    for i := 0 to PopUpMenu.Items.Count - 1 do begin
      AddItem(PopUpMenu.Items[i], Nil);
    end;
  finally
    TreeView.Items.EndUpdate;
  end;
end;

procedure TForm1.TreeView1Click(Sender: TObject);
var
  Node : TTreeNode;
  Item : TMenuItem;
begin
  if Sender is TTreeView then begin
    Node := TTreeView(Sender).Selected;
    Item := TMenuItem(Node.Data);
    Item.Click;
  end;
end;

【讨论】:

  • 非常感谢您,我认为这对于我的应用程序来说可能是一个非常好的解决方案。我现在正在处理它,完成后会发布完成的代码。
  • 我现在已经接受了,再次感谢,但是对于寻找实际完成解决方案的其他人,请参阅我单独发布的代码。
【解决方案2】:

这是我所做的,通过合并 How to make a popup menu with scrollbar?、MartynA 的代码和我自己的一些代码:

unit PopupUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  StdCtrls, Menus, ComCtrls;

type
  TPopupMode = (pmStandard, pmCustom);
  TPopupMenu = class(Menus.TPopupMenu)
  private
    FPopupForm: TForm;
    FPopupMode: TPopupMode;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Popup(X, Y: Integer); override;
    property PopupForm: TForm read FPopupForm write FPopupForm;
    property PopupMode: TPopupMode read FPopupMode write FPopupMode;
  end;

type
  TPopupForm = class(TForm)
  private
    FPopupForm: TForm;
    FPopupMenu: TPopupMenu;
    FTreeView: TTreeView;
    procedure DoResize;
    procedure TreeViewClick(Sender: TObject);
    procedure TreeViewCollapsedOrExpanded(Sender: TObject; Node: TTreeNode);
    procedure TreeViewKeyPress(Sender: TObject; var Key: Char);
    procedure WMActivate(var AMessage: TWMActivate); message WM_ACTIVATE;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    constructor Create(AOwner: TComponent; APopupForm: TForm;
      APopupMenu: TPopupMenu); reintroduce;
  end;

var
  PopupForm: TPopupForm;

implementation

{$R *.dfm}

{ TPopupForm }

constructor TPopupForm.Create(AOwner: TComponent; APopupForm: TForm;
  APopupMenu: TPopupMenu);

  procedure AddItem(Item : TMenuItem; ParentNode : TTreeNode);
  var
    I : Integer;
    Node : TTreeNode;
  begin
    if Item.Caption <> '-' then begin
      Node := FTreeView.Items.AddChildObject(ParentNode, Item.Caption, Item);
      Node.ImageIndex := Item.ImageIndex;
      for I := 0 to Item.Count - 1 do begin
        AddItem(Item.Items[I], Node);
      end;
    end;
  end;

var
  I: Integer;
begin
  inherited Create(AOwner);
  BorderStyle := bsNone;

  FPopupForm := APopupForm;
  FPopupMenu := APopupMenu;

  FTreeView := TTreeView.Create(Self);
  FTreeView.Parent := Self;
  FTreeView.Align := alClient;
  FTreeView.BorderStyle := bsSingle;
  FTreeView.Color := clMenu;
  FTreeView.Images := FPopupMenu.Images;
  FTreeView.ReadOnly := TRUE;
  FTreeView.ShowHint := FALSE;
  FTreeView.ToolTips := FALSE;
  FTreeView.OnClick := TreeViewClick;
  FTreeView.OnCollapsed := TreeViewCollapsedOrExpanded;
  FTreeView.OnExpanded := TreeViewCollapsedOrExpanded;
  FTreeView.OnKeyPress := TreeViewKeyPress;

  FTreeView.Items.BeginUpdate;
  try
    FTreeView.Items.Clear;
    for I := 0 to FPopupMenu.Items.Count - 1 do
    begin
      AddItem(FPopupMenu.Items[I], NIL);
    end;
  finally
    FTreeView.Items.EndUpdate;
  end;
  DoResize;
end;

procedure TPopupForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.WindowClass.Style := Params.WindowClass.Style or CS_DROPSHADOW;
end;

procedure TPopupForm.DoResize;
const
  BORDER = 2;
var
  ItemRect, TVRect : TRect;
  MF : TForm;
  Node : TTreeNode;
begin
  TVRect := Rect(0, 0, 0, 0);
  Node := FTreeView.Items[0];
  while Node <> NIL do begin
    ItemRect := Node.DisplayRect(TRUE);
    ItemRect.Right := ItemRect.Right + FTreeView.Images.Width + 1;
    if ItemRect.Left < TVRect.Left then
      TVRect.Left := ItemRect.Left;
    if ItemRect.Right > TVRect.Right then
      TVRect.Right := ItemRect.Right;
    if ItemRect.Top < TVRect.Top then
      TVRect.Top := ItemRect.Top;
    if ItemRect.Bottom > TVRect.Bottom then
      TVRect.Bottom := ItemRect.Bottom;
    Node := Node.GetNextVisible;
  end;
  MF := Application.MainForm;
  if Top + TVRect.Bottom - TVRect.Top > MF.Top + MF.ClientHeight then begin
    TVRect.Bottom := TVRect.Bottom -
      (Top + TVRect.Bottom - TVRect.Top - (MF.Top + MF.ClientHeight));
  end;
  if Left + TVRect.Right - TVRect.Left > MF.Left + MF.ClientWidth then begin
    TVRect.Right := TVRect.Right -
      (Left + TVRect.Right - TVRect.Left - (MF.Left + MF.ClientWidth));
  end;
  ClientHeight := TVRect.Bottom - TVRect.Top + BORDER * 2;
  ClientWidth := TVRect.Right - TVRect.Left + BORDER * 2;
end;

procedure TPopupForm.TreeViewClick(Sender: TObject);
var
  Node : TTreeNode;
  Item : TMenuItem;
begin
  if Sender is TTreeView then begin
    Node := TTreeView(Sender).Selected;
    if assigned(Node) then begin
      Item := TMenuItem(Node.Data);
      if assigned(Item.OnClick) then begin
        Item.Click;
        Close;
      end;
    end;
  end;
end;

procedure TPopupForm.TreeViewCollapsedOrExpanded(Sender: TObject;
  Node: TTreeNode);
begin
  DoResize;
end;

procedure TPopupForm.TreeViewKeyPress(Sender: TObject; var Key: Char);
begin
  if Ord(Key) = VK_RETURN then begin
    TreeViewClick(Sender);
  end
  else if Ord(Key) = VK_ESCAPE then begin
    Close;
  end;
end;

procedure TPopupForm.WMActivate(var AMessage: TWMActivate);
begin
  SendMessage(FPopupForm.Handle, WM_NCACTIVATE, 1, 0);
  inherited;
  if AMessage.Active = WA_INACTIVE then
    Release;
  FTreeView.Select(NIL, []);
end;

{ TPopupMenu }

constructor TPopupMenu.Create(AOwner: TComponent);
begin
  inherited;
  FPopupMode := pmStandard;
end;

procedure TPopupMenu.Popup(X, Y: Integer);
begin
  case FPopupMode of
    pmCustom:
    with TPopupForm.Create(nil, FPopupForm, Self) do
    begin
      Top := Y;
      Left := X;
      Show;
    end;
    pmStandard: inherited;
  end;
end;

end.

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 2016-04-15
    • 2010-12-24
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多