views:

332

answers:

2

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

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.)

+4  A: 

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.

Alan Clark
bsSplitButton does do the dropdown, but not when the button is clicked to the left of the divider. Easier to just handle in the onclick to process both methods.
skamradt
I saw how to do split buttons (also available in Delphi 2009, which we're using), but Microsoft's docs describe menu buttons as distinct from both split buttons and push buttons, and it seems very odd that the only way to do a menu button is to simulate one with OnClick.
Josh Kelley
It's hardly a *simulation*, Josh. What do you think "real" menu buttons do, if not display a menu when they're clicked? I mean, you could make a TMenuButton component, but all it would do is override its `Click` method to display its `PopupMenu` property instead of calling the `OnClick` event handler. So you'd really only save one function call.
Rob Kennedy
+5  A: 

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.

skamradt
You could also use the down-pointing small triangle character ▾ (http://www.fileformat.info/info/unicode/char/25be/index.htm)
Rob Kennedy
Do I need to do anything special to get that triangle to show up? It renders as a box (unrecognized character) when I try it in my controls.
Josh Kelley
If you can't be assured of having a font with the desired character, then a bit-button with a glyph aligned to the right is probably the better idea. You might be able to ask the OS for an appropriate built-in image, but that's a topic for a separate question.
Rob Kennedy