是否可以在窗体中拖动控件时做得像Windows一样,在按下鼠标拖动的时候只显示控件的虚框(原控件位置不变)(效果见下图),待确定位置松开鼠标后,原控件再移动目标位置。

解决方案 »

  1.   

    做了个示例,你自己研究!其实很简单!
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            this.button1.MouseDown += new MouseEventHandler(button1_MouseDown);
                this.button1.MouseUp += new MouseEventHandler(button1_MouseUp);
                this.button1.MouseMove += new MouseEventHandler(button1_MouseMove);
            }        protected int x, y;
            protected bool movingFlag = false;
            void button1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;            movingFlag = true;
                x = e.X;
                y = e.Y;        }        void button1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left) return;            movingFlag = false;
                this.button1.Visible = true;
            }        void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (movingFlag == false) return;            this.button1.Visible = false;
                this.button1.Location = new Point(button1.Location.X + (e.X - x), button1.Location.Y + (e.Y - y));
                this.Invalidate();
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);            if (movingFlag == false) return;
                Graphics g = e.Graphics;
                Pen p = new Pen(SystemColors.Highlight, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;            g.DrawRectangle(p, button1.Location.X, button1.Location.Y, button1.Width, button1.Height);
            }
      /// <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.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 12);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(162, 56);
                this.button1.TabIndex = 0;
                this.button1.Text = "&Drop Me!";
                this.button1.UseVisualStyleBackColor = true;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(528, 460);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
        }
    }