如何在C#或VB.Net Winform应用程序中的javascript警报/确认框中获取用户操作,如OK或Cancel?

如何在C#或VB.Net Winform应用程序中的javascript警报/确认框中获取用户操作,如OK或Cancel?,c#,vb.net,winforms,internet-explorer,alert,C#,Vb.net,Winforms,Internet Explorer,Alert,我在Vb.Net WinForms应用程序中使用SHDocVw.InternetExplorer API在我的应用程序中记录来自Internet Explorer的用户操作 通过使用“user32.dll”的“getForeGroundIndow”,我可以获得确认框的句柄。我可以很容易地获得所需的信息,如“ClassName”和“WindowText” 现在我需要知道关闭确认框的用户操作,即用户按下OK或Cancel按钮 下面是我正在使用的示例代码 Private Function GetAle

我在Vb.Net WinForms应用程序中使用SHDocVw.InternetExplorer API在我的应用程序中记录来自Internet Explorer的用户操作

通过使用“user32.dll”的“getForeGroundIndow”,我可以获得确认框的句柄。我可以很容易地获得所需的信息,如“ClassName”和“WindowText”

现在我需要知道关闭确认框的用户操作,即用户按下OK或Cancel按钮

下面是我正在使用的示例代码

Private Function GetAlertMessage() As String
    Dim hwnd = GetForegroundWindow
    Dim ForeGroundWindowClassName As String = GetClassNameFromHandle(hwnd)

    If String.CompareOrdinal("#32770", ForeGroundWindowClassName) = 0 Then
        'Its definitely an alert
        Dim AlertMessage As String = String.Empty

        For Each childWin As WindowChildInfo In GetChildWindows(hwnd)
            If childWin.ClassName.ToLower = "static" Then
                AlertMessage = childWin.Text
            End If
        Next
        Return AlertMessage
    End If
End Function

Public Function GetClassNameFromHandle(ByVal hWnd As Integer) As String
    Dim sbClassName As New Text.StringBuilder("", 256)
    Call GetClassName(hWnd, sbClassName, 256)
    Return sbClassName.ToString
End Function

Private children As List(Of WindowChildInfo)
Public Function GetChildWindows(ByVal hwnd As Integer) As List(Of WindowChildInfo)
    children = New List(Of WindowChildInfo)
    EnumChildWindows(hwnd, AddressOf EnumProc, Nothing)
    Return children
End Function

Private Function EnumProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
    If hwnd <> 0 Then
        children.Add(New WindowChildInfo(hwnd, GetClassNameFromHandle(hwnd), GetText(hwnd)))
    End If
    Return 1
End Function
私有函数GetAlertMessage()作为字符串
Dim hwnd=GetForegroundWindow
Dim ForeGroundIndowClassName作为字符串=GetClassNameFromHandle(hwnd)
如果String.CompareOrdinal(“#32770”,ForeGroundIndowClassName)=0,则
这绝对是一个警告
Dim AlertMessage作为String=String.Empty
对于每个孩子,在GetChildWindows(hwnd)中以WindowChildInfo的形式运行
如果childWin.ClassName.ToLower=“static”,则
AlertMessage=childWin.Text
如果结束
下一个
返回警报消息
如果结束
端函数
公共函数GetClassNameFromHandle(ByVal hWnd作为整数)作为字符串
将sbClassName设置为新文本。StringBuilder(“,256)
调用GetClassName(hWnd,sbClassName,256)
返回sbClassName.ToString
端函数
私有子项作为列表(WindowChildInfo)
公共函数GetChildWindows(ByVal hwnd作为整数)作为(WindowChildInfo的)列表
children=新列表(WindowChildInfo)
EnumChildWindows(hwnd,EnumProc的地址,无)
返回儿童
端函数
私有函数EnumProc(ByVal hwnd为整数,ByVal lParam为整数)为整数
如果hwnd为0,则
添加(新WindowChildInfo(hwnd,GetClassNameFromHandle(hwnd),GetText(hwnd)))
如果结束
返回1
端函数

如果我正确回答了您的问题,您需要在javascript中使用confirm

var action= confirm("your message here");

如果用户按“确定”,则返回true;如果用户按“取消”,则返回false。

在c#中,您可以使用DialogResult类进行此操作。它不是winform对话框,而是在单独运行的Internet Explorer中打开的javascript确认框。您无法执行此操作。您需要处理浏览器中运行的代码产生的JavaScript消息框。枚举按钮控件窗口永远不会告诉您单击了哪一个。我认为OP试图从浏览器外部捕获MessageBoxResult,而不是从浏览器内部捕获MessageBoxResult。他使用的是dot net Winforms,这肯定是一个独立于浏览器本身的桌面应用程序。@inquisitive:是的,你说得绝对正确,“Mairaj”解决方案对我不起作用。