简体   繁体   English

我如何在所有区域上拖动无边界表格? 如何调整无边界表格的大小?

[英]How can I drag borderless form on all it's area ? How to resize borderless form?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Draw_Form
{
    public partial class Form1 : Form
    {
        SolidBrush mybrush;

        public Form1()
        {
            InitializeComponent();

            mybrush = new SolidBrush(this.BackColor);

            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        private const int cGrip = 16;      // Grip size
        private const int cCaption = 32;   // Caption bar height;

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
            ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
            rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
            e.Graphics.FillRectangle(mybrush, rc);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x84)
            {  // Trap WM_NCHITTEST
                Point pos = new Point(m.LParam.ToInt32());
                pos = this.PointToClient(pos);
                if (pos.Y < cCaption)
                {
                    m.Result = (IntPtr)2;  // HTCAPTION
                    return;
                }
                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                    return;
                }
            }
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

The problem is that I can drag the form only on it's top small bar area. 问题是我只能在窗体的顶部小条区域上拖动它。 But I want to be able to drag it from every place on the form. 但我希望能够将其从表单上的每个位置拖动。

Second problem I can't rsize the form since he is borderless. 第二个问题,因为他是无国界的,所以我无法调整表格的大小。

And last how can I make that hooking a keys from example Ctrl+F will make new instances and will create each time a new form like this ? 最后,如何使Ctrl + F示例中的键挂接会创建新的实例,并每次都会创建一个新的表单?

This let me drag it from anywhere but now I can't resize the form from the left bottom corner like before: 这使我可以从任何地方拖动它,但是现在我不能像以前一样从左下角调整窗体的大小:

protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }

I have created a fairly simple class for you that will do everything you need. 我为您创建了一个相当简单的类,它将满足您的所有需求。 You can create shortcut keys, resize the form even if it does not have a border, and you can move it by clicking and dragging on any point inside the form. 您可以创建快捷键,即使窗体没有边框,也可以调整窗体的大小,还可以通过在窗体内的任意点上单击并拖动来移动它。 Hope this helps, I did my best to explain the code in the comments. 希望这会有所帮助,我已尽力在注释中解释了代码。 If you need any more clarification just let me know! 如果您需要更多说明,请告诉我!

CustomForm.cs : CustomForm.cs

public class CustomForm : Form
{
    /// <summary>
    /// How close your cursor must be to any of the sides/corners of the form to be able to resize
    /// </summary>
    public int GrabSize = 8;

    /// <summary>
    /// The shortcut keys for this form
    /// </summary>
    public Dictionary<Keys, Action> ShortcutKeys = new Dictionary<Keys, Action>();

    private bool Drag = false;
    private Point DragOrigin;
    private const int HT_LEFT = 10;
    private const int HT_RIGHT = 11;
    private const int HT_TOP = 12;
    private const int HT_BOTTOM = 15;
    private const int HT_TOPLEFT = 13;
    private const int HT_TOPRIGHT = 14;
    private const int HT_BOTTOMLEFT = 16;
    private const int HT_BOTTOMRIGHT = 17;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);

        // If hold left click on the form, then start the dragging operation
        if (e.Button == MouseButtons.Left)
        {
            // Only start dragging operation if cursor position is NOT within the resize regions
            if (e.X > GrabSize && e.X < Width - GrabSize && e.Y > GrabSize && e.Y < Height - GrabSize)
            {
                DragOrigin = e.Location;
                Drag = true;
            }
        }
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);

        // If let go of left click while dragging the form, then stop the dragging operation
        if (e.Button == MouseButtons.Left && Drag)
            Drag = false;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        // Move the form location based on where the cursor has moved relative to where the cursor position was when we first started the dragging operation
        if (Drag)
            Location = new Point(Location.X + (e.Location.X - DragOrigin.X), Location.Y + (e.Location.Y - DragOrigin.Y));
    }

    /// <summary>
    /// Invokes any shortcut keys that have been added to this form
    /// </summary>
    protected override bool ProcessCmdKey(ref Message msg, Keys key)
    {
        Action action;
        if(ShortcutKeys.TryGetValue(key, out action))
        {
            action.Invoke();
            return true;
        }
        return base.ProcessCmdKey(ref msg, key);
    }

    /// <summary>
    /// Handles resizing of a borderless control/winform
    /// </summary>
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if(FormBorderStyle == FormBorderStyle.None && m.Msg == 0x84)
        {
            Point CursorLocation = PointToClient(new Point(m.LParam.ToInt32()));
            if (CursorLocation.X <= GrabSize)
            {
                if (CursorLocation.Y <= GrabSize) // TOP LEFT
                    m.Result = new IntPtr(HT_TOPLEFT);
                else if (CursorLocation.Y >= ClientSize.Height - GrabSize) // BOTTOM LEFT
                    m.Result = new IntPtr(HT_BOTTOMLEFT);
                else
                    m.Result = new IntPtr(HT_LEFT); // RESIZE LEFT
            }
            else if (CursorLocation.X >= ClientSize.Width - GrabSize)
            {
                if (CursorLocation.Y <= GrabSize)
                    m.Result = new IntPtr(HT_TOPRIGHT); // RESIZE TOP RIGHT
                else if (CursorLocation.Y >= ClientSize.Height - GrabSize)
                    m.Result = new IntPtr(HT_BOTTOMRIGHT); // RESIZE BOTTOM RIGHT
                else
                    m.Result = new IntPtr(HT_RIGHT); // RESIZE RIGHT
            }
            else if (CursorLocation.Y <= GrabSize)
                m.Result = new IntPtr(HT_TOP); // RESIZE TOP
            else if (CursorLocation.Y >= ClientSize.Height - GrabSize)
                m.Result = new IntPtr(HT_BOTTOM); // RESIZE BOTTOM
        }
    }
}

To use this class, simply make your forms inherit from CustomForm rather then Form . 要使用此类,只需使您的表单继承自CustomForm而不是Form You can add shortcut keys to the form by adding to the CustomForm's shortcut key dictionary like so: ShortcutKeys.Add(key, action); 您可以通过添加到CustomForm的快捷键字典中来向表单添加快捷键,如下所示: ShortcutKeys.Add(key, action); where key is a System.Windows.Forms.Keys , and action is an Action Delegate . 其中keySystem.Windows.Forms.KeysactionAction委托

In your case, your code would look something similar to: 就您而言,您的代码看起来类似于:

public partial class Form1 : CustomForm
{
    SolidBrush mybrush;
    private const int cGrip = 16;      // Grip size
    private const int cCaption = 32;   // Caption bar height

    public Form1()
    {
        InitializeComponent();

        mybrush = new SolidBrush(this.BackColor);

        // Adds a new shortcut key that will invoke CreateNewForm every time Ctrl+F is pressed
        ShortcutKeys.Add(Keys.Control | Keys.F, CreateNewForm);

        this.FormBorderStyle = FormBorderStyle.None;
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
        rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
        e.Graphics.FillRectangle(mybrush, rc);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    /// <summary>
    /// Create a new instance of this form
    /// </summary>
    private void CreateNewForm()
    {
        new Form1().Show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM