【问题标题】:What is the simplest method to change the desktop with one click一键更换桌面最简单的方法是什么
【发布时间】:2009-08-08 19:12:38
【问题描述】:

我有一个 WindowStyle="none" 和 WindowState=Maximized" 的窗口,现在我想在我的上下文菜单中设置 MenuItem 以将应用程序切换到另一个桌面。

最简单的方法是什么?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:
            using System.Windows.Forms;
            using System.Drawing;
            using System.Windows.Interop;
    
            Screen screen = Screen.FromHandle(new WindowInteropHelper(this).Handle);
            int i;
            for (i = 0; i < Screen.AllScreens.Length; i++)
            {
                if (Screen.AllScreens[i] == screen) break;
            }
            i++; i = i % Screen.AllScreens.Length;
    
            this.WindowState = WindowState.Normal;
            int x = 0;
            for (int j = 0; j < i; j++)
            {
                x += Screen.AllScreens[j].Bounds.Width;
            }
            this.Left = x + 1;
            this.WindowState = WindowState.Maximized;
    

    这会将最大化的窗口移动到下一个监视器。我没有测试它,因为我只有一台显示器。移动未最大化的窗口比较困难,因为新显示器的尺寸不一定与旧显示器的尺寸相同。您可以省略设置 WindowState 并将窗口居中放在屏幕上,或者在当前监视器上获取窗口的 x 位置并将其添加到新的 x 位置。使用后者时,您需要检查新位置是否仍在监视器内。

    另请注意,这仅在您的显示器彼此相邻设置时才有效。当显示器堆叠时,这将不起作用。

    【讨论】:

    • thx,但这里无法解析宽度:x += Screen.AllScreens[j].WorkingArea.Width;
    • 您需要将 System.Windows.Forms 和 System.Drawing 添加到您的程序集引用中。
    • 我忘记画图了……但现在是问题,就是短暂闪烁但不要改变桌面
    • 很抱歉,我无法为您提供帮助,因为我自己无法测试。不过,该代码应该可以帮助您开始开发自己的方法;你可以得到这样的屏幕。
    • 我认为是因为我激活的屏幕有1号和3号,2号被禁用,坐标计算错误
    【解决方案2】:

    我已经解决了问题

    当使用 MouseLeftButtonDown 单击最大化的窗口然后最小化它,现在我可以将它拖到另一个屏幕上。 MouseLeftButtonUp 方法最大化窗口

    private void win_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        click = new Point(e.GetPosition(this).X, e.GetPosition(this).Y);
        win.WindowState = WindowState.Normal;
    }
    
    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        this.Left += e.GetPosition(this).X - click.X;
        this.Top += e.GetPosition(this).Y - click.Y;
    }
    
    private void win_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        win.WindowState = WindowState.Maximized;
    }
    

    谢谢@所有 :)

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 2013-04-06
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多