只需单击一下即可更改桌面的最简单方法是什么

时间:2009-08-08 19:12:39

标签: c# wpf xaml

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

最简单的方法是什么?

2 个答案:

答案 0 :(得分:2)

        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位置。使用后者时,您需要检查新位置是否仍在监视器内。

另请注意,这仅适用于您的显示器彼此相邻设置的情况。当监视器堆叠时,这将不起作用。

答案 1 :(得分:0)

我已经解决了问题

使用MouseLeftButtonDown单击最大化窗口然后将其最小化,现在可以将其拖动到另一个屏幕。 MouseLeftButtonUp Methode最大化了窗口

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;
}

thx @ all:)

相关问题