Author: Jonas Bilinkevicius
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?
Answer:
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:
1 procedure TForm1.Button1Click(Sender: TObject);
2 var
3 pm: TTPMParams;
4 DisplayPoint: TPoint;
5 r: TRect;
6 begin
7 SystemParametersInfo(SPI_GETWORKAREA, 0, @r, 0);
8 r.top := r.bottom + 1;
9 r.bottom := screen.height;
10 DisplayPoint := Point(699, r.top);
11 with pm, pm.rcexclude do
12 begin
13 Top := r.top;
14 Bottom := r.bottom;
15 Left := 0;
16 Right := screen.width;
17 cbSize := SizeOf(pm);
18 end;
19 TrackPopupMenuEx(PopupMenu1.Handle, TPM_VERTICAL or TPM_HORIZONTAL,
20 DisplayPoint.x, DisplayPoint.y, Handle, @pm);
21 end;
|