• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#制作悬浮框

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一,制作winform 窗体

窗体要做小一点,你见过500*500的悬浮框吗?

二,去掉边框和标题栏

   this.FormBorderStyle = FormBorderStyle.None;

  运行出来如下所示:

  

三,在窗体中拖放label 控件

  因为准备在悬浮框中放置gif动画,

          labelex.AutoSize = false;
                labelex.Left = 0;
                labelex.Top = 0;
                labelex.Width = this.Width;
                labelex.Height = this.Height;

 

四,拖拉label事件

const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 0x0001;
        const int HTCAPTION = 0x0002;
        private Point ptMouseCurrrnetPos, ptMouseNewPos, ptFormPos, ptFormNewPos;
        public  bool blnMouseDown = false;

        [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;

        const int WM_NCLBUTTONDBLCLK = 0xA3;
        public const int WM_RBUTTONDOWN = 0x0204;
        public const int WM_LBUTTONDOWN = 0x0201;
 private void labelex_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                blnMouseDown = true;
                // Save window position and mouse position
                ptMouseCurrrnetPos = Control.MousePosition;
                ptFormPos = Location;
            }

            ReleaseCapture();
            //SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

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

        private void labelex_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;

            }
        }
    }

五,将窗体置于最顶端

  将窗体属性TopMost = True,这样窗体就可以不被其他窗体覆盖,置于屏幕的最顶端了。

  

六,这样一个简单的悬浮框 就做好了。可以手动尝试一下。

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#实现FTP文件夹下载功能【转载】发布时间:2022-07-13
下一篇:
C#窗体内控件大小随窗体等比例变化(转)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap