ClientToScreen conversion

I have a TWebLabel with TWebPopupMenu attached. This works when right-clicked. But I also want it to pop up when left clickedl.

To do so I need the position of the Label on the screen. I tried this but no luck

procedure Tfrm_Ops_Main.lblLoggedClick (Sender : TObject);
var
  LPosn : TPoint;
begin
  LPosn := Point (lblLogged.Left, lblLogged.Top);
  LPosn := lblLogged.ClientToScreen (LPosn);
  pum.Popup (LPosn.X, LPosn.Y);
end;

LPosn.Y is bang on. but LPosn.X is off the screen. The value is actually too large by about 600.

Is there a way to get the values or to pop up the menu close to the label.

P.S. I have a more complete function that adds the scroll value, in case the screen has been scrolled. I am just simplifying for testing.

I use this to position an edit box at the appropriate place on the form. I think you need to use the form or parent control.

p[0] := DrawSurface.ClientToScreen(DrawSurface.Parent.ScreenToClient(Node.Points[0]));

I finally came with a decent generic solution. I am posting it here, in case its useful to others.

Just call it with the ElementId and it will call generate a right-click event when you do left-click;

procedure Left_To_Right_Click (const AElementId : string);
begin
  asm
    const targetElement = document.getElementById(AElementId);
    if (targetElement) {
      targetElement.addEventListener('click', function(event) {
        // Prevent default left-click behavior
        event.preventDefault();

        // Create a right-click event
        const rightClick = new MouseEvent('contextmenu', {
          bubbles: true,
          cancelable: true,
          view: window,
          button: 2, // 2 is right button
          buttons: 2, // Secondary button mask
          clientX: event.clientX,
          clientY: event.clientY
        });

        // Dispatch the right-click event
        targetElement.dispatchEvent(rightClick);
      });
    }
  end;
end;
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.