tags:

views:

96

answers:

2

I have a window with WindowStyle="none" and WindowState=Maximized" and now I'd like in my contextmenu set the MenuItem to switch the application to the other desktop.

whats the simplest way to do that?

+1  A: 
        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;

This will move a maximised window to the next monitor. I didn't test it though as I have only one monitor. Moving a window that is not maximised is harder because the size of the new monitor is not necessarily the same as the size of the old monitor. You could leave out setting the WindowState and center the window on the screen, or get the x position of the window on the current monitor and add it to the new x position. When using the latter you need to check if the new position is still inside the monitor.

Also note that this only works if your monitors are set up next to each other. This will not work when monitors are stacked.

Ruud v A
thx, but cannot resolve Width here: x += Screen.AllScreens[j].WorkingArea.Width;
Mario Priebe
You need to add System.Windows.Forms and System.Drawing to your assembly references.
Ruud v A
i forget the drawing... but now is the problem, that is short flicker but dont change the desktop
Mario Priebe
I'm sorry, then I cannot help you because I can't test it myself. The code should get you started to develop your own method though; you can get the screens like this.
Ruud v A
I think it is because my activated screens has number 1 and 3and the 2 is disabled and the calculation of the coordinates are wrong
Mario Priebe
Try using Bounds instead of WorkingArea. As far as I know there is no way to detect if a screen is enabled. Try placing a breakpoint at the end of this code and investigate the values. That should give you a little more insight on how this works.
Ruud v A
http://mostlytech.blogspot.com/2008/01/maximizing-wpf-window-to-second-monitor.html Should help you out too. It does something similar.
Ruud v A
A: 

I've solve the problem

when click the maximized window with the MouseLeftButtonDown then minimized this and now can i drag this to the other screen. The MouseLeftButtonUp Methode maximized the window

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 : )

Mario Priebe