简体   繁体   English

显示表格但没有窃取焦点过一会儿就停止工作

[英]Show a Form without stealing focus stopped working after a while

I created my own on screen keyboard with special features. 我创建了自己的具有特殊功能的屏幕键盘。 I'm using override methods ShowWithoutActivation and CreateParams for preventing my Form getting focus (according to this stackoverflow question ). 我正在使用override方法ShowWithoutActivationCreateParams来防止我的窗体获得焦点(根据此stackoverflow问题 )。

    protected override bool ShowWithoutActivation
    {
        get { return true; }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams param = base.CreateParams;
            param.ExStyle |= 0x08000000;
            return param;
        }
    }

But after a few Hide() and Show() my Form is again focusable. 但是经过几次Hide()Show()我的表单又可以重新聚焦了。 To fix it I need to restart application, but it is of course poor solution. 要修复它,我需要重新启动应用程序,但这当然是较差的解决方案。 Is it possible to do without restart? 不重启就可以吗? Showing Form after being hidden without giving it focus not help. 隐藏后不显示焦点就显示窗体无济于事。 My application can just be focused again. 我的应用程序可以再次集中。

I'm noticed that in most cases it happens randomly, but in one of them always: if I show my application after right mouse click on NotifyIcon in tray and choose item from ContextMenuStrip. 我注意到,在大多数情况下,它是随机发生的,但是在其中一种情况下,总是这样:如果在右键单击托盘中的NotifyIcon后显示我的应用程序,然后从ContextMenuStrip中选择项目。 OnClick I simply call show function. OnClick我只是简单地调用show函数。

Do you need to click the window? 您需要点击窗口吗? When not, try this one: 如果没有,请尝试以下方法:

   protected override void WndProc(ref Message message)
   {
       switch (message.Msg)
       {
           case 0x84: // WM_NCHITTEST
               message.Result = (IntPtr)(-1); // HTTRANSPARENT;
               return;
       }

       base.WndProc(ref message);
       return;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM