Window ActualTop, ActualLeft

前端 未结 4 1199
小蘑菇
小蘑菇 2020-12-02 02:11

What is the correct way to retrieve a window\'s position in WPF?

Here\'s some attempts I made. First attempt, the obvious

Point GetPosition(Window wi         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 02:51

    You can use reflection if you don't want to rely on Winforms or PI

    private double GetWindowLeft(Window window)
        {
            if (window.WindowState == WindowState.Maximized)
            {
                var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                return (double)leftField.GetValue(window);
            }
            else
                return window.Left;
        }
    

提交回复
热议问题