开发者

WPF and windows tiling

I have a WPF application that will always run on windows 7, it opens and displays in full-screen mode

There is a button within WPF app that opens up Wo开发者_如何学Crd

I would like the WPF application on opening up Word to send a command to the operating system so that it tiles both the WPF application and the Word application windows side by side, so that they each take up 50% of screen each. Is this possible? if yes would anyone know what the code may be? ( a bit like when you right click on windows 7 task bar and click Show Windows Side by Side)


Yes it is possible and is relatively easy.

You say that you already have code that opens your WPF window "full screen". Assuming this is a true "full screen" view and not a "maximized" window you have already calculated the screen rectangle. If not, you can use the Screen class from System.Windows.Forms to get the screen rectangle. Remember that the Screen class is part of WinForms, so it uses device coordinates (pixels) instead of resolution-independent coordinates (96dpi) so you'll need to convert from Rectangle to Rect and then apply the PresentationSource.FromVisual(window).TransformFromDevice transform to get WPF coordinates.

Once you have the screen Rect in device-independent (96dpi) coordinates, you must compute the separate rectangles. For example here is the computation for a 50% horizontal division of screenRect:

Rect rect1 = new Rect(screenRect.X, screenRect.Y, screenRect.Width/2, screenRect.Height);
Rect rect2 = new Rect(screenRect.X + screenRect.Width/2, screenRect.Y, screenRect.Width/2, screenRect.Height);

Once you have the bounds you can adjust your own window simply enough:

window.Left = rect1.X;
window.Top = rect1.Y;
window.Width = rect1.Width;
window.Height = rect1.Height;

Moving the Word window is slightly more complicated. You must convert to device coordinates, start the process, wait for Word to initialize, then call Win32's SetWindowPos function. This must be done on a separate thread to avoid blocking the UI.

// Transform rect2 to device coordinates
rect2.Transform(PresentationSource.FromVisual(window).TransformToDevice);

// Execute the rest of this in a separate thread
ThreadPool.QueueUserWorkItem(_ =>
{
  // Start Word
  var process = Process.Create(pathToWinwordExe);

  // Wait for Word to initialize
  process.WaitForInputIdle();

  // Set the position of the main window
  IntPtr hWnd = process.MainWindowHandle;
  if(hWnd!=IntPtr.Zero)
    SetWindowPos(
      hWnd, IntPtr.Zero,
      (int)rect2.X, (int)rect2.Y, (int)rect2.Width, (int)rect2.Height,
      SWP_NOZORDER);
});

Don't forget the DllImport for SetWindowPos. Something like this should work:

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
public static extern bool SetWindowPos(
  HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy, int flags);


here is a work around for this:

        Window1 window = new Window1();
        double height = SystemParameters.WorkArea.Height;
        double width= SystemParameters.WorkArea.Width;
        this.Height = height;
        this.Width = width / 2;
        this.Left = 0;
        this.Top = 0;
        window.Height = height;
        window.Width = width / 2;
        window.Left = width / 2;
        window.Top = 0;
        window.Show();

Hope it helps!!


Another way that I tried out was to call a vbs file after starting up the exe program within the wpf application, the vbs file will then send the command to the system to tile vertically etc

dim objShell set objShell = CreateObject("Shell.Application") objShell.TileVertically set objShell = nothing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜