在c sharp中调整自定义窗体(具有阴影效果)和鼠标拖动事件控件的大小?

在c sharp中调整自定义窗体(具有阴影效果)和鼠标拖动事件控件的大小?,c,winforms,resize,C,Winforms,Resize,在我的应用程序中,我必须调整窗体的大小及其所有鼠标拖动效果,窗体应该具有阴影效果。问题是,我的所有窗体都是自定义窗体(没有边框) 提前感谢没有边框(或一些控件),您打算如何调整大小?找出该部分,然后在表单中尝试以下代码: public class CustomForm : Form { private const int WmNcLButtonDown = 0xA1; private const int HtBottomRight = 17; [DllImport("u

在我的应用程序中,我必须调整窗体的大小及其所有鼠标拖动效果,窗体应该具有阴影效果。问题是,我的所有窗体都是自定义窗体(没有边框)

提前感谢

没有边框(或一些控件),您打算如何调整大小?找出该部分,然后在表单中尝试以下代码:

public class CustomForm : Form
{
    private const int WmNcLButtonDown = 0xA1;
    private const int HtBottomRight = 17;

    [DllImport("user32.dll")]
    private static extern int ReleaseCapture();

    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

    // elsewhere
    void ResizeForm()
    {
        ReleaseCapture();
        SendMessage(this.Handle, WmNcLButtonDown, HtBottomRight, 0);
    }
}

这段代码将调整窗体的大小,就像使用了右下角一样。查找不同位置的HT_BOTTOMRIGHT和其他HT_常量以调整大小。

我认为您必须自己实现

  • 鼠标按下时开始鼠标拖动时绑定+更改光标以调整图标大小
  • 鼠标拖动时,只需减小窗体大小即可
  • 向上移动鼠标时取消绑定鼠标拖动事件

  • 我建议动态事件绑定的原因是,你可以指定哪个控件或区域应该有鼠标下压

    我不确定下拉阴影效果,但是你应该能够通过在右下角放置一个按钮和一些合适的图标来调整窗体的大小。当用户单击并拖动此按钮时,它会调整窗体的大小。下面是一些示例代码:

    public partial class Form1 : Form
    {
        private int bottomBorder;
        private int rightBorder;
        private Point mouseStart;
        private bool isResizing = false;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isResizing)
            {
                var newLocation = button1.Location;
                newLocation.Offset(
                    e.X - mouseStart.X,
                    e.Y - mouseStart.Y);
                button1.Location = newLocation;
                this.Height = button1.Bottom + bottomBorder;
                this.Width = button1.Right + rightBorder;
                button1.Refresh();
            }
    
        }
    
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            isResizing = true;
            mouseStart = e.Location;
        }
    
        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            isResizing = false;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            bottomBorder = this.Height - button1.Bottom;
            rightBorder = this.Width - button1.Right;
        }
    }
    

    我使用了Don Kirkby和Matthew Ferreira的解决方案,并将两者结合起来创建了自己的解决方案。我添加了一个名为“resizeHandle”的状态条,使其大小为20x20像素,并收听其事件

    public class CustomForm : Form
    {
    private const int WmNcLButtonDown = 0xA1;
    private const int HtBottomRight = 17;
    private const int wmNcLButtonUp = 0xA2;
    private bool isResizing = false;
    
    [DllImport("user32.dll")]
    private static extern int ReleaseCapture();
    
    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
    
    private void resizeHandle_MouseDown(object sender, MouseEventArgs e)
    {
        isResizing = true;
    }
    
    private void resizeHandle_MouseMove(object sender, MouseEventArgs e)
    {
        if (isResizing)
        {
            // Check if we have released the Left mouse button
            isResizing = (e.Button == MouseButtons.Left);
            ReleaseCapture();
            if (isResizing)
            {
                SendMessage(Handle, wmNcLButtonDown, HtBottomRight, 0);
            }
            else
            {
                // Left Mouse button was released, end resizing.
                SendMessage(Handle, wmNcLButtonUp, HtBottomRight, 0);
            }
        }
    }
    

    是的,我几天前也用同样的方式实施了重新调整。但我想我应该选择你的答案,因为你是第一个在这里发帖的人。一个更准确。