C# 子窗口跟随父窗口且显示在最顶层

从中可参考的知识点

  1. 如何获得当前活动窗口
using System.Runtime.InteropServices;


 [DllImport("User32.dll")]
 public static extern IntPtr GetForegroundWindow();     //获取活动窗口句柄

 IntPtr hWnd = GetForegroundWindow();    //获取活动窗口句柄

 if (flg_AlarmWind_is_show == 0 && this.Handle == hWnd)
{
      AlarmWind.Location = new Point(this.Left, this.Top);
      AlarmWind.TopMost = true;
      AlarmWind.Show();
  }
flg_AlarmWind_is_show = 1;
  1. 怎样将窗口放置最上层
 AlarmWind.TopMost = true;
  1. 怎样改变窗口位置:
AlarmWind.Location = new Point(this.Left, this.Top);

思路

要显示在父窗口上层,不能被父窗口遮挡。但如果父窗口不再成为活动窗口(最小化或被其他进程窗口遮挡)时,子窗口又不能弹出。所以要判断当前活动窗口是不是父窗口,是则显示在最顶层,否则隐藏。

例程:

  private AlarmInfoWindow AlarmWind = new AlarmInfoWindow(); //为避免频繁创建,定义成全局的
       
        private int flg_AlarmWind_is_show = 0;

        public Form_EzHostCtrl()
        {
            InitializeComponent();                    
         
            creatTimerforAlarmShow();//定时器要随对象一起创建
        }
        public void Initial()
        {
            LoadPath();
            LayoutInitial();
        }
        private System.Timers.Timer AlarmWindShowTimer = new System.Timers.Timer();
        private void creatTimerforAlarmShow()
        {
            AlarmWindShowTimer.Interval = 50;
            AlarmWindShowTimer.Elapsed += AlarmWindShowTimer_Elapsed; //定时时间到的时候的回调函数
            AlarmWindShowTimer.AutoReset = true;//需要反复检测鼠标
            AlarmWindShowTimer.Enabled = true;  //启动定时器

        }

        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();     //获取活动窗口句柄

        /* 定时器回调函数 */
        private void AlarmWindShowTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //如果鼠标在窗体边缘附近  
            if ((Cursor.Position.X - this.Left < 50 && Cursor.Position.X - this.Left > -50)
               || (Cursor.Position.X - this.Right < 50 && Cursor.Position.X - this.Right > -50)
               || (Cursor.Position.Y - this.Top < 50 && Cursor.Position.Y - this.Top > -50)
               || (Cursor.Position.Y - this.Bottom < 50 && Cursor.Position.Y - this.Bottom > -50))
            {
                IntPtr hWnd = GetForegroundWindow();    //获取活动窗口句柄
                
                if (flg_AlarmWind_is_show == 0 && this.Handle == hWnd)
                {
                    AlarmWind.Location = new Point(this.Left, this.Top);
                    AlarmWind.TopMost = true;
                    AlarmWind.Show();
                }
                flg_AlarmWind_is_show = 1;
            }
            else {
                AlarmWind.Hide();
                flg_AlarmWind_is_show = 0;
            }
        }
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxgui1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值