Reputation: 54433
This
public Form1()
{
InitializeComponent();
Size = new System.Drawing.Size(8, 8);
Console.WriteLine("Size: " + Size);
}
produces this as the output:
Size: {Width=178, Height=47}
Now I guess that is because some space is needed for the title bar and its controls and the form border.
But after removing all these things the result still is the same, even though I can reduce the Form to anything smaller when using the Mouse!
Here is the tiny program I tried to improve with a keyboard interface:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MinimumSize = new System.Drawing.Size(6, 6);
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
label1.Text = Width + " x " + Height + " px";
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right && e.Shift) Width++;
else if (e.KeyCode == Keys.Down && e.Shift) Height++;
else if (e.KeyCode == Keys.Left && e.Shift) Width--;
else if (e.KeyCode == Keys.Up && e.Shift) Height--;
else if (e.KeyCode == Keys.Right) Left++;
else if (e.KeyCode == Keys.Down) Top++;
else if (e.KeyCode == Keys.Left) Left--;
else if (e.KeyCode == Keys.Up) Top--;
}
}
The Form has one Label and nothing else:
//
// Form1
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(282, 253);
this.ControlBox = false;
this.Controls.Add(this.label1);
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(2, 2);
this.Name = "Form1";
this.ShowIcon = false;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.ResumeLayout(false);
this.PerformLayout();
I wonder why the mouse lets me shrink it but the code doesn't? Are there some style bits I need to set?
Upvotes: 2
Views: 107
Reputation: 356
I think this is because of the forms border style, looks like changing it to None allows the form to shrink smaller. Also moving the re-sizing code to the form's load event helps.
Edit:
Setting FormBorderStyle to SizeableToolWindow
seems like the best fit, allows the form to be pretty small and still resized using the mouse.
Upvotes: 1
Reputation: 596
Overriding CreateParams
for Form
and setting WS_POPUP
style allows to set any size for Form
in case FormBorderStyle
is set to FormBorderStyle.None
. Otherwise, size of the border and title will be added.
uint WS_POPUP = 0x80000000;
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.Style |= unchecked((int)WS_POPUP);
return createParams;
}
}
Upvotes: 0