代码之家  ›  专栏  ›  技术社区  ›  Ravi shankar

是否在C夏普中调整自定义窗体(具有放置阴影效果)和鼠标拖动事件控件的大小?

  •  8
  • Ravi shankar  · 技术社区  · 15 年前

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

    提前谢谢

    4 回复  |  直到 15 年前
        1
  •  2
  •   Don Kirkby    15 年前

        2
  •  1
  •   Don Kirkby    15 年前

    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;
        }
    }
    
        3
  •  0
  •   Matthew Ferreira    15 年前

    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);
        }
    }
    

        4
  •  0
  •   IndrekV    11 年前

    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);
            }
        }
    }