如果不知道窗体的标题用:Private Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Sub Timer1_Timer()
Label1 = "这个程序的句柄是" & GetForegroundWindow
End Sub
如果知道窗体标题用:Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Form_Load()
Dim 句柄 As Long
句柄 = FindWindow(vbNullString, "窗体名")
MsgBox 句柄
End Sub
Label1 = "这个程序的句柄是" & GetForegroundWindow
End Sub
如果知道窗体标题用:Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Form_Load()
Dim 句柄 As Long
句柄 = FindWindow(vbNullString, "窗体名")
MsgBox 句柄
End Sub
如果SURB有窗口标题,那么一运行就能得到他的句柄,不管他是否获得焦点
我可不可以通过获得的句柄来判断是不是 surf.exe.因为surf.exe每次启动的标题栏都不一样。
我也很想知道有什么办法能在程序一运行时就能得到所有已打开的窗口的句柄,并用SHOWWINDOW或CLOSEWINDOW来处理,这个问题我出了60分不过没有人告诉我
Get/Set Window Caption AND Get Topmost Window
This code will first allow you to get the HWND of the foreground window, or the window with focus. Then it will capture
the title of the window and reverse it.
visual basic code:--------------------------------------------------------------------------------Option Explicit'Declare API:
Private Declare Function GetForegroundWindow 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 SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String) As LongPrivate Sub Command1_Click()
Dim lhWnd As Long, strCaption As String 'create our buffer for the caption:
strCaption = String(100, Chr$(0))
'get the topmost window:
lhWnd = GetForegroundWindow()
'get the caption
GetWindowText lhWnd, strCaption, 100
'clear the buffer:
strCaption = Left(strCaption, InStr(strCaption, Chr(0)) - 1)
'reverse the string and set the new caption:
strCaption = StrReverse(strCaption)
SetWindowText lhWnd, strCaption
End Sub--------------------------------------------------------------------------------
Set Window Parent
This code, although not pretty, makes the VB form the parent of an instance of notepad, thus making notepad "trapped"
into that window.
visual basic code:--------------------------------------------------------------------------------Option Explicit'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As LongPrivate Sub Form_Load()
Dim lhWnd As Long 'Get Notepads HWND:
lhWnd = FindWindow("Notepad", vbNullString)
'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
'set the parent:
SetParent lhWnd, Me.hwnd
End If
End Sub--------------------------------------------------------------------------------
Execute a file in it's default program
This example opens a text file in notepad (if that's the default program)
visual basic code:--------------------------------------------------------------------------------Option Explicit'declare constants:
Private Const SW_SHOWNORMAL = 1'declare API:
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As _
String, ByVal nShowCmd As Long) As LongPrivate Sub Form_Load()
Dim lError As Long 'launch C:\movies.txt, given that it exists:
lError = ShellExecute(Me.hwnd, vbNullString, "C:\movies.txt", vbNullString, _
"C:\", SW_SHOWNORMAL)
'if returns 2:
If lError = 2 Then
MsgBox "File does not exist!"
End If
End Sub--------------------------------------------------------------------------------
__________________
Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
说明
获取一个已装载模板的完整路径名称
返回值
Long,如执行成功,返回复制到lpFileName的实际字符数量;零表示失败。会设置GetLastError
参数表
参数 类型及说明
hModule Long,一个模块的句柄。可以是一个DLL模块,或者是一个应用程序的实例句柄
lpFileName String,指定一个字串缓冲区,要在其中容纳文件的用NULL字符中止的路径名,hModule模块就是从这个文件装载进来的
nSize Long,装载到缓冲区lpFileName的最大字符数量
注解
在Windows 95下,函数会核查应用程序的内部版本号是否为4.0或更大的一个数字。如果是,就返回一个长文件名,否则返回短文件名