SetWindowPos不能置顶的问题

cwf673452719 2017-08-16 03:33:55

public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶
public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置
public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小
public static bool setTopChildWin()
{
IntPtr ChildWin = FindWindow(null, "MessageTip"); // 窗口类型WpfApplication1 窗口主题Tip
int childwin = (int)ChildWin;
if (childwin != 0)
{

SetWindowPos(ChildWin, MainWindow.HWND_TOPMOST, 0, 0, 0, 0, MainWindow.SWP_NOMOVE | MainWindow.SWP_NOSIZE);
return true;
}
return false;
}



写的是一个定时提醒的小工具,定时器新建窗口并置顶,但是我如果在任务栏反复点这个窗口,使其多次显示-隐藏后,这个置顶方法就不能再是窗口置顶了。
...全文
421 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwf673452719 2017-08-22
  • 打赏
  • 举报
回复
TopMost = true 也是一样的

  private void timer_method()
        
        {
            if (_ckbtextstatus == true)
            {

                //查找窗口并置顶
                System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       new delegate_setTopChildWin(setTopChildWin));
               
                
                if (_settop == false)
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new delegate_messagetip(messagetip));
                    
                    
                }
            }
        }

 private delegate void delegate_setTopChildWin();
        /// <summary>
        /// 窗口置顶
        /// </summary>
        /// <returns></returns>
        private void setTopChildWin()
        {
            IntPtr ChildWin = FindWindow(null, "MessageTip");    // 窗口类型WpfApplication1 窗口主题Tip
            IntPtr ChildWin1 = FindWindow("TimeTipAPP", null);
            IntPtr ChildWin2 = FindWindow("APPMessage", null);
            int childwin = (int)ChildWin;
            if (childwin != 0 && _messagewin!=null)
            {

                //SetWindowPos(ChildWin, MainWindow.HWND_TOPMOST, 0, 0, 0, 0, MainWindow.SWP_NOMOVE | MainWindow.SWP_NOSIZE);
                _messagewin.Topmost = true;
                _settop = true;
                return ;
                
            }
            _settop = false;
            return ;
        }

 /// <summary>
        /// 提醒弹窗委托
        /// </summary>
        /// <returns></returns>
        private delegate void delegate_messagetip();
        private void messagetip()
        {
            _parentWin = Window.GetWindow(this);

            _messagewin = new APPMessage(_textmessage);
            _messagewin.Owner = _parentWin;
            hide();
            _messagewin.Show();
            setTopChildWin();
               
        }

主要在新弹出窗口移动并关闭后,定时器再次执行到 //查找窗口并置顶 System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new delegate_setTopChildWin(setTopChildWin));这个句就卡主了
xuyufeng822 2017-08-17
  • 打赏
  • 举报
回复
不用调用API吧, frm1.TopMost = true 就可以了,在定时器里写,时刻 保持最前。 但是如果有多个在最前的窗体要注意哪个活动,哪个就暂时在最前

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧