vb获取notepad.exe的标题

求个运行后能显示出比如“无标题-记事本”之类的vb程序。我需要源码。。本人刚学VB几天,网上的代码复制进来都用不了,求个能直接用的。多谢!
2026年09月26日 01:02
有5个网友回答
网友(1):

‘在窗口上添加一个 CommandButton
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
Private Const isProcessName = "notepad.exe" '设置进程名
Private Sub Command1_Click()
Dim ChildHwnd As Long, Pid As Long, hPro As Long
Dim Tmp As String * 255, ProcessName As String, TmpName As String, WindowText As String
ChildHwnd = GetWindow(GetDesktopWindow, 5)
Do While ChildHwnd <> 0&
If IsWindowVisible(ChildHwnd) Then
GetWindowThreadProcessId ChildHwnd, Pid
hPro = OpenProcess(&H410, 0, Pid)
GetModuleFileNameEx hPro, 0, Tmp, 255

ProcessName = Mid(Tmp, 1, InStr(Tmp, vbNullChar) - 1)
For i = 1 To Len(ProcessName)
TmpName = Right(ProcessName, i)
If Left(TmpName, 1) = "\" Then ProcessName = Mid(TmpName, 2): Exit For
Next
If LCase(ProcessName) = LCase(isProcessName) Then
GetWindowText ChildHwnd, Tmp, 255
WindowText = Mid(Tmp, 1, InStr(Tmp, vbNullChar) - 1)
MsgBox WindowText
End If
CloseHandle hPro
End If
ChildHwnd = GetWindow(ChildHwnd, 2)
Loop
End Sub

网友(2):

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal clsname As String, ByVal winname As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Dim PROCID&, Hand&
Private Sub Command1_Click() Dim sWindowText As String
Dim t As Long
sWindowText = String(255, 0)
Hand = FindWindow("notepad", vbNullString)
GetWindowThreadProcessId Hand, PROCID
t = GetWindowText(Hand, sWindowText, 254)
MsgBox sWindowText 'sWindowText为notepad.exe的标题

End Sub
'msgbox中显示的字符串sWindowText为你要的notepad.exe的标题
'如果你想要其它.exe文件标题,只需要修改相应的FindWindow("notepad", vbNullString)中值:notepad,改为对应的窗口类即可,可以使用spy++等程序获得。

网友(3):

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Sub Command1_Click()

Dim ParentHandle As Long, s As String
Shell "NotePad", vbNormalFocus '打开记事本
ParentHandle = FindWindow("Notepad", vbNullString) '查找窗口
s = String(255, 0)
GetWindowText ParentHandle, s, 255 '获取标题
MsgBox s '显示标题
End Sub

网友(4):

这里有个代码,很不错
http://zhidao.baidu.com/question/306626932.html

网友(5):

在文件中有个生成。exe文件的东西