2010-04-10 52 views
1

我将永远在Windows 7上运行一个WPF应用程序,它会打开并显示在全屏模式WPF和窗口平铺

有WPF应用程序中的一个按钮,打开的Word

我会就像WPF应用程序打开Word向操作系统发送命令一样,这样它就可以将WPF应用程序和Word应用程序窗口并排放置,从而每个屏幕占用50%的屏幕。这可能吗?如果是的话,谁会知道代码可能是什么? (有点像当您在Windows右键单击7任务栏,然后单击显示窗口并排)

回答

1

是的,它是可能的,并且相对容易。

你说你已经有了能够打开你的WPF窗口“全屏”的代码。假设这是一个真正的“全屏”视图,而不是“最大化”窗口,那么您已经计算了屏幕矩形。如果没有,您可以使用System.Windows.Forms中的Screen类来获取屏幕矩形。请记住Screen类是WinForms的一部分,因此它使用设备坐标(像素)而不是独立于分辨率的坐标(96dpi),因此您需要将矩形转换为矩形,然后应用PresentationSource.FromVisual(window).TransformFromDevice转换来获取WPF坐标。

一旦你在设备无关(96dpi)坐标屏幕矩形,你必须计算单独的矩形。例如这里是screenRect 50%的水平分工的计算:

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

一旦你的边界,你可以调整自己的窗口足够简单:

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

移动Word窗口时略多复杂。您必须转换为设备坐标,启动进程,等待Word初始化,然后调用Win32的SetWindowPos函数。这必须在单独的线程上完成以避免阻塞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); 
}); 

不要忘了DllImportSetWindowPos。像这样的东西应该工作:

[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); 
0

这里是一个变通方法是:

 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(); 

希望它可以帮助!

+0

这将不利于设置Word应用程序窗口的位置,因为它不是一个WPF窗口。 – 2010-04-11 06:51:22

0

,我尝试了另一种方法是调用WPF应用程序中启动的exe程序后VBS文件,VBS文件将发送指令给系统垂直平铺等

暗淡objShell 集objShell =的CreateObject(“Shell.Application”) objShell.TileVertically 集objShell =什么