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

c#停靠窗体

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 public partial class FrmAnchor : Form, IMessageFilter
    {
        public FrmAnchor(Control parentControlc, Control keyControl)
        {
            InitializeComponent();
            this.Size = keyControl.Size;
            this.HandleCreated += FrmDownBoard_HandleCreated;
            this.HandleDestroyed += FrmDownBoard_HandleDestroyed;
            this.Controls.Add(keyControl);
            keyControl.Dock = DockStyle.Fill;
            Point p = parentControlc.Parent.PointToScreen(parentControlc.Location);
            int intX = 0;
            int intY = 0;
            if (p.Y + parentControlc.Height + keyControl.Height > Screen.PrimaryScreen.Bounds.Height)
            {
                intY = p.Y - keyControl.Height-1;
            }
            else
            {
                intY = p.Y + parentControlc.Height+1;
            }

            if (p.X + keyControl.Width > Screen.PrimaryScreen.Bounds.Width)
            {
                intX = Screen.PrimaryScreen.Bounds.Width - keyControl.Width;

            }
            else
            {
                intX = p.X;
            }
            this.Location = new Point(intX, intY);

        }

        public FrmAnchor(Size size,Point location, Control keyControl)
        {
            InitializeComponent();
            this.Size = keyControl.Size;
            this.HandleCreated += FrmDownBoard_HandleCreated;
            this.HandleDestroyed += FrmDownBoard_HandleDestroyed;
            this.Controls.Add(keyControl);
            keyControl.Dock = DockStyle.Fill;
            Point p = location;
            int intX = 0;
            int intY = 0;
            if (p.Y + size.Height + size.Height > Screen.PrimaryScreen.Bounds.Height)
            {
                intY = p.Y - keyControl.Height - 1;
            }
            else
            {
                intY = p.Y + size.Height + 1;
            }

            if (p.X + keyControl.Width > Screen.PrimaryScreen.Bounds.Width)
            {
                intX = Screen.PrimaryScreen.Bounds.Width - keyControl.Width;

            }
            else
            {
                intX = p.X;
            }
            this.Location = new Point(intX, intY);

        }

        private void FrmDownBoard_HandleDestroyed(object sender, EventArgs e)
        {
            Application.RemoveMessageFilter(this);
        }

        private void FrmDownBoard_HandleCreated(object sender, EventArgs e)
        {
            Application.AddMessageFilter(this);
        }

        #region 无焦点窗体

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private extern static IntPtr SetActiveWindow(IntPtr handle);
        private const int WM_ACTIVATE = 0x006;
        private const int WM_ACTIVATEAPP = 0x01C;
        private const int WM_NCACTIVATE = 0x086;
        private const int WA_INACTIVE = 0;
        private const int WM_MOUSEACTIVATE = 0x21;
        private const int MA_NOACTIVATE = 3;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_MOUSEACTIVATE)
            {
                m.Result = new IntPtr(MA_NOACTIVATE);
                return;
            }
            else if (m.Msg == WM_NCACTIVATE)
            {
                if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
                {
                    if (m.LParam != IntPtr.Zero)
                    {
                        SetActiveWindow(m.LParam);
                    }
                    else
                    {
                        SetActiveWindow(IntPtr.Zero);
                    }
                }
            }
            base.WndProc(ref m);
        }

        #endregion

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg != 0x0201 || this.Visible == false) return false;
            var pt = this.PointToClient(MousePosition);
            this.Visible = this.ClientRectangle.Contains(pt);
            return false;
        }
    }
View Code

 

效果如下:

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
c#streambytecharstring流、字节、字符、字符串(转)发布时间:2022-07-13
下一篇:
1、入门--ASP.NETWebAPI2(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