Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position the window's position on startup to the right side of the user's screen?

I am currently creating a sidebar-like WPF application in C#. When a user starts the application, I would like the window to automatically position it's self to the side of the user's screen. I have tried a few methods and google searches, but have not found any help.

Here's an example of what I'm trying to do:

http://prntscr.com/5tfkz

How can I efficiently go about achieving something like this?


@dknaack

I tried this code:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
            this.Top = 0;
            this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;

        }

and got the following errors:

Error 1 The type 'System.Drawing.Size' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 13 WindBar_Prototype_1

and

Error 2 'System.Drawing.Size' does not contain a definition for 'Width' and no extension method 'Width' accepting a first argument of type 'System.Drawing.Size' could be found (are you missing a using directive or an assembly reference?) C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 78 WindBar_Prototype_1

Any help?

like image 535
anonymous Avatar asked Feb 02 '12 22:02

anonymous


2 Answers

Description

You can use Screen from System.Windows.Forms.

So add reference to the System.Windows.Forms.dll and System.Drawing.dll. Then change the Left and Height property in the MainWindow_Loaded method.

Sample

public MainWindow()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
    this.Top = 0;
    this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
}

More Information

  • MSDN - Screen Class
like image 190
dknaack Avatar answered Oct 13 '22 17:10

dknaack


You can do this without referencing win forms assemblies by using SystemParameters. In the code behind for your window XAML:

MainWindow() {
    InitializeComponents();

    this.Loaded += new RoutedEventHandler(
      delegate(object sender, RoutedEventArgs args) {
        Width = 300;
        Left = SystemParameters.VirtualScreenLeft;
        Height = SystemParameters.VirtualScreenHeight;
    }
}

SystemParameters documentation

like image 20
KurToMe Avatar answered Oct 13 '22 15:10

KurToMe