使用鼠标调整表单大小有效,代码不会低于 178x47

Sizing Form with Mouse works, with code doesn't go below 178x47

这个

public Form1()
{
    InitializeComponent();
    Size = new System.Drawing.Size(8, 8);
    Console.WriteLine("Size: " + Size);
}

将其作为输出:

Size: {Width=178, Height=47}

现在我猜这是因为标题栏及其控件和表单边框需要一些 space。

但是在删除所有这些东西之后,结果仍然是一样的,即使我可以在使用鼠标时将表单缩小到任何更小的东西!

这是我尝试用键盘界面改进的小程序:

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 to SizeableToolWindow 似乎是最合适的,它允许表单非常小并且仍然可以使用鼠标调整大小。

Form 覆盖 CreateParams 并设置 WS_POPUP 样式允许为 Form 设置任何大小,以防 FormBorderStyle 设置为 FormBorderStyle.None.否则,将添加边框和标题的大小。

uint WS_POPUP = 0x80000000;

protected override CreateParams CreateParams
{
    get
    {
        CreateParams createParams = base.CreateParams;
        createParams.Style |= unchecked((int)WS_POPUP);
        return createParams;
    }
}