简体   繁体   English

将焦点设置为新的弹出窗口

[英]Set focus to new popup winform

I have an app that runs in the system tray and when a user presses a combation of buttons it displays a WinForm to be filled out and sends an email.我有一个在系统托盘中运行的应用程序,当用户按下按钮时,它会显示一个要填写的 WinForm 并发送电子邮件。 Everything works great but the part when the WinForm is displayed.一切都很好,但显示 WinForm 时的部分。 It shows on top and the focus appears to be on the text box but the window is not active.它显示在顶部,焦点似乎在文本框上,但该窗口未处于活动状态。

Code used to to call the Popup form.用于调用 Popup 窗体的代码。

My.Forms.frmpopup.ShowDialog()

Code on the Popup Form弹出窗体上的代码

Private Sub frmPopup_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Activate()
    BringToFront()
End Sub

Private Sub frmPopup_Load(sender As Object, e As EventArgs) Handles Me.Load
    TextBoxName.Focus()
End Sub

The real kick in the pants is that this works as long as the user is not currently focused on an Internet Explorer Window.真正令人振奋的是,只要用户当前不专注于 Internet Explorer 窗口,它就可以工作。 If I find a solution I'll post it.如果我找到解决方案,我会发布它。


It appears merging the two Subs into a single sub fixes the issue, I'll continue to test.似乎将两个 Sub 合并为一个 Sub 可以解决问题,我将继续测试。

Private Sub frmPopup_Shown(sender As Object, e As EventArgs) Handles Me.Shown Activate() BringToFront() TextBoxName.Focus() End Sub Private Sub frmPopup_Shown(sender As Object, e As EventArgs) 处理 Me.Shown Activate() BringToFront() TextBoxName.Focus() End Sub

Make sure your application has focus as well... 确保您的应用程序也具有焦点......

Include the following import... 包括以下导入...

<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
End Function

And before you call the pop-up, or from within the pop-up LOAD event, call 在调用弹出窗口之前,或者在弹出的LOAD事件中,调用

SetForegroundWindow(Me.Handle)

SendToTop(true) should bring the window to TopMost. SendToTop(true)应该将窗口带到TopMost。

Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2

Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean

Public Sub SendToTop(toTop As Boolean)
    If toTop Then
        SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    Else
        SetWindowPos(Me.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
    End If
End Sub

just use Active(); 只需使用Active(); on the popup load event 在弹出加载事件上

In my use-case* the following showed the desired result:在我的用例*中,以下显示了所需的结果:

popup.Show();
popup.Activate();

where popup: Form .其中popup: Form Not sure why you have to call the Activate() after the Show() method, but that proved to be working correctly.不知道为什么必须在Show()方法之后调用Activate() ,但事实证明它可以正常工作。


Use-case用例

My use-case was showing popup along with the loader form.我的用例显示弹出窗口和加载程序表单。 Both the popup and the loader are top level forms and are triggered with the ShowDialog() method.弹出窗口和加载器都是顶级表单, ShowDialog()方法触发。 They are executed in separate threads with the idea that loader form is displayed while the popup form is fetching its data.它们在单独的线程中执行,其想法是在弹出窗体获取其数据时显示加载程序窗体。 Once the long operation (ie fetching) is completed, the loader form closes and the popup form is shown.一旦完成长操作(即获取),加载程序窗体关闭并显示弹出窗体。

Everything was working as described, except that the popup was not focused after the loader closes.一切都按描述进行,除了加载程序关闭后弹出窗口没有聚焦。

The given two lines of code are solving my problem and now everything is working as expected.给定的两行代码正在解决我的问题,现在一切都按预期工作。

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

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