winform 移動視窗中的控制項,panel

來源:互聯網
上載者:User

from:http://www.codeproject.com/Articles/31840/Move-controls-on-a-form-at-runtime

thank the author a lot 。

本文是根據codeproject中的一個程式修改的。

可以實現,視窗中的控制項滑鼠拖動。really? yes

code:

一個form 。一個類:

form:

效果:

代碼:

public partial class Frm_MoveControl : Form{private Cls_MoveControl MoveControl;        public Frm_MoveControl(){    InitializeComponent();              MoveControl = new Cls_MoveControl(this);//this代表視窗,或者panel等容器    MoveControl.ApplyMove(button2);    MoveControl.ApplyMove(button3, Cls_MoveControl.MoveDirection.Vertical);    MoveControl.ApplyMove(button1,panel1,Cls_MoveControl.MoveDirection.Any);}}

接下來是用於行動控制項的類:

using System;using System.Collections.Generic;using System.Drawing;using System.Text;using System.Windows.Forms; namespace cacheDemo1{class Cls_MoveControl{public enum MoveDirection{    Any,    Horizontal,    Vertical}private int int_FrmWidth=200;private int int_FrmHeight = 200;public int Int_FrmWidth{    get    {        return int_FrmWidth;    }    set    {        int_FrmWidth = value;    }}public int Int_FrmHeight{    get    {        return int_FrmHeight;    }    set    {        int_FrmHeight = value;    }}public Cls_MoveControl(int width,int height){    this.int_FrmWidth = width;    this.int_FrmHeight = height;}//這裡如果傳進來的只是視窗可以用form。當然也可以用Control類,因為Form也是派上於Control類public Cls_MoveControl(Control frm){    this.int_FrmWidth = frm.ClientRectangle.Width;    this.int_FrmHeight = frm.ClientRectangle.Height;    frm.Resize += delegate(object sender, EventArgs e)    {        this.int_FrmWidth = frm.ClientRectangle.Width;        this.int_FrmHeight = frm.ClientRectangle.Height;    };}/*    * frm.ClientRectangle.Width;這裡用ClientRectangle的原因    * 因為ClientRectangle可以得到視窗可用性區域域的大小,    * 可以去掉視窗的標題列的高度    * 這裡寫Control的好處是,可以傳入Form。也可以穿容器控制項,如panel    */ public void ApplyMove(Control control){    ApplyMove(control, MoveDirection.Any);}public void ApplyMove(Control control, MoveDirection moveDirection){    ApplyMove(control, control, moveDirection);}public void ApplyMove(Control control, Control container, MoveDirection moveDirection){    bool isDrag = false;    Point dragStartPoint = Point.Empty;    control.MouseDown += delegate(object sender, MouseEventArgs e)    {        isDrag = true;        dragStartPoint = new Point(e.X, e.Y);        control.Cursor = Cursors.SizeAll;        control.Capture = true;    };    control.MouseUp += delegate(object sender, MouseEventArgs e)    {        isDrag = false;        control.Cursor = Cursors.Default;        control.Capture = false;    };    control.MouseMove += delegate(object sender, MouseEventArgs e)    {        if (isDrag) //當滑鼠有按下才會為true。一旦滑鼠放開就為false        {            //橫向移動的只能橫向移動。縱向移動的只能縱向移動。            //任意方向的則都可以移動            if (moveDirection!=MoveDirection.Vertical) //horizontal和any可以進入            {                int left=container.Left + e.X - dragStartPoint.X;                // container.Left = Math.Max(0, left); //不要超過左邊界                container.Left = GetMiddleValue(left,container.Width,this.int_FrmWidth);            }            if (moveDirection != MoveDirection.Horizontal)            {                int top = container.Top + e.Y - dragStartPoint.Y;                // container.Top = Math.Max(0, top);                container.Top = GetMiddleValue(top, container.Height, this.int_FrmHeight);                                    }        }    };}/// <summary>/// 控制拖動的控制項不要超出視窗/// </summary>/// <param name="val"></param>/// <param name="containerWOH"></param>/// <param name="FrmWidth"></param>/// <returns></returns>private int GetMiddleValue(int val,int containerWOH,int FrmWOH){    int min = 0;    int max = FrmWOH - containerWOH;    if (val<min)    {        val = min;    }    if (val>max)    {        val = max;    }    Console.WriteLine(val);    return val;}             }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.