2013-11-28 25 views
0

與 「FindWindow函數」 點擊是有人能翻譯這個代碼...手柄自動彈出,並在VB.net

int iHandle = NativeWin32.FindWindow(null, "Security Alert"); 
NativeWin32.SetForegroundWindow(iHandle); 
System.Windows.Forms.SendKeys.Send("Y%"); 

...到VB.net?

enter image description here

因爲我無法點擊「是」,在這個「安全警報」窗口(上圖)。

+4

我不相信你正在嘗試去就是好實踐。該盒子在那裏通知用戶安全證書存在問題。這是他們的選擇,不是你的潮溼或不繼續。 –

+1

......這就是說,pinvoke.net擁有所有WinAPI調用的所有功能,比如'FindWindow'。 'SetForegroundWindow'似乎是它們的別名'SetActiveWindow' – Plutonix

+0

只是提到...如果用戶安裝了另一種語言,那麼發送字母'Y'可能什麼都不做。 – SysDragon

回答

1

試試這個:

<System.Runtime.InteropServices.DllImport("user32.dll")> _ 
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As System.IntPtr 
End Function 

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

Public Sub HandleWindows() 
    Dim iPtr As Integer = FindWindow(Nothing, "MyWindow") 
    SetForegroundWindow(iPtr) 
    SendKeys.SendWait("{ENTER}") 
End Sub 

或者,你可以把窗口的句柄與進程,是這樣的:

Public Function GetWindowHandle() As Integer 
    Dim proc As Process = Process.GetProcessesByName("OUTLOOK")(0) 

    If proc IsNot Nothing AndAlso proc.MainWindowTitle.Equals("Security Alert") Then 
     return proc.MainWindowHandle() 
    End If 

    return 0 
End Sub 
+0

非常有用的代碼謝謝!問題是我正面臨着這個安全警告進入「webbrowser1」控件(默認情況下爲Internet Explorer)。而且我仍無法捕捉到引發此安全警報的事件。我也必須找到什麼是進程ID。無論如何,感謝這些想法! – SkorPPio