天天看點

Winform開發遇到的問題之拖拽控件

1.代碼塊:

//記錄滑鼠拖放
        bool bMouseDown;
        Point po = Point.Empty;
        //生成的控件
        Label lbl = null;
        TextBox txt = null;
        /// <summary>
        /// 窗體加載
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AreaPicQuery_Load(object sender, EventArgs e)
        {
            CreateControls();
        }

        /// <summary>
        /// 預設容器屬性
        /// </summary>
        private void AreaShow()
        {
            // 預設為 false,即不接受使用者拖動到其上的控件
            //this.gbName.AllowDrop = true;
            this.gbConfig.AllowDrop = true;
            // 拖動對象進入控件邊界時觸發
            this.gbName.DragEnter += new DragEventHandler(groupBox_DragEnter);
            this.gbConfig.DragEnter += new DragEventHandler(groupBox_DragEnter);
            // 完成拖動時觸發
            this.gbName.DragDrop += new DragEventHandler(groupBox_DragDrop);
            this.gbConfig.DragDrop += new DragEventHandler(groupBox_DragDrop);

        }

        /// <summary>
        /// 區域名稱初始化
        /// </summary>
        private void CreateControls()
        {
            int x = ;
            int y = ;
            list = rsqm.AreaConfigInfo();
            for (int i = ; i < list.Count; i++)
            {
                string lbltext = list[i].AreaName;
                string lblname = "lbl" + list[i].SystemID;
                lbl = new Label();
                lbl.Left = x;
                lbl.Top = y;
                lbl.Font = new Font("楷書", , FontStyle.Bold);
                lbl.Name = lblname;
                lbl.Text = lbltext;
                lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                lbl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                x += lbl.Width + ;
                if (lbl.Width > gbName.Width - x)
                {
                    x = ;
                    y += lbl.Height + ;
                }                            
                lbl.AllowDrop = false; // 預設為 false,即不可拖動
                lbl.MouseDown += new MouseEventHandler(lbl_MouseDown);
                lbl.MouseUp += new MouseEventHandler(lbl_MouseUp);
                this.gbName.Controls.Add(lbl);
            }
        }


        /// <summary>
        /// 滑鼠拖動控件到容器邊界觸發
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupBox_DragEnter(object sender, DragEventArgs e)
        {
            //定義拖動類型
            e.Effect = DragDropEffects.Copy;
            bMouseDown = true;
        }
        /// <summary>
        /// 滑鼠控件進入另外的容器内
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupBox_DragDrop(object sender, DragEventArgs e)
        {
            if (bMouseDown)
            {
                //從事件參數 DragEventArgs 中擷取被拖動的元素
                lbl = (Label)e.Data.GetData(typeof(Label));
                GroupBox grp = (GroupBox)lbl.Parent;
                //grp.Controls.Remove(lbl);
                ((GroupBox)sender).Controls.Add(lbl);
                bMouseDown = false;
            }
        }

        /// <summary>
        /// 滑鼠拖動控件操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            lbl = (Label)sender;
        }

        private void lbl_MouseUp(object sender, MouseEventArgs e)
        {
            lbl.Location = getPointToForm(new Point(e.Location.X - gbConfig.Location.X, e.Location.Y));
            lbl.DoDragDrop(lbl, DragDropEffects.Copy);           
        }

        //把相對與control控件的坐标,轉換成相對于窗體的坐标。
        private Point getPointToForm(Point p)
        {
            return this.PointToClient(lbl.PointToScreen(p));
        }
           

2.效果圖:

Winform開發遇到的問題之拖拽控件

繼續閱讀