【问题标题】:Borderless Form, Drag to top to maximize like a normal form or drag to side to split screen无边框表单,拖动到顶部以像普通表单一样最大化或拖动到侧面以分屏
【发布时间】:2020-08-21 09:55:25
【问题描述】:

不确定我是否在标题中解释得很好;

如果您在 Windows 上,单击并拖动 FireFox/Chrome/其他应用程序的标题栏并将其拖动到屏幕的顶部或侧面。它会产生一种奇怪的效果,当你释放捕获时,根据你拖动它的位置,它会最大化或分割屏幕。

当我使用App.FormBorderStyle = FormBorderStyle.None; 时,当我将它拖到顶部时,它不会给我拆分/最大效果。我已启用拖动:

public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    
    // this is the panel i use as a title bar for dragging
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

现在,我怎样才能启用拖动到屏幕顶部并让窗口显示最大/拆分效果的功能。

谢谢。

【问题讨论】:

  • 你仍然应该在 lParam 中传入 X / Y 位置。不知道这是否会解决它。

标签: c# visual-studio winforms


【解决方案1】:

也许您可以尝试通过根据 Location.X 和 Location.Y 确定表单的位置来实现它们。

这是一个你可以参考的演示。

public Form1()
{
    InitializeComponent();
    this.FormBorderStyle = FormBorderStyle.None;
}

// Save original size
Size original;

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

        // Drag to top to maximize
        if (this.Location.Y <= 0)
        {
            this.WindowState = FormWindowState.Maximized;
        }
        else
        {
            this.Size = original;
        }

        // Drag to side to split screen
        if (this.Location.X <= 0)
        {
            this.Location = new Point(0, 0);
            this.Size = new Size(SystemInformation.WorkingArea.Width / 2, SystemInformation.WorkingArea.Height);
        }
        else if (this.Location.X >= SystemInformation.WorkingArea.Width / 2)
        {
            this.Location = new Point(SystemInformation.WorkingArea.Width / 2, 0);
            this.Size = new Size(SystemInformation.WorkingArea.Width / 2, SystemInformation.WorkingArea.Height);
        }
        else
        {
            this.Size = original;
        }
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    original = this.Size;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多