Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a menu button in Windows

Microsoft's User Experience Interaction Guidelines give some UI guidelines for when to use a menu button:

http://i.msdn.microsoft.com/Aa511453.command51(en-us,MSDN.10).png

How do I create one of these menu buttons? I've found information on

  • how to create a split button in Vista and above
  • how to create a toolbar button with a dropdown menu
  • how to create a regular pushbutton and manually wire up an OnClick event handler that pops up a menu

But is there any standard way to create a button, not in a toolbar, with the little down triangle, that automatically pops up a menu when clicked?

(I'm using Delphi / C++Builder, but other solutions are welcome.)

like image 513
Josh Kelley Avatar asked Jan 06 '10 22:01

Josh Kelley


People also ask

How do I create a custom Start menu?

Head to Settings > Personalization > Start. On the right, scroll all the way to the bottom and click the “Choose which folders appear on Start” link. Choose whatever folders you want to appear on the Start menu. And here's a side-by-side look at how those new folders look as icons and in the expanded view.

How do I create a Windows program menu?

To assign a menu to a window, use the SetMenu function or specify the menu's handle in the hMenu parameter of the CreateWindowEx function when creating a window.

How do I add menu bar to Windows 10?

I press the Alt key and hold it down for around one second. Upon release, the invisible menu bar appears. This trick works for most every kind of window.


2 Answers

You can use the OnClick to force the popup, and for consistency don't use the cursor position, but rather the control position.

procedure TForm1.Button1Click(Sender: TObject);
var
  pt : TPoint;
begin
  Pt.X := Button1.Left;
  Pt.Y := Button1.Top+Button1.Height;
  pt := ClientToScreen(Pt);
  PopupMenu1.Popup(pt.x,pt.y);
end;

You can then add the "glyph" using either a Delphi 2010 button, or a previous version TBitBtn and assign the bitmap/glyph property to an appropriate image and align right.

like image 55
skamradt Avatar answered Oct 15 '22 05:10

skamradt


You don't mention which version of Delphi you are using, but in Delphi 2010 TButton has new properties for this: DropDownList which can be associated with a TPopupMenu to define the menu items, and Style which can be set to bsSplitButton.

This produces a button that you can press that also has a dropdown arrow on the right of it, To make the menu popup when you click to the left of the arrow this code in the button click handler should do the job.

procedure TForm1.Button1Click(Sender: TObject);
var
  CursorPos: TPoint;
begin
  GetCursorPos(CursorPos);
  PopupMenu1.Popup(CursorPos.X, CursorPos.Y);
end;

in previous versions of Delphi I think you had to use TToolBar.

like image 38
Alan Clark Avatar answered Oct 15 '22 03:10

Alan Clark