2010-06-19 100 views

回答

3

你必須有窗口的列表,然後才能瓦他們的一種方式做,這是存儲在WeakReferences創建的所有窗口的靜態屬性,像這樣:

static List<WeakRef> _registeredWindows; 

public void RegisterWindow(Window w) 
{ 
    _registeredWindows.Add(new WeakReference(w)); 
} 

現在很容易平鋪所有可見的註冊窗口:

public void TileRegisteredWindowsHorizontally() 
{ 
    // Find visible registered windows in horizontal order 
    var windows = (
    from weak in _registeredWindows 
    let window = weak.Target as Window 
    where window!=null && window.Visibility==Visibility.Visible 
    orderby window.Left + window.Width/2 // Current horizontal center of window 
    select window 
).ToList(); 

    // Get screen size 
    double screenWidth = SystemParameters.PrimaryScreenWidth; 
    double screenHeight = SystemParameters.PrimaryScrenHeight; 

    // Update window positions to tile them 
    int count = windows.Count(); 
    foreach(int i in Enumerable.Range(0, count)) 
    { 
    var window = windows[i]; 
    window.Left = i * screenWidth/count; 
    window.Width = screenWidth/count; 
    window.Top = 0; 
    window.Height = screenHeight; 
    } 
} 

它是如何工作:第一個查詢掃描WeakRefs可見直播窗口和排序由他們的中心水平。末端的環路調整窗口位置。

+0

我知道這已經7年了,但是有一個特殊的原因,有一個'WeakReference'列表,而不是直接把'Window'列表到列表中? – mcy 2017-09-18 08:10:56