Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just do</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); var pnt: TPoint; begin if GetCursorPos(pnt) then PopupMenu1.Popup(pnt.X, pnt.Y); end; </code></pre> <h1>Some more discussion</h1> <p>If you for some reason <em>need</em> to use <code>OnMosuseUp</code>, you can do</p> <pre><code>procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var pnt: TPoint; begin if (Button = mbLeft) and GetCursorPos(pnt) then PopupMenu1.Popup(pnt.X, pnt.Y); end; </code></pre> <p>Your code doesn't work because</p> <ol> <li><p><code>ClientToScreen</code> is a function of the Windows API with signature</p> <pre><code>function ClientToScreen(hWnd: HWND; var lpPoint: TPoint): BOOL; </code></pre> <p>But, there is also a <code>TControl.ClientToScreen</code> with signature</p> <pre><code>function TControl.ClientToScreen(const Point: TPoint): TPoint; </code></pre> <p>Hence, if you are in a class method, the class being a decendant of <code>TControl</code>, <code>ClientToScreen</code> will refer to the latter one. If not, it will refer to the former one. And this one, of course, needs to know which window we are to transform coordinates from!</p></li> <li><p>Also, if you declare</p> <pre><code>var mb1: TMouseButton </code></pre> <p>as a parameter, then only a variable of type <code>TMouseButton</code> will be accepted. But I cannot see any reason why you would like this signature of your <code>ShowPopupMenuEx</code> function. In fact, I see no need for such a function at all...</p></li> </ol> <h1>An Alternative</h1> <p>My code above will popup the menu at the cursor pos. If you need to fix the point relative to one corner of the button, instead, you can do</p> <pre><code>// Popup at the top-left pixel of the button procedure TForm1.Button1Click(Sender: TObject); begin with Button1.ClientToScreen(point(0, 0)) do PopupMenu1.Popup(X, Y); end; // Popup at the bottom-right pixel of the button procedure TForm1.Button1Click(Sender: TObject); begin with Button1.ClientToScreen(point(Button1.Width, Button1.Height)) do PopupMenu1.Popup(X, Y); end; // Popup at the bottom-left pixel of the button procedure TForm1.Button1Click(Sender: TObject); begin with Button1.ClientToScreen(point(0, Button1.Height)) do PopupMenu1.Popup(X, Y); end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload