Fixing the position of a form - c#

I am starting a winform application[.NET 3.5, C#], where in the the main form of the application starts at a particular specified location. Am calling the following code in the constructor for this
private void SetFormPosition()
{
this.StartPosition = FormStartPosition.Manual;
this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
}
After the application starts, I would like to keep the location of the form fixed throughout the application lifetime.
Perhaps, I could 'tap' the Location event changed but am not sure if that would be very elegant.
Please suggest.
Thanks.

I agree with others that you probably shouldn't be doing this, but if you must, read on.
You can override the SetBoundsCore method and prevent any movement. We use this to prevent vertical resizing on some UserControl implementations (such as those that contain a ComboBox or other fixed height control), but it is also responsible for the location changing.
The following should get you started:
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
x = this.Location.X;
y = this.Location.Y;
//...etc...
base.SetBoundsCore(x, y, width, height, specified);
}

You could set the FormBorderStyle to None. This has the added benefit of removing the bar at the top of the window that would give users a false sense that they should be able to move the window.

Just change this
Location = new Point(this.Width,this.Height);

Related

How to make a form's size to fill all screen at design time?

How can I make a form fill all the screen (in terms of size) when clicked (not
fullscreen like f11). At design - time (Not on code behind)?
If I understand your question correctly, making your form in run time isn't an problem but you want to design also in that form size. You could just set your form's height and width properties according to your resolution. Like for 1366x768, width-1366 height-768.
Yes, Resolution differences will be a major problem and I don't see anything you can do other than building an responsive layout. In that case design size doesn't matter (full screen or not).
At design time: use the WindowsState property defined to Maximized.
At runtime without using this property: you can use this:
static public class FormHelper
{
static public void SetSizeToScreen(this Form form)
{
int left = Screen.PrimaryScreen.Bounds.Left;
int top = Screen.PrimaryScreen.Bounds.Top;
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
form.Location = new Point(left, top);
form.Size = new Size(width, height);
}
static public void SetSizeToDesktop(this Form form)
{
int left = SystemInformation.WorkingArea.Left;
int top = SystemInformation.WorkingArea.Top;
int width = SystemInformation.WorkingArea.Width;
int height = SystemInformation.WorkingArea.Height;
form.Location = new Point(left, top);
form.Size = new Size(width, height);
}
}
Usage:
this.SetSizeToDesktop();

What is the use of SetBoundsCore?

I don't know the usage of the SetBoundsCore and if any sample program it is comfortable.
I have googled but didn't get one. Is it used for retain the same value?
For example, if set the height as 100 for first time it will remain the same and if i set the height as 200 it will not change again.
I assume like this.
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width,height, specified);
}
You can look at MSDN
Basically it sets coordinates and size of your control. Since it is protected and virtual, you can only call it from your user control like you specified in your question.
Difference between public method SetBounds() and SetBoundsCore() is here:
What is the difference between SetBounds and SetBoundsCore

Set window's default location on a user screen

I have a windows Form Application.I want that when the application will run my window will always just on Taskbar and on the Right side means on the bottom-right of a user screen.It doesn't matter what is a user screen resolution is.So how can i do that ???
To build on MeNoMore's answer, to make your form show up in the bottom right corner of the primary screen (the screen with the task bar is usually the primary screen) you would set the following in the constructor of your form.
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width,
Screen.PrimaryScreen.WorkingArea.Height - this.Height);
Other answers will not work. You have to do two things:
Set form's StartPosition to Manual
Set form's Location to what point you want
E.g. inside form's constructor:
StartPosition = FormStartPosition.Manual;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
Screen.PrimaryScreen.WorkingArea.Height - Height);
You can try to use:
Form1.Location = new Point(x, y);
Take a look here
Use Form.StartPosition property
You use the WinTaskBar component for considering other factors on a desktop/screen.
[DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
It will let you control the positions (left & top).
Example:
formStartLeft = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
formStartTop = taskBarHeight + Screen.PrimaryScreen.WorkingArea.Height;

How to determine the screen width/height using C#

I want to set the width & height of a Window dynamically based on the user screens maximum width/height. How can I determine this programmatically?
For the primary screen:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
(Note that there are also some other primary screen related properties which depend on various factors, Full* & Maximised*)
Virtual screen:
SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight
If you want the specific dimensions of the monitor your program is running on (if someone is running more than one monitor) you could also use:
var helper = new WindowInteropHelper(this); //this being the wpf form
var currentScreen = Screen.FromHandle(helper.Handle);
This will return a screen object referencing the monitor the program is running on. From there you can use the currentScreen.Bounds.Width / Height property (for the full size) or the currentScreen.WorkingArea.Width / Height (minus task bar, etc.) depending on what you want.
use Screen Object
Screen.PrimaryScreen.Bounds.Width
I couldn't use any of the solutions above under .NET 4.0.30319.42000 with Windows 10 Enterprise when calling it from the Ranorex Studio 8.0.1+git.8a3e1a6f, so I used the line
using WinForms = System.Windows.Forms;
[…]
SetWindowPos(processes[0].MainWindowHandle,
0,
y,
x,
WinForms.SystemInformation.PrimaryMonitorSize.Width,
WinForms.SystemInformation.PrimaryMonitorSize.Height,
SWP.SHOWWINDOW);
You can use the SizeChanged event
SizeChanged="MyWindow_SizeChanged"
Then in your event handler,
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.MinWidth > 0 && this.MinHeight > 0)
{
double heightScaleFactor = e.NewSize.Height / this.MinHeight;
double widthScaleFactor = e.NewSize.Width / this.MinWidth;
mainGrid.LayoutTransform = new ScaleTransform(heightScaleFactor, widthScaleFactor);
}
}
where MainGrid is a container for all the contents in MyWindow.
You can get the screen height and width:
int height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
int width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
Then set the Window's Height and Width properties to those in the Initialization.
this.Height = height;
this.Width = width;
Works to get the screen's height and width in WinForms or in ASP .NET. No muss, no fuss, except you'll need to reference the System.Windows.Forms assembly in your project if it's not a WinForm project.

Getting initial dimensions from inherited UserControl

I am trying to create a series of UserControls that all inherit from a custom UserControl object. One of the key behaviors I want to implement is the ability to dynamically resize all of the controls to fit the control size.
In order to do this, I need to get the initial width and height of the control to compare it to the resized dimensions. In my inherited controls, I can put code in the constructor after the InitializeComponent() call to grab the dimensions. Is there any way I can do this from the base object code?
Also, if there is a better approach to doing this, I am open to suggestions.
I ended up using my base control's dimensions as the fixed size for all inherited controls. This was fairly easy to do by overriding the SetBoundsCore() method:
public partial class BaseControl : UserControl
{
private int _defaultWidth;
private int _defaultHeight;
public BaseControl()
{
InitializeComponent();
_defaultWidth = this.Width;
_defaultHeight = this.Height;
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
if (this.DesignMode)
{
width = _defaultWidth;
height = _defaultHeight;
}
base.SetBoundsCore(x, y, width, height, specified);
}
}
Any controls inherited BaseControl automatically default to its fixed dimensions.
At runtime, my resize code calculates a resize ratio based on the new Width and Height vs. the _defaultWidth and _defaultHeight members.
In the user control, take advantage of the docking and anchoring properties of every control in the container. When the user control is sized or resized, the contents should adjust themselves automatically. Then in code, all you need to do is set the size of the user control.

Categories