WPF Learnings - Drawing on Multiple Monitors

Writespace Some time ago I wrote a fullscreen editing environment add-in for Word to learn some WPF and some Office Ribbon stuff. The editor is called Writespace and is now available om Codeplex as open source.

Scott Hanselman was kind enough to take a few minutes and review the first draft of the editor and told me I should support multiple monitors and that I could look at BabySmash code to see how he did it. Said and done, I downloaded the BabySmash code, dug into the multi-monitor stuff and found out it's not that strange.

Writespace is a fullscreen editor, and when the user press CTRL+M I want to move the whole thing over to the next available monitor. First you may want to do some sanity check that you got more than one monitor available. This is easy enough with something like this:

if (SystemInformation.MonitorCount < 2)

{

    ...only one monitor available...

}

 

The different screens are available via the Screen.AllScreens[] array so once I've picked a screen to draw on I send it as a paramter to a CreateEditor() method:

private static void CreateEditor(Screen screen)

{

    var editor = new TextEditorWindow

                    {

                        WindowStartupLocation = WindowStartupLocation.Manual,

                        Left = screen.WorkingArea.Left,

                        Top = screen.WorkingArea.Top,

                        Width = screen.WorkingArea.Width,

                        Height = screen.WorkingArea.Height

                    };

 

    //setting up other stuff, like events and things here...

 

    editor.Show();

    editor.WindowState = WindowState.Maximized; //do this after Show() or won't draw on secondary screens

    editor.Focus();

}

The setting of WindowState to Maximized after Show() is a trick/workaround for something that seems to be a bug or something I don't grok about WPF in full screen and multiple monitors. The editor window itself has WindowStyle="None" ResizeMode="NoResize" set.

Hope this helps someone.

No Comments