Windows窗体焦点问题 [英] windows form focus issue

查看:251
本文介绍了Windows窗体焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了一个应用程序,该应用程序将继续在后台运行,并且对用户不可见.当用户按下热键时,我将提示您输入密码.

我已经填写了表格的

ShowInTaskbar = false并设置为模型.

现在的问题是,当我再次显示它时,需要一两秒的时间才能获得焦点.它将可见,但不会立即获得焦点.

我尝试将焦点手动设置为文本框,但对我不起作用.

任何帮助将不胜感激.

Hi All,

I have created an application which will keep on working in background and not visible to user. When user presses a hot key then I will prompt for password.

I have made my form’s

ShowInTaskbar=false and set is a model.

Now the problem is when I will show it again, it will take time of a sec or two to get focus. It will be visible but will not get focus immediately.

I have tried to set focus manually to textbox but it does not work for me.

Any help will be appreciated.

推荐答案

如果应用程序被激活,则它将被激活并立即获得焦点,除非您搞砸了某些东西,例如呈现控件.问题并没有明确表述,因为展示应用程序和关注焦点涉及三个相关方面:1)以Z顺序将表单放在顶部,请参见System.Windows.Forms.Form.BringToFront; 2)激活,请参见System.Windows.Forms.Form.Activate; 3)获得键盘焦点;如果表单具有任何控件(应该具有,否则,您为什么需要一个控件:-)),因此,将焦点放在表单上是没有意义的;其中一个控件应重点关注,请参见System.Windows.Forms.Control.Focus.如果在停用之前集中控制,它将再次集中在激活上.您不必在代码中执行此操作.此外,您不希望失去用户在停用之前已经完成的焦点更改.

参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.form.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].

您可以调查为什么进展如此缓慢;您可以使用System.Diagnostics.StopwatchSystem.Windows.Forms.Form.Activated和System.Windows.Forms.Control的时间. aspx> http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [
If application is activated, it is activated and got focus immediately, unless you screw up something, such are rendering of control. Question is not clearly formulating, because there three related aspects of showing application and focusing: 1) putting a form on top in Z-order, see System.Windows.Forms.Form.BringToFront; 2) activation, see System.Windows.Forms.Form.Activate; 3) getting keyboard focus; if the form has any controls (it should have, otherwise why would you need one :-)), focusing of the form makes no sense; one of the controls should be focused, see System.Windows.Forms.Control.Focus. If some control was focused before deactivation, it will be focused again on activation; you don''t have to do it in code. Besides, you don''t want loosing a change in focus the user have done before deactivation.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

You can investigate why it goes so slowly; you can research order of operations and time them handling events System.Windows.Forms.Form.Activated and System.Windows.Forms.Control using System.Diagnostics.Stopwatch, http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^]. It''s hard to tell you more without looking at your code.

Why do you use ShowInTaskbar = false? Is does not make sense for main form of the application (but very useful for all other forms which should better be Owned). However, if this is a tray icon application, this is right thing to do.

—SA


确保每次激活表单时您都无需进行任何耗时的处理.
Make sure you don''t do any time consuming processing every time a form activates.


这篇关于Windows窗体焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆