这个
public Form1()
{
InitializeComponent();
Size = new System.Drawing.Size(8, 8);
Console.WriteLine("Size: " + Size);
}
产生这个作为输出:
Size: {Width=178, Height=47}
现在我猜这是因为标题栏及其控件和表单边框需要一些空间。
但是在删除所有这些东西之后,结果仍然是一样的,即使我可以在使用鼠标时将表单缩小到任何更小的东西!
这是我尝试用键盘界面改进的小程序:
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--;
}
}
表单只有一个标签,没有别的:
//
// 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();
我想知道为什么鼠标让我缩小它但代码却不允许? 是否需要设置一些样式位?
最佳答案
我认为这是因为表单边框样式,看起来将其更改为 None 可以让表单缩小。将调整大小代码移动到表单的加载事件也有帮助。
编辑:
将 FormBorderStyle 设置为 SizeableToolWindow
似乎是最合适的,它允许表单非常小并且仍然可以使用鼠标调整大小。
关于c# - 使用鼠标调整表单大小有效,代码不会低于 178x47,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315798/