前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# 停靠窗体

c# 停靠窗体

作者头像
冰封一夏
发布2019-09-11 15:19:35
1K0
发布2019-09-11 15:19:35
举报
文章被收录于专栏:c#Winform自定义控件系列
代码语言:javascript
复制
 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;
        }
    }

效果如下:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档