以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  用C#做一个悬浮窗口[含三种移动无标题窗体的办法]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=84957)


--  作者:卷积内核
--  发布时间:5/28/2010 10:50:00 AM

--  用C#做一个悬浮窗口[含三种移动无标题窗体的办法]
第一步:建立一个窗体,设置其属性:

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Opacity = 0.5;

第二步:设置窗体的OnLoad事件:

        private void Form2_Load(object sender, EventArgs e)
        {
            this.Top = 20;
            this.Left = Screen.PrimaryScreen.Bounds.Width - 80;
            this.Width = 60;
            this.Height = 60;
        }

第三步:修改窗体的Paint事件,美化界面,这里做一个渐变背景,需要引入System.Drawing.Drawing2D;

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Color FColor = Color.Red;
            Color TColor = Color.Yellow;
            Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal);
            g.FillRectangle(b, this.ClientRectangle);
        }

第四步:需要实现鼠标拖动悬浮窗体,

        const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 0x0001;
        const int HTCAPTION = 0x0002;
        protected override void WndProc(ref System.Windows.Forms.Message m)
       {
            switch(m.Msg)
           {
                    case WM_NCHITTEST:
                             base.WndProc(ref m);
                             if (m.Result==(IntPtr)HTCLIENT)
                                      m.Result=(IntPtr)HTCAPTION;
                             break;
                    default:
                             base.WndProc(ref m);
                             break;
           }
      }

关于这一步,愚翁 不是这样处理的,他是处理鼠标事件来实现的,效果差不多,也贴在这里学习一下吧:
先定义几个类成员变量:
        private Point ptMouseCurrrnetPos, ptMouseNewPos,ptFormPos, ptFormNewPos;
        private bool blnMouseDown = false;

再添加三个鼠标事件:


--  作者:卷积内核
--  发布时间:5/28/2010 10:50:00 AM

--  
再添加三个鼠标事件:
        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                blnMouseDown = true;
                // Save window position and mouse position
                ptMouseCurrrnetPos = Control.MousePosition;
                ptFormPos = Location;
            }

        }

        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            if (blnMouseDown)
            {
                //Get the current position of the mouse in the screen
                ptMouseNewPos = Control.MousePosition;
                //Set window position
                ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrrnetPos.X + ptFormPos.X;
                ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrrnetPos.Y + ptFormPos.Y;
                //Save window position
                Location = ptFormNewPos;
                ptFormPos = ptFormNewPos;
                //Save mouse position
                ptMouseCurrrnetPos = ptMouseNewPos;

            }
        }

        private void Form2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                //Return back signal
                blnMouseDown = false;
        }


第三种移动无标标题窗体的办法:
通过API来处理,需要引入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 Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   ReleaseCapture();
   SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0);
  }

呵,这个看起来好像比较“正统”。


--  作者:卷积内核
--  发布时间:5/28/2010 10:51:00 AM

--  
第一种采用,需注意窗体上的控件是否把窗体覆盖了。。。MouseDown、MouseMove、MouseUp事件应该是鼠标所处位置最顶层的控件的事件

在窗体的类中声明两个变量

private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下

创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    int xOffset;
    int yOffset;

    if (e.Button == MouseButtons.Left)
    {
        xOffset = -e.X ;
        yOffset = -e.Y ;
        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)
{
    // 修改鼠标状态isMouseDown的值
    // 确保只有鼠标左键按下并移动时,才移动窗体
    if (e.Button == MouseButtons.Left)
    {
        isMouseDown = false;
    }
}


--  作者:卷积内核
--  发布时间:5/28/2010 10:52:00 AM

--  
第二种调用API 验证OK!

        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, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

第三种未验证

        private bool isMouseDown = false;

        private Point FormLocation; //form的location

        private Point mouseOffset; //鼠标的按下位置

        [DllImport("user32.dll")]

        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]

        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息

        private const int SC_MOVE = 0xF010;//移动信息

        private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息

        private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息

        private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息

        private const int SC_MAXIMIZE = 0xF030;//最大化信息

        private const int SC_MINIMIZE = 0xF020;//最小化信息      

        protected override void WndProc(ref Message m)

        {

            switch (m.Msg)

            {

                case WM_SYSCOMMAND:

                    if (m.WParam == (IntPtr)SC_MAXIMIZE)

                    {

                        m.WParam = (IntPtr)SC_MINIMIZE;

                    }

                    break;

                case WM_NCHITTEST: //如果鼠标移动或单击

                    base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息

                    if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT

                    {

                        m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION

                        return;//直接返回退出方法

                    }

                    break;

            }

            base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms