Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Setting the Display for a WPF Application

, 2 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to set the display for a WPF application.
This is an old version of the currently published technical blog.

I'm working on a presenttion project for which I needed to set the display on which a set of WPF applications are running. There's a total of 6 WPF applications that will be running on the same machine at once. Each application is going to be running on a different display. I thought I would share the solution that I used to control which display that each instance of this application went to. 

The first thing I needed to do was to retrieve the information on how how many displays that the computer has and the coordinates of each display. The computer creates a single (logical) display and maps each display device to cover a range of coordinates on this logical display. So I needed to retrieve a list of the displays and the coordinates to which it is mapped. The class available for doing this is in in the System.Windows.Forms library. Since I wasn't making a Windows Forms application I didn't want to add a using directive that would include the entire library; if I did there would be some class names that exists both in this name space and a WPF name space that I was using that could cause some resolution issues. So I only included the single class from the namespace. 

using Screen=System.Windows.Forms.Screen

The Screen class contains a member named AllScreens that contains a collection of Screen objects that give the information on each screen. If you wanted to make a simple WPF program that displayed all of the screens and their positions it only takes a few lines of code. The following is the code for the code-behind and the XAML for such a program. 

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = Screen.AllScreens;
    }
}
<Window x:Class="ScreenTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="100" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding DeviceName}" />
                            <TextBlock Grid.Column="1" Text="{Binding WorkingArea.X}" />
                            <TextBlock Grid.Column="2" Text="{Binding WorkingArea.Y}" />
                            <TextBlock Grid.Column="3" Text="{Binding WorkingArea.Width}" />
                            <TextBlock Grid.Column="4" Text="{Binding WorkingArea.Height}" />
                        </Grid>
                    </StackPanel>
                    
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
 

Screenshot of the interface running

For the programs that I'll be running I've set device name for the program's intended display in a configuration file. The typical display name for the first display is \\.\DISPLAY1. I've made a method that will take the indended display name, try to find it, and set the position and size of the window accordingly.

void SetTargetDisplay()
{
    var targetDeviceName = Settings.Default.DisplayDevice;
    if(!String.IsNullOrEmpty(targetDeviceName))
    {
        // see if the device name specified exists here. It's possible
        // this was configured  to  run on a different machine and the 
        // configured device might not exists
        var screen = (from s in Screen.AllScreens 
             where s.DeviceName.ToLower().Equals(targetDeviceName.ToLower()) 
             select s).FirstOrDefault();
        if (screen != null)
        {
            Left = screen.WorkingArea.Left;
            Top = screen.WorkingArea.Top;
            Width = screen.WorkingArea.Width;
            Height = screen.WorkingArea.Height;
        }
    }
}

If the display doesn't exists (which could happen because of a typographical error or the program having been configured for another machine) then the method will just ignore the request.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.
 
For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.
 
While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.
 
Twitter:@J2iNet
Follow on   Twitter   LinkedIn

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionUsing WinForms classes in WPF PinmemberSeth Morris8-Oct-12 15:16 
GeneralLooks good... PinmvpSandeep Mewara1-Oct-12 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 2 Oct 2012
Article Copyright 2012 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid