14

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?

6 Answers 6

17

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

9
  • @JamesDidzun Did you referenced System.Drawing.dll too ?
    – dknaack
    Commented Feb 2, 2012 at 23:03
  • Okay, I just tried that, and I didn't have any trouble building it anymore. The only problem is, it's not on the side of the screen! Look: prntscr.com/5th4q - Thank again
    – anonymous
    Commented Feb 2, 2012 at 23:05
  • Looks like the event gets not fired. You can set a breakpoint to see if the method get called. Check out my updated answer this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    – dknaack
    Commented Feb 2, 2012 at 23:07
  • Actually, I figured out the problem. I had some empty space in the window... Haha. Thanks for your help! -Best Answer-
    – anonymous
    Commented Feb 2, 2012 at 23:08
  • One more thing... How can I get the side bar on to the right side instead of the left?
    – anonymous
    Commented Feb 2, 2012 at 23:16
5

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

2
  • System.Windows.Forms.Screen.PrimaryScreen is better for the situations you have more than 1 screen
    – MikroDel
    Commented Dec 12, 2012 at 12:33
  • How is it better MikroDel? I'm using a tripple monitor setup, two side by side, and the third one is above the two right in the middle. With SystemParameters I actually got everything I needed. Commented Feb 17, 2016 at 14:53
2

in your xaml :

WindowStartupLocation="Manual" 

in the constructor :

 Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
 Top=0
0
1
public MainWindow()
{
    InitializeComponent();
    WindowStartupLocation = WindowStartupLocation.Manual;
    Left =  System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}
1
  • WindowStartupLocation = WindowStartupLocation.Manual; is a good hint
    – MikroDel
    Commented Dec 12, 2012 at 12:35
0

use startPosition property or location property

4
  • 1
    The startPosition property does not have a side-of-screen location
    – anonymous
    Commented Feb 2, 2012 at 22:39
  • I can't try the code now, but isn't there in the form's class a this.position property, where you can set custom points ? like: this.position =new Point(x,y) ?
    – yoozz
    Commented Feb 2, 2012 at 22:45
  • @yoz cia The question is about WPF Window. Commented Feb 2, 2012 at 22:50
  • private void SetWindowPosition(){ Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty); Top = 0; }
    – yoozz
    Commented Feb 2, 2012 at 22:59
-1
<body>
<script>
function myfunc()
{
    w1=window.open('http://www.google.com','Google','left=0,top=0,width=250px,height=250px');
    w2=window.open('http://www.yahoomail.com','Yahoo Mail','left=1166,top=0,width=250px,height=250px');
    w3=window.open('http://www.people.com','People Magazine','left=1166,top=500,width=250px,height=250px');
    w4=window.open('http://www.time.com','Time Magazines','left=0,top=500,width=250px,height=250px');
    w5=window.open('http://www.ew.com','Entertainment Weekly','left=550,top=250,width=250px,height=250px');

}

function myclose()
{
 w1.close(); w2.close(); w3.close(); w4.close(); w5.close();
 w6=window.open('smartwindow.html',' mywindow',',');
}
</script>
    <div id="cover">
    <img src="images/abstract.jpeg" id="1" width="500px" height="400px" alt="color defined"onClick="myfunc()"/>
     <input type="button" value="click to close all windows" onClick="myclose()"/>
     </div>
</body>
1
  • 1
    Huh? This is HTML, not XAML/WPF/C#. You need to read the question, not just the title. Commented Jul 21, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.