因为要建立很多的控件,所以就把功能做在控件的内部了。
我想关键就是,MouseMove那句
这里的版面有点乱,如果copy到.Net里就好看点了。

解决方案 »

  1.   

    这样实现好像不大好吧。如果MyTextBox里面有一堆文本,我想用鼠标选取其中的一段,就没办法做到了!
      

  2.   

    以前写了个用在Form上using System;
    using System.Windows.Forms;
    using System.Drawing;
    namespace NetFishApplication
    {
        class MyForm : Form
        {
            // 鼠标位移记录器
            private MouseOffsetRecorder mr = new MouseOffsetRecorder();    
            // 可拖动标志
            private bool boolFormDrag = false;
            public MyForm()
            {
                // 设置窗体样式
                this.AutoScale = false;
                this.BackColor = System.Drawing.Color.Red;
                this.ClientSize = new System.Drawing.Size(140, 50);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                
                // 事件关联
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseUp);
                this.DoubleClick += new System.EventHandler(this.MyForm_DBClick);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseMove);
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyForm_OnMouseDown);
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyForm_OnPaint);
            }
            static void Main()
            {
                Application.Run(new MyForm());
            }
            
            private void MyForm_OnMouseDown(object sender,    MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    this.boolFormDrag = true;
                    mr.Record();
                }
            }
            private void MyForm_OnMouseMove(object sender,    MouseEventArgs e)
            {
                if ( this.boolFormDrag == true)
                {
                    this.Location = new Point(this.Location.X + mr.offsetX, this.Location.Y + mr.offsetY);
                    mr.Record();
                }
            }
            private void MyForm_OnMouseUp(object sender,    MouseEventArgs e)
            {
                this.boolFormDrag = false;
            }        private void MyForm_DBClick(object sender, System.EventArgs e)
            {
                this.Close();
            }
                
            protected void MyForm_OnPaint(object sender,System.Windows.Forms.PaintEventArgs e)
            {
                Graphics g = e.Graphics;                  
                g.DrawString("Double Click To Exit",this.Font,new SolidBrush(Color.Black),1,20);
            }
        }
        
        /// <summary>
        /// 记录鼠标在两个动作间的位移
        /// </summary>
        public class MouseOffsetRecorder 
        {
            private Point StartPosition = new Point();        // 水平位置上的位移
            public int offsetX
            {
                get
                {
                    return this.GetCurrentPosition().X - this.StartPosition.X;
                }
            }
            // 垂直位置上的位移
            public int offsetY
            {
                get
                {
                    return this.GetCurrentPosition().Y - this.StartPosition.Y;
                }
            }
            
            public void Record()
            {
                this.StartPosition = this.GetCurrentPosition();
            }
            protected Point GetCurrentPosition()
            {
                return  new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
            }
        }
    }
      

  3.   

    感谢您使用微软产品。根据您提供的代码,进行适当的修改,可以达到您需要的效果,没有出现“拖动时乱动”的现象。其中,修改部分用特别的注释//标示。如下是修改好后的代码,供您参考:
    namespace MoveControl_Runtime
    {
    /// <summary>
    /// Summary description for MyTextBox.
    /// </summary>
    public class MyTextBox : System.Windows.Forms.TextBox
    {
    private bool bGetFocus=false;
    private bool bDown=false;
    //记录鼠标开始位置
    private Point mousePos; public MyTextBox()
    {
    this.GotFocus += new System.EventHandler(this.TextBoxGetFocus);
    this.LostFocus += new System.EventHandler(this.TextBoxLostFocus);
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownTextBox);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpTextBox);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMoveTextBox);
    } private void TextBoxGetFocus(object sender, System.EventArgs e)
    {
    this.bGetFocus=true;
    } private void TextBoxLostFocus(object sender, System.EventArgs e)
    {
    this.bGetFocus=false;
    }

    private void MouseDownTextBox(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.bDown=true;
    // 记录鼠标开始位置
    mousePos = new Point(e.X, e.Y);
    } private void MouseUpTextBox(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.bDown=false;
    }

    private void MouseMoveTextBox(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(this.bGetFocus==true)
    {
    if(this.bDown==true)
    {
    // 根据鼠标的移动,实际移动控件
    int diffx,diffy;
    diffx =  mousePos.X - e.X;
    diffy =  mousePos.Y - e.Y;
    this.Location = new Point(this.Location.X-diffx,this.Location.Y-diffy);
    }
    }
    }
    }
    } — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  4.   

    To: lzh4481(卷土重来):我的这个项目没有你说的那种情况,不过很谢谢你提起:)
    To: whxbb(whxbb):谢谢你的代码,感觉比较对症呢:),thank you!
    To: acptvb(微软全球技术中心 VB技术支持):太感谢你了,我在家试了一下,实在是太棒了,我都得到你们这个团对的很多人的帮助了,真不知道什么时候才能和你们一样棒:)