如何在运行时拖放和调整面板上的标签大小?c#, winForms

本文关键字:标签 winForms 运行时 拖放 调整 | 更新日期: 2023-09-27 18:15:51

我有这个代码拖动面板,但它没有做的事情。我必须选择是拖放还是调整大小。我认为我的代码有问题在这里的形式加载。总之,我这里有5个标签和一个名为label1, label2, label3, label4, label5的面板在panel1。

    private void form_Load(object sender, EventArgs e)
    {
            //for drag and drop           
            //this.panel1.AllowDrop = true; // or Allow drop in the panel.
            foreach (Control c in this.panel1.Controls)
            {
                c.MouseDown += new MouseEventHandler(c_MouseDown);
            }
            this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
            this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);  
            //end of drag and drop
    }
    void c_MouseDown(object sender, MouseEventArgs e)
    {
        Control c = sender as Control;                       
            c.DoDragDrop(c, DragDropEffects.Move);
    }
    void panel1_DragDrop(object sender, DragEventArgs e)
    {
        Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
        lblResizeAmtWord.Visible = false;
        if (c != null)
        {
            c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
            //this.panel1.Controls.Add(c); //disable if already on the panel
        }
    }
    void panel1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }  

如何在运行时拖放和调整面板上的标签大小?c#, winForms

我使用了我想要移动的控件的MouseDown, Up和Move事件。比如我的控件名称是ctrlToMove

    private Point _Offset = Point.Empty;
    private void ctrlToMove_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _Offset = new Point(e.X, e.Y);
        }
    }
    private void ctrlToMove_MouseUp(object sender, MouseEventArgs e)
    {
        _Offset = Point.Empty;
    }
    private void ctrlToMove_MouseMove(object sender, MouseEventArgs e)
    {
        if (_Offset != Point.Empty)
        {
            Point newlocation = ctrlToMove.Location;
            newlocation.X += e.X - _Offset.X;
            newlocation.Y += e.Y - _Offset.Y;
            ctrlToMove.Location = newlocation;
        }
    }