Jump to content
Steve Maughan

Problem with ClientToScreen() on Multiple Monitors

Recommended Posts

I have a popup menu on a TPaintBox. Up to now I've used the simple code:

PopupPoint := PaintBox.ClientToScreen(X, Y);
RadialMenu.Popup(PopupPoint.X, PopupPoint.Y);

It seem this doesn't work on multi-monitor systems — the Radial Menu isn't visible after the popup and the user thinks the application has crashed. 

 

How do I adjust this so it works with multiple monitors? Thanks!

 

Steve

Share this post


Link to post

Which version of Delphi are you using?  TControl.ClientToScreen() does not take 2 integers as input, it takes either a TPoint or a TRect.

 

But, fixing that, the rest of the code shown works just fine for me on my 2-monitor system.  ClientToScreen() returns screen coordinates, and Popup() takes screen coordinates, and screen coordinates are within the Virtual Screen, not any particular monitor.  Regardless of which monitor the PaintBox is displayed on, the PopupMenu appear wherever I click on the PaintBox.  So, either you are doing something else wrong that we can't see, or this is a bug in your Delphi's VCL,

Edited by Remy Lebeau

Share this post


Link to post
1 minute ago, Remy Lebeau said:

Which version of Delphi are you using?  TControl.ClientToScreen() does not take 2 integers as input, it takes either a TPoint or a TRect.

 

But, fixing that, the rest of the code shown works fine for me on my 2-monitor system.  So, unless there is a bug in your Delphi's VCL, this should work just fine, since ClientToScreen() returns screen coordinates, and Popup() takes screen coordinates.  Screen coordinates are within the Virtual Screen, not any particular monitor.

Thanks Remy, I am actually passing a TPoint (my error when creating the code example). The user is reporting that the application crashes when they right click, and it's corrected when they only have one monitor. Does ClientToScreen work in all cases when the extra monitor is above, below, left and right of the main screen?

 

Steve

Share this post


Link to post
29 minutes ago, Steve Maughan said:

The user is reporting that the application crashes when they right click, and it's corrected when they only have one monitor.

So you haven't tried to reproduce the problem with two monitors...?

 

29 minutes ago, Steve Maughan said:

Does ClientToScreen work in all cases when the extra monitor is above, below, left and right of the main screen? 

Yes.

If you had looked at the source you would have seen that TControl.ClientToScreen calls TControl.GetClientOrigin which ends up calling Winapi.Windows.ClientToScreen(Handle, Result);

The ClientToScreen API function returns the coordinates relative to the top left corner of the primary monitor, which is what you'd want.

Share this post


Link to post
2 minutes ago, Anders Melander said:

So you haven't tried to reproduce the problem with two monitors...?

 

Yes.

If you had looked at the source you would have seen that TControl.ClientToScreen calls TControl.GetClientOrigin which ends up calling Winapi.Windows.ClientToScreen(Handle, Result);

The ClientToScreen API function returns the coordinates relative to the top left corner of the primary monitor, which is what you'd want.

I don't have an extra monitor but a colleague has one so we're trying to reproduce. It seem the problem occurs in a configuration of two monitors above the main screen / laptop screen. 

Share this post


Link to post
2 hours ago, Steve Maughan said:

The user is reporting that the application crashes when they right click, and it's corrected when they only have one monitor.

Is there an error message?  And you didn't answer my question - which version of Delphi are you using?  Nothing in the code you have shown should be accessing any monitor-specific functionality, AFAIK.

2 hours ago, Steve Maughan said:

Does ClientToScreen work in all cases when the extra monitor is above, below, left and right of the main screen?

Yes.

Share this post


Link to post
16 hours ago, Remy Lebeau said:

Is there an error message?  And you didn't answer my question - which version of Delphi are you using?  Nothing in the code you have shown should be accessing any monitor-specific functionality, AFAIK.

Yes.

Hi Remy,

 

I'm using Delphi 10.4.2.

 

We have been unsuccessful in replicating the problem, although I did see it for myself on a Zoom call. At this point I'm going to move on and class it as a strange setup issue.

 

Thanks — Steve 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×