zoukankan      html  css  js  c++  java
  • c# 停靠窗体

     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

    效果如下:

  • 相关阅读:
    Netty入门——客户端与服务端通信
    使用配置文件自定义Ribbon配置
    使用Java代码自定义Ribbon配置
    Spring Cloud Ribbon入门
    负载均衡简介
    常见的几种负载均衡算法
    Eureka编程
    Eureka多机高可用
    Maven项目打包成可执行Jar文件
    Eureka单机高可用伪集群配置
  • 原文地址:https://www.cnblogs.com/bfyx/p/9540711.html
Copyright © 2011-2022 走看看