首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#net无际框窗口的拖动

2012-12-14 
c#.net无边框窗口的拖动!之前一直没有怎么好好学习C#,只是了解一个大概,做起事来才知道是多么的难,就拿本

c#.net无边框窗口的拖动!
之前一直没有怎么好好学习C#,只是了解一个大概,做起事来才知道是多么的难,就拿本问来说,这一个功能,我找了好多资料都没有实现我想要的效果,最终还是自己琢磨出来了,发出来分享下,方便后来者借鉴,当然大家有什么更好的办法可以交流哈哈,言归正传。

大家都知道,在有边框的情况下,form的拖动是不需要什么代码就可以实现的,但是在很多情况下,边框的存在影响了整体程序的布局和美观,不得不取消掉,那么就会发现根本无法拖动了,怎么实现拖动呢?

首先定义三个参数

private bool moveFlag = false;//鼠标点击
        private int x = 0;
        private int y = 0;

x,y就是坐标了,不用解释

接下来:

 

protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag && (e.Button == MouseButtons.Left))
                this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (!moveFlag && e.Clicks >= 1)
                moveFlag = true;
            x = e.X;
            y = e.Y;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag)
                moveFlag = false;
            base.OnMouseUp(e);

  }


三个函数,一目了然,不用多做解释了,唯一要说明的事,获取的鼠标事件是当前form的,所以System.Windows.Forms.MouseEventArgs e。

另外呢,还要加一点,大家都看到千千静听啊什么的那种软件,是多FORM的,怎么样在拖动主 form的时候附属form也跟着一起动呢?这点的实现也很简单,请看代码:

在form1中定义一个 Form f;

这个对象用来做什么呢?后面就会用到了

在你打开form2的事件函数下加上

Form2 f3 = new Form2(this);

f3.show();

f = f3;//哈哈,当前打开的Form2赋给f了

好了,后面就可以直接操作f了。

在上面的第三个函数中加入

      
  protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag)
                moveFlag = false;
            base.OnMouseUp(e);

            f.Hide();
            Form3 f31 = new Form3(this); 
            f31.Show();
             f = f31;



  }



OK,测试一下吧,是不是实现了呢?

[最优解释]
protected override void WndProc(ref Message m)
        {
            if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
                m.WParam = (IntPtr)1;
            base.WndProc(ref m);
            if (m.Msg == 132 && m.Result.ToInt32() == 1)
                m.Result = (IntPtr)2;
        }

[其他解释]
说实话,这个方法的效果不是很好(纯个人感觉。不同意的勿喷),不过还是感谢楼主分享。
[其他解释]
嗯 虽然不做winform up..
[其他解释]
        #region 窗体移动
        int a = 0, b = 0;
        private void frmLogin_MouseDown(object sender, MouseEventArgs e)
        {
            a = e.X;
            b = e.Y;
            this.Cursor = System.Windows.Forms.Cursors.SizeAll;          
        }

        private void frmLogin_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(e.X + this.Left - a, e.Y + this.Top - b);
            }
        }

        private void frmLogin_MouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.Default;
            if (this.Left < -Width / 2)
                this.Left = -this.Width / 2;
            if (this.Left > SystemInformation.PrimaryMonitorSize.Width - this.Width / 2)
                this.Left = SystemInformation.PrimaryMonitorSize.Width - this.Width / 2;


            if (this.Top < 20)
                this.Top = 20;
            if (this.Top > SystemInformation.PrimaryMonitorSize.Height - 20)
                this.Top = SystemInformation.PrimaryMonitorSize.Height - 20;
        }

        #endregion 
[其他解释]

引用:
C# code
protected override void WndProc(ref Message m)
        {
            if (m.Msg == 163 &amp;&amp; this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) &amp;&amp; m……



这个是最简单的,不用再找了

[其他解释]
引用:
 C# 无边框窗体移动

你新建个窗体。不用修改一行代码。双击窗体或查看代码。在Form1类中粘贴如下代码就能实现了。

C# code

[System.Runtime.InteropServices.DllImport("user32.dll")]   
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, ……
 +1
[其他解释]
 C# 无边框窗体移动

你新建个窗体。不用修改一行代码。双击窗体或查看代码。在Form1类中粘贴如下代码就能实现了。


[System.Runtime.InteropServices.DllImport("user32.dll")]   
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wparam, int lparam);   
protected override void OnMouseDown(MouseEventArgs e)   
{   
    base.OnMouseDown(e);   
    if (e.Button == MouseButtons.Left)//按下的是鼠标左键   
    {   
        Capture = false;//释放鼠标,使能够手动操作   
        SendMessage(this.Handle, 0x00A1, 2, 0);//拖动窗体   
    }   
}  


[其他解释]
http://www.cnblogs.com/jianu/archive/2010/07/03/1770293.html

无边框窗体拖动  还半透明的
[其他解释]
太好了啊,找了很长的时间,
[其他解释]
null

热点排行