简体   繁体   English

模态对话框没有得到关注

[英]Modal dialog doesn't get focus

I'm dealing with a bug relating to a C# WinForms app that starts up with a splash screen, then closes the splash screen and opens a login form. 我正在处理一个与C#WinForms应用程序有关的错误,该错误以启动屏幕启动,然后关闭启动屏幕并打开登录表单。 On some computers everything works fine. 在某些计算机上,一切正常。 On others, the login form appears, but with a flashing title bar, which after a few flashes, loses focus altogether. 在其他表单上,将显示登录表单,但带有闪烁的标题栏,该标题栏在闪烁几下后完全失去焦点。 The cursor is still flashing in the "User name" text box, but the app does not have focus, and when you start typing, nothing happens, which is very annoying for the user. 光标仍在“用户名”(User name)文本框中闪烁,但该应用程序没有焦点,并且当您开始键入内容时,什么也没有发生,这对用户来说很烦人。

There seems to be no difference what OS is running (we've tried Windows 7 and Server 2008), and we have been particular not to have any keyboard or mouse input after starting up the app. 所运行的操作系统(我们尝试使用Windows 7和Server 2008)似乎没有什么区别,并且特别要注意的是,启动该应用程序后,不要输入任何键盘或鼠标。

Now - does anyone have any idea what could be causing the app to lose focus? 现在-是否有人知道导致应用失去焦点的原因是什么?
Alternatively, how would you debug this issue? 或者,您将如何调试此问题? We have been unable to replicate the problem in the Visual Studio debug environment, but that doesn't entirely surprise me because I'd guess it's an issue of how the compiled app interacts with the OS... or am I wrong? 我们一直无法在Visual Studio调试环境中复制该问题,但这并不能完全让我感到惊讶,因为我想这是编译后的应用程序与操作系统交互的问题...还是我错了?

EDIT #1: I thought this was solved by @vinodpthmn, by ensuring that the splash screen is properly closed before the login form appears, but this appears not to have helped. 编辑#1:我认为这已由@vinodpthmn解决,方法是确保在登录表单出现之前正确关闭启动画面,但这似乎没有帮助。 So I created a logger to track all the events and threads, and found the following interesting log entries: 因此,我创建了一个记录器来跟踪所有事件和线程,并发现以下有趣的日志条目:

Thread 01 - 2012/03/29 12:51:09.693 - Show splash screen
Thread 01 - 2012/03/29 12:51:20.350 - Splash screen closed
Thread 01 - 2012/03/29 12:51:20.490 - Login Form Activated
Thread 01 - 2012/03/29 12:51:20.522 - Login Form Load
Thread 01 - 2012/03/29 12:51:25.694 - Login Form deactivated
Thread 01 - 2012/03/29 12:51:25.694 - Active form =
Thread 01 - 2012/03/29 12:51:25.694 - Active app =

Those last lines showing the active form and app respectively display Form.ActiveForm and the currently active app in Windows (code for this here ). 显示活动表单和应用程序的最后Form.ActiveForm行分别显示Form.ActiveForm和Windows中当前活动的应用程序( 此处的代码)。 And they are both empty/null. 而且它们都是空的。 This is so, even if in the Login form Load I call Activate() or SetForegroundWindow() as suggested by @memetolsen. 即使是在登录表单“加载”中,我也会按照@memetolsen的建议调用Activate()SetForegroundWindow() The login form never even receives focus! 登录表单甚至从未获得关注!

Any ideas? 有任何想法吗?

EDIT #2: Well, I just eliminated the splash screen, and now the login form gets focus. 编辑#2:好吧,我只是取消了初始屏幕,现在登录表单成为焦点。 Replacing the splash screen reintroduces the bug. 替换初始屏幕会重新引入该错误。 But I have put code in to ensure that the splash screen is disposed - not just closed - before I try to open the login form. 但是,在尝试打开登录表单之前,我已经输入了代码以确保启动屏幕已放置好 -而不仅仅是关闭了-。

Could it make a difference that the splash screen is opened with Application.Run(frmSplash) ? 使用Application.Run(frmSplash)打开初始屏幕会有所不同吗?

Could it make a difference that the splash screen is opened with Application.Run(frmSplash) 使用Application.Run(frmSplash)打开初始屏幕会有所不同吗

Certainly, doing it this way guarantees you'll have this kind of problem. 当然,以这种方式进行操作可确保您会遇到此类问题。 This trouble is caused because for a split second you have no window that can receive the focus. 造成此问题的原因是,一瞬间您没有可接收焦点的窗口。 The splash screen is gone, your main window still needs a handful of milliseconds to get created and visible. 初始屏幕不见了,您的主窗口仍然需要几毫秒的时间才能创建并可见。 This forces Windows to find another window to give the focus to. 这将迫使Windows找到另一个焦点所在的窗口。 Since it can't be your window, it will pick the window of another application. 由于它不是您的窗口,它将选择另一个应用程序的窗口。 Your main window will not have the focus when appears. 出现时,主窗口将没有焦点。

The exact rules that the window manager uses are not clear to me, this focus loss doesn't always happen. 窗口管理器使用的确切规则对我来说并不明确,这种焦点丢失并不总是会发生。 I'm guessing it is time related. 我猜这与时间有关。

Solve your problem by using the excellent and trouble free support for splash screens that's already built into the framework. 通过对框架中已内置的初始屏幕的出色且无故障的支持来解决您的问题。 You'll find the code to use it in this answer . 您将在此答案中找到使用它的代码。 If you don't want to use it then rework your code to ensure the main window is displayed before you close the splash screen. 如果您不想使用它,请在关闭启动屏幕之前重新编写代码以确保显示主窗口。

I have faced a similar issue earlier, the problem was due to improper closing of splash screen. 我之前也遇到过类似的问题,该问题是由于启动画面关闭不当造成的。 I suspect the focus might be on that form (not sure though). 我怀疑重点可能在表单上(虽然不确定)。

  1. make sure you unload/close splash screen form properly 确保正确卸载/关闭启动画面表单
  2. login screen is shown after proper unload of splash screen 正确卸载启动画面后,将显示登录画面
  3. login form is shown as modal to instruct the execution pointer to wait until next user interaction 登录表单显示为模式,以指示执行指针等待下一次用户交互

I suggest you set up remote debugging . 我建议您设置远程调试 This will enable you to debug the application remotely from your development machine. 这将使您能够从开发计算机上远程调试应用程序。

Maybe it would be a solution if you put this: 如果您输入以下内容,可能会是一个解决方案:

this.Activate();

or this: 或这个:

SetForegroundWindow(this.Handle.ToInt32());

in the load event of the form. 在表单的load事件中。

If this doesn't work, try using a timer which ferforms this code after a certain time. 如果这不起作用,请尝试使用计时器,该计时器在一定时间后会转换此代码。

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

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