hardcodet / wpf-notifyicon

NotifyIcon (aka system tray icon or taskbar icon) for the WPF platform
MIT License
830 stars 130 forks source link

TrayInfo.GetDeviceCoordinates is giving wrong coordinates after system scale change #15

Closed rolikoff closed 4 years ago

rolikoff commented 5 years ago

Found an issue with wpf-notifyicon 1.0.8 where TrayInfo.GetDeviceCoordinates() returns wrong coordinates. To reproduce that, set the system scale to 100%, open the app that uses wpf-notifyicon, call var ScreenLocation = Hardcodet.Wpf.TaskbarNotification.Interop.TrayInfo.GetTrayLocation(); Then change the scale to 150% and call GetTrayLocation() again, it will return the same result.

The problem is that GetTrayLocation() does some coordinates transformation depending on current system DPI:

        public static Point GetDeviceCoordinates(Point point)
        {
          return new Point
          {
              X = (int)(point.X / SystemInfo.DpiFactorX),
              Y = (int)(point.Y / SystemInfo.DpiFactorY)
          };

SystemInfo.DpiFactorX and SystemInfo.DpiFactorY are caching the old values. There needs to be some sort of system DPI change awareness, or, alternatively, do not cache the DPI Factor and do win32 API calls every time one of the properties are triggered.

hardcodet commented 5 years ago

@rolikoff Makes sense - I'll look into subscribing to DPI factor changes. This might need some work anyway (new APIs as of Win 8.1 / 10, per-monitor DPIs).

Lakritzator commented 5 years ago

Been there, done that... @hardcodet if you like, I could take care of this, but you are more than welcome to do so.

You will need to handle more Windows messages, especially the WM_DPICHANGED.

But the main issue with this, if the hosting application doesn't make itself DPI aware, you can't do much. So you might need to specify some prerequisites for the application using this library.

I have some DPI code in here: https://github.com/dapplo/Dapplo.Windows/blob/master/src/Dapplo.Windows.Dpi/NativeDpiMethods.cs Actually it's a whole project: https://github.com/dapplo/Dapplo.Windows/tree/master/src/Dapplo.Windows.Dpi 😁

hardcodet commented 5 years ago

@Lakritzator I'll gladly take you up on that offer! I'm as diconnected from the Windows API as it gets those days ;)

Lakritzator commented 4 years ago

Just to inform, if the dotnet core 3 pull-request is merged I will continue on this.

rolikoff commented 4 years ago

@Lakritzator thanks, I am not sure the maintainer has enough time to actually maintain the repo. There're still lots of issues with the lib that some people give up and use winforms just for handling the system tray icon and balloons. Maybe WinUI3.0 will bring us something...

Lakritzator commented 4 years ago

I'm in direct contact with the maintainer, I don't need that much time from him as he just needs to confirm that he's okay with the direction. I have the rights to merge myself, but don't want to misuse them. I also have all the knowledge to fix this, I just don't want to create fixes which make merging my bigger PR very hard.

For the next Greenshot version I would like to move to this project, so I need to have this solved too. But I am also quite busy, due to the closing of the kindergarten my free time is practically went down to 0. When one doesn't earn money with a project, the priority is the lowest 🤷

Lakritzator commented 4 years ago

So we are one step further, bear with me that I have limited time and energy (work & kid are taking it's toll), last evening I was trying to look at this I just fell asleep... and needed my 🛌

Lakritzator commented 4 years ago

@hakanyildizhan without having more information, your question is a bit like "always when I connect a second keyboard to my PC, it starts beeping"...

I can't imagine why this library would un-minimize your MainWindow after coming from back from sleep. But if you have a main window which should never be visible, than you should make sure it is not visible yourself! Anyway, it certainly is not related to THIS issue.

Lakritzator commented 4 years ago

I forgot to reference this issue in my commit, this is fixed with https://github.com/hardcodet/wpf-notifyicon/commit/de6a2b2e2598e8842c17ce14635a918ec3dbaa74 You can expected it in version 1.0.9 or later, don't know when it will be available.