【发布时间】:2015-07-19 05:57:12
【问题描述】:
在我的一个项目中,我必须获得前景窗口的标题,所以我调用 GetForegroundWindow() 入口点表单 User32.dll 来获取窗口句柄,然后我调用 GetWindowText() 来获取标题,但输出的错误更少什么都没有,这是我在 VB.NET 程序中使用的代码。
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hwnd As Long, ByVal lpString As System.Text.StringBuilder, ByVal cch As Long) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hWnd As IntPtr
hWnd = GetForegroundWindow()
Dim title As New System.Text.StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
Me.Text = title.ToString
End Sub
End Class
【问题讨论】:
-
这是一个 VB6 声明,一个旧的 VB 版本,最初是一个 16 位开发工具。您总是可以通过查看
Long来判断。它是 VB.NET 中的Integer。通过访问例如 pinvoke.net 网站获取最新的声明。
标签: vb.net dllimport hwnd entry-point