【发布时间】:2009-08-08 19:12:38
【问题描述】:
我有一个 WindowStyle="none" 和 WindowState=Maximized" 的窗口,现在我想在我的上下文菜单中设置 MenuItem 以将应用程序切换到另一个桌面。
最简单的方法是什么?
【问题讨论】:
我有一个 WindowStyle="none" 和 WindowState=Maximized" 的窗口,现在我想在我的上下文菜单中设置 MenuItem 以将应用程序切换到另一个桌面。
最简单的方法是什么?
【问题讨论】:
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 位置。使用后者时,您需要检查新位置是否仍在监视器内。
另请注意,这仅在您的显示器彼此相邻设置时才有效。当显示器堆叠时,这将不起作用。
【讨论】:
我已经解决了问题
当使用 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;
}
谢谢@所有 :)
【讨论】: