在VB .NET中,可以使用以下方法来检查唯一打开的窗口是否为当前窗口:
GetForegroundWindow
函数来获取当前活动窗口的句柄。Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")>
Private Shared Function GetForegroundWindow() As IntPtr
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentWindow As IntPtr = GetForegroundWindow()
' 进一步处理当前窗口的逻辑
End Sub
End Class
GetWindowText
函数来获取指定窗口句柄对应的窗口标题。Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentWindow As IntPtr = GetForegroundWindow()
Dim windowTitle As New StringBuilder(256)
GetWindowText(currentWindow, windowTitle, windowTitle.Capacity)
' 进一步处理当前窗口的逻辑
End Sub
End Class
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentWindow As IntPtr = GetForegroundWindow()
Dim windowTitle As New StringBuilder(256)
GetWindowText(currentWindow, windowTitle, windowTitle.Capacity)
Dim expectedWindowTitle As String = "My Unique Window"
If windowTitle.ToString() = expectedWindowTitle Then
' 当前窗口是唯一打开的窗口
Else
' 当前窗口不是唯一打开的窗口
End If
End Sub
End Class
这样,通过获取当前活动窗口的句柄和窗口标题,可以检查唯一打开的窗口是否为当前窗口。请注意,以上代码仅为示例,实际应用中可能需要根据具体情况进行适当的修改和扩展。
参考链接:
领取专属 10元无门槛券
手把手带您无忧上云