Error in rs.PopupMenu() location

Does anyone have a workaround for rs.PopupMenu() popping up in the wrong position? By default it is supposed to popup under the cursor, but since V7 or earlier it always is way far away for me, most often in the lower right hand corner of the screen.

I have posted about this a long time ago, but nothing has been done. It may be that this phenomenon is due to display scaling? I just now tried to figure out how to get the cursor position and calculate myself in screen coordinates to feed to the method, but I failed miserably - I’m not very good with this stuff.

Has anyone ever tried to program a workaround?

Partially (and hopefully correctly) answering my own question - the answer to it might be yes.

I tried scaling the X and Y coordinates of the cursor location according to the display scaling I have here - 225%:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino, System

items=["A","B","C","D","None of the Above"]
ds=100/225  #display scaling factor
screen_point=System.Windows.Forms.Cursor.Position
screen_point.X*=ds
screen_point.Y*=ds
result=Rhino.UI.Dialogs.ShowContextMenu(items, screen_point, None)
print ("You chose {}".format(items[result]))

Running this on my laptop at 225% scaling, the menu then pops up pretty much where it’s supposed to, close to the location of the mouse cursor where one clicks, a little to the right and below.

Unscaled:

Scaled:

Anyone know how to query the Windows display scaling factor on the local machine?

Hmm, looks like I’m answering my own questions today… found on StackOverflow:

import ctypes
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
print (ScaleFactor)

What was astonishing was that this also seems to work in IronPython 2.7 so I can be backwards compatible to V7. Probably doesn’t work on Mac though.

Hi @Helvetosaur,

This work here, even on monitors with dpi scaling enabled.

import rhinoscriptsyntax as rs

items = ["A","B","C","D","None"]
rc = rs.PopupMenu(items)

– Dale

@dale Well, I don’t know what to tell you - as the picture above attests to, PopupMenu() doesn’t work correctly here on either of my two computers/monitors with different dpi scaling. And this in Rhino 6, 7 and 8. Adding the correction (225 for my laptop and 150 for my desktop) via the script makes it position perfectly.

Are you testing with a 4K monitor?