2010-04-10 51 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 =什麼