2008. május 8., csütörtök

How to make a popup menu appear at a certain position over the Windows Taskbar


I create my popup menu items dynamically (not ownerdrawn). To place the popup menu at the right position (above a component), I need to determine (read) the menu item height. How would I do this?


It's quite shocking but the API offers no way to do this. The API method to get a menu to pop up in a given area is TrackPopupMenuEx. Unfortunately it is not easily applicable with a Delphi popup menu, since you cannot get the handle of the tool window the Menus unit uses to process the menu messages. So you would have to duplicate that windows message processing in another window you can get at, e.g. the form.

Make a popup menu pop up over the taskbar, bottom aligned to taskbar top:

procedure TForm1.Button1Click(Sender: TObject);
  pm: TTPMParams;
  DisplayPoint: TPoint;
  r: TRect;
  SystemParametersInfo(SPI_GETWORKAREA, 0, @r, 0);
  r.top := r.bottom + 1;
  r.bottom := screen.height;
  DisplayPoint := Point(699, r.top);
  with pm, pm.rcexclude do
    Top := r.top;
    Bottom := r.bottom;
    Left := 0;
    Right := screen.width;
    cbSize := SizeOf(pm);
  TrackPopupMenuEx(PopupMenu1.Handle, TPM_VERTICAL or TPM_HORIZONTAL,
    DisplayPoint.x, DisplayPoint.y, Handle, @pm);

Nincsenek megjegyzések:

Megjegyzés küldése