Tile desktop windows in rows and columns in C#

[desktop windows]

The example List desktop windows in C# shows how to list the currently running desktop windows. This example uses that technique to fill a ListBox with the titles of those desktop windows. (See the previous example to see how that part works.) If you select some of them and click Arrange, this example arranges them in rows and columns on the screen.

The program stores information about each desktop window in the following WindowInfo structure.

// Display a window's title, saving its handle.
private struct WindowInfo
    public string Title;
    public IntPtr Handle;

    public WindowInfo(string title, IntPtr handle)
        Title = title;
        Handle = handle;

    // Display the title.
    public override string ToString()
        return Title;

This structure simply stores a desktop window’s handle and title. The structure overrides its ToString method to return the title so the ListBox displays the title.

The following code shows how the program arranges the desktop windows.

// Arrange the selected controls.
private void btnArrange_Click(object sender, EventArgs e)
    if (lstWindows.SelectedItems.Count == 0) return;

    // Get the form's location and dimensions.
    int screen_top = Screen.PrimaryScreen.WorkingArea.Top;
    int screen_left = Screen.PrimaryScreen.WorkingArea.Left;
    int screen_width = Screen.PrimaryScreen.WorkingArea.Width;
    int screen_height = Screen.PrimaryScreen.WorkingArea.Height;

    // See how big the windows should be.
    int window_width = (int)(screen_width / nudCols.Value);
    int window_height = (int)(screen_height / nudRows.Value);

    // Position the windows.
    int window_num = 0;
    int y = screen_top;
    for (int row = 0; row < nudRows.Value; row++)
        int x = screen_left;
        for (int col = 0; col < nudCols.Value; col++)
            // Restore the window.
            WindowInfo window_info =

            // Position window window_num;
                x, y, window_width, window_height);

            // If that was the last window, return.
            if (++window_num >= lstWindows.SelectedItems.Count)
            x += window_width;
        y += window_height;

This code first gets the screen’s working area–the area not including the task bar. It then divides the screen’s width and height by the desired number of columns and rows to see how big it should make each window.

Next the program loops through the rows and columns. In each inner iteration, it gets the next item’s WindowInfo structure. It calls the SetWindowPlacement API function to restore the window (so it isn’t maximized or minimized), passing the function the window’s handle. Next the code positions the window at the next (x, y) value and sets its size.

The code then increases x to get ready for the next window in the row. If this is the last window in the row, the code increases y to get ready for the next row.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in API, miscellany, system and tagged , , , , , , , , , , , . Bookmark the permalink.