• 0

[.Net - C# & VB] Form in corner of screen?


Question

I'm trying to figure out the best way to do this - I'd like a form to appear at the lower right hand corner or the lower left hand corner of a screen of arbitrary size. Is there an easy way to do this in VB 2005 and/or C#? Thanks.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Look at the System.Windows.Forms.Screen object. Then just subtract the width of the form from the width of the workspace (do the same with the heights) and use that as the form position coordinates.

Link to comment
Share on other sites

  • 0

Alright I figured I'd have to do something like that. That's how I used to do it with SWT in Java, but I figured maybe there was something built into .net for it.

Do you know if there is a way to compensate for the taskbar - like is there a function to get the size and location of the taskbar so I can compensate for it?

Link to comment
Share on other sites

  • 0
Alright I figured I'd have to do something like that. That's how I used to do it with SWT in Java, but I figured maybe there was something built into .net for it.

Do you know if there is a way to compensate for the taskbar - like is there a function to get the size and location of the taskbar so I can compensate for it?

Use the WorkingArea property from the Screen class. It gives the area excluding the task bar, docked windows, and docked tool bars.

Link to comment
Share on other sites

  • 0
I'm trying to figure out the best way to do this - I'd like a form to appear at the lower right hand corner or the lower left hand corner of a screen of arbitrary size. Is there an easy way to do this in VB 2005 and/or C#? Thanks.

For Left:

Me.Left = 0
Me.Top = Screen.PrimaryScreen.Bounds.Height - Me.Height

For Right:

Me.Left = Screen.PrimaryScreen.Bounds.Width - Me.Width
Me.Top = Screen.PrimaryScreen.Bounds.Height - Me.Height

This should work.

Link to comment
Share on other sites

  • 0

Awesome. Combined both ideas and it works great. I ended up doing this...

Me.Left = Screen.PrimaryScreen.WorkingArea.Width - Me.Width

Me.Top = Screen.PrimaryScreen.WorkingArea.Height - Me.Height

The only problem now is if the taskbar is at the top, it leaves a huge gap at the bottom. Does anyone know how to get the taskbar location on the screen?

Link to comment
Share on other sites

  • 0

Ok so I kinda cheated a bit to handle the taskbar location. I was going to dip into the FindWindow API that everyone keeps talking about all over the place then I came up with a better way...

Dim abusedForm As New Form

abusedForm.WindowState = FormWindowState.Maximized

abusedForm.Visible = True

Me.Left = Screen.PrimaryScreen.WorkingArea.Width + abusedForm.Location.X - Me.Width

Me.Top = Screen.PrimaryScreen.WorkingArea.Height + abusedForm.Location.Y - Me.Height

abusedForm.Dispose()

The only thing is now it doesn't take into consideration the window borders so you have a slight buffer to the bottom and to the left of the form. It doesn't bother me, but you'll need some other way to handle this if it bothers you. Also I am not sure if you need the line that says "abusedForm.Visible = True" but you might so if you want to try getting rid of it, by all means go ahead.

Any suggestions or other ways to handle the taskbar location?

Edit... if you remove the abusedform.visible line, you loose the buffer that I mentioned above. This may be more desirable.

Link to comment
Share on other sites

  • 0
Ok so I kinda cheated a bit to handle the taskbar location. I was going to dip into the FindWindow API that everyone keeps talking about all over the place then I came up with a better way...

Dim abusedForm As New Form

abusedForm.WindowState = FormWindowState.Maximized

abusedForm.Visible = True

Me.Left = Screen.PrimaryScreen.WorkingArea.Width + abusedForm.Location.X - Me.Width

Me.Top = Screen.PrimaryScreen.WorkingArea.Height + abusedForm.Location.Y - Me.Height

abusedForm.Dispose()

The only thing is now it doesn't take into consideration the window borders so you have a slight buffer to the bottom and to the left of the form. It doesn't bother me, but you'll need some other way to handle this if it bothers you. Also I am not sure if you need the line that says "abusedForm.Visible = True" but you might so if you want to try getting rid of it, by all means go ahead.

Any suggestions or other ways to handle the taskbar location?

Edit... if you remove the abusedform.visible line, you loose the buffer that I mentioned above. This may be more desirable.

Try setting your form's WindowState to normal, it's StartPosition to Manual, and it's Bounds=Screen.PrimaryScreen.WorkingArea. That's all you should need to do.

' in the Form's constructor
			StartPosition = FormStartPosition.Manual
			WindowState = FormWindowState.Normal
			Bounds = Screen.PrimaryScreen.WorkingArea

Edited by azcodemonkey
Link to comment
Share on other sites

  • 0

codemonkey - if I do that all it does is make my form full screen. Which form did you want me to do that with? The abused one or the main one that I'm actually working with?

Link to comment
Share on other sites

  • 0
codemonkey - if I do that all it does is make my form full screen. Which form did you want me to do that with? The abused one or the main one that I'm actually working with?

Ah, ok. My bad. I thought you were trying to fill the screen minus the taskbar. I forgot to reread this.

			Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Width, 
				Screen.PrimaryScreen.WorkingArea.Bottom - Height)

That should put you in the bottom right corner regardless of where the taskbar is, and account for the taskbar.

Link to comment
Share on other sites

  • 0

No problem. The way it's working now... it's working, but with all things I try to find the least expensive way to do things. Is creating a form just to get its size and start location expensive? I mean at that point it's nothing more than an object without many parameters and it seems to run smoothly, but I'm always looking for the "best way" to do it :)

Link to comment
Share on other sites

  • 0
No problem. The way it's working now... it's working, but with all things I try to find the least expensive way to do things. Is creating a form just to get its size and start location expensive? I mean at that point it's nothing more than an object without many parameters and it seems to run smoothly, but I'm always looking for the "best way" to do it :)

Yeah, creating objects, especially ones that utilize unmanaged resources like window handles, is an expensive operation. The way I showed is very cheap as the Screen is static and global, so there's only ever one instance. So, barring any other examples that have some cool way to achieve this, I'd say setting Location the way I did would be "best". It's only two math operations instead of allocating resources for another form and then destroying that form. As with most things in programming, there're numerous ways to skin a cat.

Link to comment
Share on other sites

  • 0

That works great! Thanks. I learned quite a bit of stuff in this little exercise here about window placement and so forth. Thanks a bunch everyone!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.