我在VS C# 2008添加了 DotNetBar的RibbonBar 控件,想让界面更美观些,就把窗体改成了无边框的,可是这样就不能移动啦
想通过移动RibbonBar 控件来移动整个窗体
网上找到了很多版本的方法都不行,点在空白窗体处也移动不了
例如 
private Point mouseOffset;
private bool isMouseDown = false;private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    int xOffset;
    int yOffset;    if (e.Button == MouseButtons.Left) 
    {
        xOffset = -e.X /*- SystemInformation.FrameBorderSize.Width*/;
        yOffset = -e.Y /*- SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height*/;
        mouseOffset = new Point(xOffset, yOffset);
        isMouseDown = true;
    }
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (isMouseDown) 
    {
        Point mousePos = Control.MousePosition;
        mousePos.Offset(mouseOffset.X, mouseOffset.Y);
        Location = mousePos;
    }
}private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) 
    {
        isMouseDown = false;
    }
}
另一种
using System.Runtime.InteropServices;[DllImport("user32.dll")]
   public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
   public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
   public const int WM_SYSCOMMAND = 0x0112;
   public const int SC_MOVE = 0xF010;
   public const int HTCAPTION = 0x0002;private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}问题出在哪里啊,这代码应该没问题吖

解决方案 »

  1.   


     [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
      private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                        }
      

  2.   

      private void UserMsgFrm_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);        }
    那怪了我可以移动啊
      

  3.   


    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 

    ReleaseCapture(); 
    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); 
    } 代码是没问题的
    你窗体里面的控件是不是把form1给占用完了?  导致你怎么点也点不到form1上只能点击到别的控件上,建议在窗体上面加一个panel然后用panel_MouseDown事件,这个panel可以设置背景色为透明
      

  4.   

    如果你别的控件把窗体都给占用完了你就点不到form1就触发不了这个事件了,如果你窗体上还有空隙建议你鼠标点那里去试试。那样应该可以拖动。
      

  5.   


    private Point mouseOffset;  
    private bool isMouseDown = false;//这两行全局!        private void Form1_MouseDown(object sender, MouseEventArgs e)//自己在Form1事件里点,然后粘代码!
            {
                int xOffset;
                int yOffset;
      
               if (e.Button == MouseButtons.Left)
              {
                  xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
     
                   yOffset = -e.Y - SystemInformation.CaptionHeight -
      
                      SystemInformation.FrameBorderSize.Height;
     
                  mouseOffset = new Point(xOffset, yOffset);
     
                   isMouseDown = true;
              }        }        private void Form1_MouseMove(object sender, MouseEventArgs e)//自己在Form1事件里点,然后粘代码!        {
                if (isMouseDown)
               {
                   Point mousePos = Control.MousePosition;
                   mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                   Location = mousePos;
              }
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)//自己在Form1事件里点,然后粘代码!        {
                if (e.Button == MouseButtons.Left)
               {
                    isMouseDown = false;
               }        }
    我行的。楼主试试?
      

  6.   

    源码下载 帮我检查下吧http://d.namipan.com/d/18cfd831ea27917be6c2e97e5b2d53556de0a0c5f7270000
      

  7.   

    http://www.cnblogs.com/sufei/archive/2009/05/06/1450683.html 这里是我做的通用的例子,很细节的,有注释,看看吧,算了还是把文章发过来吧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 Invoicing_Tube
    {
        public partial class login : Form
        {
            public login()
            {
                InitializeComponent();
            }
            //定义一下坐标
            private Point mouseOffset;
            //判断一下是否按下了左键默认的为False
            private bool isMouseDown = false;
            private void login_MouseDown(object sender, MouseEventArgs e)
            {
                try
                {
                    //X轴
                    int xOffset;
                    //Y轴
                    int yOffset;
                    //当按下左键时
                    if (e.Button == MouseButtons.Left)
                    {
                         //X轴为当前的位置加上窗体的长度
                        xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                        //Y轴为当前的位置加上窗体的高度
                        yOffset = -e.Y - SystemInformation.CaptionHeight -
                            SystemInformation.FrameBorderSize.Height;
                        //综合一下坐标
                        mouseOffset = new Point(xOffset, yOffset);
                        //表示按下了左键
                        isMouseDown = true;
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }        private void login_MouseMove(object sender, MouseEventArgs e)
            {
                try
                {
                    //如果按下了左键
                    if (isMouseDown)
                    {
                        //前当的坐标==鼠标的位置
                        Point mousePos = Control.MousePosition;
                        mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                        Location = mousePos;
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }        private void login_MouseUp(object sender, MouseEventArgs e)
            {
                try
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        isMouseDown = false;
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }