I have a Windows-based application written in C#..
I'd like to set the size and location of the form unchangeable.
To do this first I got rid of the control box
this.ControlBox = false;
Then I defined the size and location of the form to maximum.
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
The problem is: if I double-clik the title bar of the application, its FormWindowState goes back to Normal.
To work around this I created the following handler.
private void MainForm_Resize(object sender, EventArgs e) { this.Resize -= MainForm_Resize;
this.WindowState = FormWindowState.Maximized;
this.Resize += MainForm_Resize; }
The problem is: the application blinks, for a few seconds we can see the size and location of the application changing and then changing back.
I tried hiding the title bar entirely by setting
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
but then the task bar disappears! I'd like to have the task bar visible.
Is there any simpler way to not allow the user to change the size and location of the form??
Thanks.
|
| hisao0 Tuesday, February 03, 2009 5:40 AM |
Windows tries very hard to prevent you from doing something so irresponsible. It's not perfect, but close:
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Location = Point.Empty; this.StartPosition = FormStartPosition.Manual; this.WindowState = FormWindowState.Maximized; this.MinimumSize = Screen.PrimaryScreen.Bounds.Size; } }
Hans Passant.- Marked As Answer byhisao0 Wednesday, February 04, 2009 4:26 AM
-
|
| nobugz Tuesday, February 03, 2009 10:35 AM |
you can use this
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
to prevent a form from resizing. - Proposed As Answer byTushar Capoor Tuesday, February 03, 2009 6:36 AM
- Edited by_asgar Tuesday, February 03, 2009 6:29 AMaddl
-
|
| _asgar Tuesday, February 03, 2009 6:28 AM |
Hello, asgar! Thanks for replying. I tried what you said, but the problem is when I double-click the title bar (the upper bar )of the application. The size and location changes because FormWindowState goes back to Normal. I want the size and location of the application fixed when the FormWindowState is maximized. I don't know the size and location at design time, because these values are dependent on the client machine. I tried to read these values at Load form
private void MainForm_Load(object sender, EventArgs e) { this.rec = new Rectangle( this.DesktopBounds.X, this.DesktopBounds.Y, this.DesktopBounds.Width, this.DesktopBounds.Height); } And then recover these values every time the user tried to resize
private void MainForm_Resize(object sender, EventArgs e) { this.Resize -= MainForm_Resize;
if (!this.rec.IsEmpty) this.DesktopBounds = new Rectangle( this.rec.X, this.rec.Y, this.rec.Width, this.rec.Height);
this.Resize += MainForm_Resize; }
But it is not working. For some reasons only the location recovers, not the size.
|
| hisao0 Tuesday, February 03, 2009 7:14 AM |
protected override void OnResize(EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
base.OnResize(e);
}
I need a job! |
| cablehead Tuesday, February 03, 2009 7:22 AM |
Hello, cablehead!
Thanks for replying.
Your way is quite nice and better than my handler.
But I still have the problem of blinking. I can see the form changing size and changing back, everytime I double-click the title bar.
I was thinking of some way to disable the double-click on the title bar.
|
| hisao0 Tuesday, February 03, 2009 7:49 AM |
Windows tries very hard to prevent you from doing something so irresponsible. It's not perfect, but close:
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Location = Point.Empty; this.StartPosition = FormStartPosition.Manual; this.WindowState = FormWindowState.Maximized; this.MinimumSize = Screen.PrimaryScreen.Bounds.Size; } }
Hans Passant.- Marked As Answer byhisao0 Wednesday, February 04, 2009 4:26 AM
-
|
| nobugz Tuesday, February 03, 2009 10:35 AM |
Hello, nobugz!
Thanks for the reply.
Your code worked fine, except when I double-click the title bar. The application WindowState goes back to normal. But using the code of cablehead, it returns to maximized.
I am sorry if you feel this is irresponsible. I am just doing what my boss told me, and he in turn is doing what the client is asking.
I think the idea is to make the software interface as simple as possible. The end-users are field workers not used to operating computers. |
| hisao0 Wednesday, February 04, 2009 4:13 AM |
Public Sub New() InitializeComponent() Me.Location = Point.Empty Me.StartPosition = FormStartPosition.Manual Me.Size = Screen.PrimaryScreen.Bounds.Size Me.MinimumSize = Screen.PrimaryScreen.Bounds.Size Me.MaximumSize = Screen.PrimaryScreen.Bounds.Size Me.MaximizeBox = False End Sub
- Proposed As Answer byKomplexoR Wednesday, February 04, 2009 5:03 PM
-
|
| _asgar Wednesday, February 04, 2009 2:36 PM |
With the code below you can keep the MinBox, MaxBox..etc. Keeps it Maximized..
private const int WM_SYSCOMMAND = 0x0112;
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCLBUTTONDBLCLK:
return;
case WM_SYSCOMMAND:
if ((int)m.WParam == 61728 && this.WindowState != FormWindowState.Minimized)
return;
break;
}
base.WndProc(ref m);
}
I need a job! |
| cablehead Wednesday, February 04, 2009 7:28 PM |
Just a thought, how about you fix the maximum and minimum size of the Form. Setting the minimum equal to maximum will not return the form to it's normal state when clicked.
Not sure whether if this will work for you. |
| Xaria Thursday, February 05, 2009 3:52 AM |
Thanks everybody for the replies.
I found the following solution:
private void MainForm_LocationChanged(object sender, EventArgs e) { this.LocationChanged -= MainForm_LocationChanged; this.WindowState = FormWindowState.Normal; this.Location = Point.Empty; this.WindowState = FormWindowState.Maximized; this.LocationChanged += MainForm_LocationChanged; }
I still can see the form moving andthen moving back everytime I double-click the title bar, or drag the title bar, but at least it moves back to the right location with the right size.
|
| hisao0 Tuesday, February 10, 2009 5:46 AM |