private int startX;
        private int startY;   
private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                startX = e.X;
                startY = e.Y;
            }
        }        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            this.Left += e.X - startX;
            this.Top += e.Y - startY;
        }为什么这样子是不对的呢?

解决方案 »

  1.   

    http://www.google.com.hk/search?hl=zh-CN&q=wpf+dragmove&hl=zh-CN&sourceid=cndr
      

  2.   

    事件加到窗体上private int startX;
    private int startY;   
    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            startX = e.X;
            startY = e.Y;
        }
    }private void Form_MouseMove(object sender, MouseEventArgs e)
    {
        if (startX != 0)
        {
            this.Left += e.X - startX;
            this.Top += e.Y - startY;
        }
    }private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        startX = 0;
    }
      

  3.   

    除了添个button,写上代码之外,还要设置什么吗?真是奇怪了。。为什么一样的代码我就不能实现效果呢???我运行书的随书代码可以实现,一模一样地复制到别处再运行就不行。。这到底是为什么呢?
      

  4.   

    我晕,搞那么复杂干嘛,
    this.Left += 100;
    这就是你想表达的意思吧
      

  5.   

    你不是要移动窗体吗?要Button干嘛?public Form1()
    {
        //这个位置能找到不?
        InitializeComponent();    this.MouseDown += Form_MouseDown;
        this.MouseMove += Form_MouseMove;
        this.MouseUp += Form_MouseUp;
      

  6.   


    protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case 0x84:
                        base.WndProc(ref m);
                        if ((int)m.Result == 0x1)
                            m.Result = (IntPtr)0x2;
                        return;
                }
                base.WndProc(ref m);
            }
      

  7.   


    const int HTLEFT = 10;
    const int HTRIGHT = 11;
    const int HTTOP = 12;
    const int HTTOPLEFT = 13;
    const int HTTOPRIGHT = 14;
    const int HTBOTTOM = 15;
    const int HTBOTTOMLEFT = 0x10;
    const int HTBOTTOMRIGHT = 17;protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x0084:
                base.WndProc(ref m);
                Point vPoint = new Point((int)m.LParam & 0xFFFF,
                    (int)m.LParam >> 16 & 0xFFFF);
                vPoint = PointToClient(vPoint);
                if (vPoint.X <= 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPLEFT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMLEFT;
                    else m.Result = (IntPtr)HTLEFT;
                else if (vPoint.X >= ClientSize.Width - 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPRIGHT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMRIGHT;
                    else m.Result = (IntPtr)HTRIGHT;
                else if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOP;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)HTBOTTOM;
                break;
            case 0x0201://鼠标左键按下的消息 
                m.Msg = 0x00A1;//更改消息为非客户区按下鼠标 
                m.LParam = IntPtr.Zero;//默认值 
                m.WParam = new IntPtr(2);//鼠标放在标题栏内 
                base.WndProc(ref m);
                break; 
            default:
                base.WndProc(ref m);
                break;
        }
    }此段代码拷贝到窗体代码空位处,即可实现拖动和拉伸无边框窗体的功能。超强超短。
      

  8.   

    真是不查不问不知道,一查一问吓一跳,各位的方法真多啊~
    真是得好好学学的呢。
    最后采用lyx朋友的方法,谢谢各位~各种方法的实现值得好好学习一下~