在模块中加入
Option Explicit#If Win16 Then
DefInt A-Z
' Required Win16 API declarations
Private Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Public Declare Function SetActiveWindow Lib "User" (ByVal hwnd As Integer) As Integer
Public Declare Function ShowWindow Lib "User" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Private Declare Function GetWindow Lib "User" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Private Declare Function GetWindowText Lib "User" (ByVal hwnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer
Private Declare Function GetParent Lib "User" (ByVal hwnd As Integer) As Integer
Public Declare Function IsIconic Lib "User" (ByVal hwnd As Integer) As Integer
#ElseIf Win32 Then
DefLng A-Z
' Required Win32 API declarations
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetActiveWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) 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 GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
#End If
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_TOPMOST = -1
'Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_NOTOPMOST = -2' Constants used with APIs
Public Const SW_RESTORE = 9' Constant used by GetWindowWord to find next window
Public Const GW_HWNDNEXT = 2' Constants used by FindWindowPartial
Public Const FWP_STARTSWITH = 0
Public Const FWP_CONTAINS = 1Public Sub AppActivatePartial(TitleContains$, Method%)
Dim hWndApp 'As SysInt
Dim nRet As Long
'
' Retrieve window handle for first top-level window
' that starts with or contains the passed string.
'
hWndApp = FindWindowPartial(TitleContains, Method)
If hWndApp Then
'
' Switch to it, restoring if need be.
'
If IsIconic(hWndApp) Then
Call ShowWindow(hWndApp, SW_RESTORE)
End If
nRet = SetActiveWindow(hWndApp)
SendKeys "abAFLJASLDFJALSDFJLADSJFSJDALSJDc" & vbCrLf
Else
'
' Alert user that request failed.
'
MsgBox "No matching applications found."
End If
End SubPublic Function FindWindowPartial(TitleStart$, Method%) As Long
Dim hWndTmp 'As SysInt
Dim nRet 'As SysInt
Dim TitleTmp As String
'
' Find first window and loop through all subsequent
' windows in master window list.
'
hWndTmp = FindWindow(vbNullString, vbNullString)
Do Until hWndTmp = 0
'
' Make sure this window has no parent.
'
If GetParent(hWndTmp) = 0 Then
'
' Retrieve caption text from current window.
'
TitleTmp = Space(256)
nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
If nRet Then
'
' Clean up return string, preparing for
' case-insensitive comparison.
'
TitleTmp = UCase(Left(TitleTmp, nRet))
'
' Use appropriate method to determine if
' current window's caption either starts
' with or contains passed string.
'
Select Case Method
Case FWP_STARTSWITH
If InStr(TitleTmp, UCase(TitleStart)) = 1 Then
FindWindowPartial = hWndTmp
Exit Do
End If
Case FWP_CONTAINS
If InStr(TitleTmp, UCase(TitleStart)) Then
FindWindowPartial = hWndTmp
Exit Do
End If
End Select
End If
End If
'
' Get next window in master window list and continue.
'
hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
Loop
End Function
Private Sub Command1_Click()
dim conf As ConfigClass
dim hwnd as long
conf.title="你应用程序的标题"'就是form.caption的内容
hwnd = FindWindowPartial(conf.title, 0)
SetActiveWindow (hwnd)
end sub
Option Explicit#If Win16 Then
DefInt A-Z
' Required Win16 API declarations
Private Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Public Declare Function SetActiveWindow Lib "User" (ByVal hwnd As Integer) As Integer
Public Declare Function ShowWindow Lib "User" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Private Declare Function GetWindow Lib "User" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Private Declare Function GetWindowText Lib "User" (ByVal hwnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer
Private Declare Function GetParent Lib "User" (ByVal hwnd As Integer) As Integer
Public Declare Function IsIconic Lib "User" (ByVal hwnd As Integer) As Integer
#ElseIf Win32 Then
DefLng A-Z
' Required Win32 API declarations
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetActiveWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) 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 GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
#End If
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_TOPMOST = -1
'Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_NOTOPMOST = -2' Constants used with APIs
Public Const SW_RESTORE = 9' Constant used by GetWindowWord to find next window
Public Const GW_HWNDNEXT = 2' Constants used by FindWindowPartial
Public Const FWP_STARTSWITH = 0
Public Const FWP_CONTAINS = 1Public Sub AppActivatePartial(TitleContains$, Method%)
Dim hWndApp 'As SysInt
Dim nRet As Long
'
' Retrieve window handle for first top-level window
' that starts with or contains the passed string.
'
hWndApp = FindWindowPartial(TitleContains, Method)
If hWndApp Then
'
' Switch to it, restoring if need be.
'
If IsIconic(hWndApp) Then
Call ShowWindow(hWndApp, SW_RESTORE)
End If
nRet = SetActiveWindow(hWndApp)
SendKeys "abAFLJASLDFJALSDFJLADSJFSJDALSJDc" & vbCrLf
Else
'
' Alert user that request failed.
'
MsgBox "No matching applications found."
End If
End SubPublic Function FindWindowPartial(TitleStart$, Method%) As Long
Dim hWndTmp 'As SysInt
Dim nRet 'As SysInt
Dim TitleTmp As String
'
' Find first window and loop through all subsequent
' windows in master window list.
'
hWndTmp = FindWindow(vbNullString, vbNullString)
Do Until hWndTmp = 0
'
' Make sure this window has no parent.
'
If GetParent(hWndTmp) = 0 Then
'
' Retrieve caption text from current window.
'
TitleTmp = Space(256)
nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
If nRet Then
'
' Clean up return string, preparing for
' case-insensitive comparison.
'
TitleTmp = UCase(Left(TitleTmp, nRet))
'
' Use appropriate method to determine if
' current window's caption either starts
' with or contains passed string.
'
Select Case Method
Case FWP_STARTSWITH
If InStr(TitleTmp, UCase(TitleStart)) = 1 Then
FindWindowPartial = hWndTmp
Exit Do
End If
Case FWP_CONTAINS
If InStr(TitleTmp, UCase(TitleStart)) Then
FindWindowPartial = hWndTmp
Exit Do
End If
End Select
End If
End If
'
' Get next window in master window list and continue.
'
hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
Loop
End Function
Private Sub Command1_Click()
dim conf As ConfigClass
dim hwnd as long
conf.title="你应用程序的标题"'就是form.caption的内容
hwnd = FindWindowPartial(conf.title, 0)
SetActiveWindow (hwnd)
end sub
解决方案 »
- 运行程序后在某个指定目录生成另一个程序呢?
- VB+Access问题,请求代码回复,谢谢!
- 求袁飞yunafei dnetmenu controlb 下载?
- vb下如何对指定目录下的文件改名,然后移动到另外的目录
- 【在线等,立给分】VB6窗体大小的值怎么是4800多?
- 关于数据库的操作效率问题。望高手不吝赐教。
- API问题》(如何Show出文件夹选项...)?具体的值如何设置?
- VB 中读取Word文档出现的错误,麻烦各位大神帮忙解决一下
- 请问有谁是编程沙龙的会员?帮我下载一个文件给高分谢谢!!
- 如何动态创建ODBC?
- 第一次作报表,有些问题不明白?
- 求一个图像识别的例子,要能识别手形的,提供参考例子,方法也可以
Private Sub Command1_Click()
dim conf As ConfigClass
dim hwnd as long
conf.title="你应用程序的标题"'就是form.caption的内容
hwnd = FindWindowPartial(conf.title, 0)
SetActiveWindow (hwnd)
end sub
Call ShowWindow(hwnd, SW_RESTORE)