none
c# winform画一个矩形,并且矩形可以拖动,有什么好思路? RRS feed

  • 问题

  • c# winform画一个矩形,并且矩形可以拖动,有什么好思路?

    我现在是这么实现的,添加panel,设置为透明,并设置边界颜色,拖拽panel时,效果类似于拖动矩形。但是,这样做有点卡。 请问有别的好思路吗?

    2014年10月11日 8:18

答案

  • 你好:

    拖拽一个panel应该不会卡吧,比如下面这个拖拽方法在我的机器上就很流畅:

    public partial class MainForm : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Panel panel1;
            bool drag;
            int x, y;
            public MainForm()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.BackColor = System.Drawing.SystemColors.Control;
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.panel1.Location = new System.Drawing.Point(72, 64);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(120, 120);
                this.panel1.TabIndex = 0;
                this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseisUp);
                this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMoves);
                this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseisDown);
                // 
                // MainForm
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.panel1);
                this.Name = "MainForm";
                this.Text = "MainForm";
                this.ResumeLayout(false);
            }
    
            public void MouseisDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    drag = true;
                    x = e.X;
                    y = e.Y;
                }
    
            }
    
            public void MouseisUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                drag = false;
    
            }
    
            public void MouseMoves(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (drag)
                {
                    this.panel1.Location = new Point(e.X + this.panel1.Left - x, e.Y + this.panel1.Top - y);
                }
            }
        }

    还有一个思路就是直接在From上画矩形,当鼠标按下的时候计算鼠标的位置是否在矩形之内,如果在矩形之内的话在鼠标拖动事件中将原先的矩形清除,重新画一个新的矩形。这样效率可能会更高一点。我暂时没有现成的代码,你可以尝试一下。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月13日 2:52
    版主
  • public Form1()
            {
                InitializeComponent();
            }
            static bool _canMove = false;
            private void button1_Click(object sender, EventArgs e)
            {
                _canMove = !_canMove;
                var sen = sender;
                var even = e;
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (!_canMove)
                    return;
                Button btn = button1;
                this.Invoke((MethodInvoker)delegate()
                {
                    if (sender is Button)
                    {
                        label1.Text = string.Format("button:x={0},y={1},\r\nmouse:x={2},y={3}", btn.Location.X, btn.Location.Y, e.Location.X, e.Location.Y);
                        btn.Location = new Point(e.Location.X + btn.Location.X - btn.Size.Width / 2, e.Location.Y + btn.Location.Y - btn.Size.Height / 2);
                        Thread.CurrentThread.Join(100);
                    }
                    else
                    {
                        btn.Location = new Point(e.Location.X - btn.Size.Width / 2, e.Location.Y - btn.Size.Height / 2);
    
                    }
                });
            }
    
    /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.label1 = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(91, 64);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(260, 189);
                this.button1.TabIndex = 0;
                this.button1.Text = "按钮";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 9);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(41, 12);
                this.label1.TabIndex = 1;
                this.label1.Text = "label1";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(598, 372);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label1;

    我做了,当你点击按钮时,按钮会自动跟着你的鼠标动,

    差不多意思,你改成放个img,当你鼠标click的时候出发,创建个flag变量,判断时候需要执行移动

    希望对你有帮助

    2014年10月16日 3:20

全部回复

  • 你好:

    拖拽一个panel应该不会卡吧,比如下面这个拖拽方法在我的机器上就很流畅:

    public partial class MainForm : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Panel panel1;
            bool drag;
            int x, y;
            public MainForm()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.BackColor = System.Drawing.SystemColors.Control;
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.panel1.Location = new System.Drawing.Point(72, 64);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(120, 120);
                this.panel1.TabIndex = 0;
                this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseisUp);
                this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMoves);
                this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseisDown);
                // 
                // MainForm
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.panel1);
                this.Name = "MainForm";
                this.Text = "MainForm";
                this.ResumeLayout(false);
            }
    
            public void MouseisDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    drag = true;
                    x = e.X;
                    y = e.Y;
                }
    
            }
    
            public void MouseisUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                drag = false;
    
            }
    
            public void MouseMoves(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (drag)
                {
                    this.panel1.Location = new Point(e.X + this.panel1.Left - x, e.Y + this.panel1.Top - y);
                }
            }
        }

    还有一个思路就是直接在From上画矩形,当鼠标按下的时候计算鼠标的位置是否在矩形之内,如果在矩形之内的话在鼠标拖动事件中将原先的矩形清除,重新画一个新的矩形。这样效率可能会更高一点。我暂时没有现成的代码,你可以尝试一下。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月13日 2:52
    版主
  • public Form1()
            {
                InitializeComponent();
            }
            static bool _canMove = false;
            private void button1_Click(object sender, EventArgs e)
            {
                _canMove = !_canMove;
                var sen = sender;
                var even = e;
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (!_canMove)
                    return;
                Button btn = button1;
                this.Invoke((MethodInvoker)delegate()
                {
                    if (sender is Button)
                    {
                        label1.Text = string.Format("button:x={0},y={1},\r\nmouse:x={2},y={3}", btn.Location.X, btn.Location.Y, e.Location.X, e.Location.Y);
                        btn.Location = new Point(e.Location.X + btn.Location.X - btn.Size.Width / 2, e.Location.Y + btn.Location.Y - btn.Size.Height / 2);
                        Thread.CurrentThread.Join(100);
                    }
                    else
                    {
                        btn.Location = new Point(e.Location.X - btn.Size.Width / 2, e.Location.Y - btn.Size.Height / 2);
    
                    }
                });
            }
    
    /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.label1 = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(91, 64);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(260, 189);
                this.button1.TabIndex = 0;
                this.button1.Text = "按钮";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                this.button1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 9);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(41, 12);
                this.label1.TabIndex = 1;
                this.label1.Text = "label1";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(598, 372);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label1;

    我做了,当你点击按钮时,按钮会自动跟着你的鼠标动,

    差不多意思,你改成放个img,当你鼠标click的时候出发,创建个flag变量,判断时候需要执行移动

    希望对你有帮助

    2014年10月16日 3:20