vb 对另一个程序设置焦点,很急!!

vb 对另一个程序设置焦点,很急!!,第1张

运用api

Public Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

用getwindow取老纳得另一程序句柄,及要缺含哗设置焦点的控件的句柄,然后用sendmessage进行设置和控件。

你可以baidu一伏行下这两个api的用法。

'以下代码必须生成EXE才有效

Private 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

Private Declare Function FindWindowEx Lib "衡态user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function GetClassName Lib "辩缓user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Private Type POINTAPI

x As Long

y As Long

End Type

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Private Type WINDOWPLACEMENT

Length As Long

flags As Long

showCmd As Long

ptMinPosition As POINTAPI

ptMaxPosition As POINTAPI

rcNormalPosition As RECT

End Type

Private Sub Form_Load()

Dim hwnd As Long

Dim cname As String

cname = String(255, Chr(0))

If App.PrevInstance = True Then

GetClassName Me.hwnd, cname, 255

hwnd = FindWindowEx(0, Me.hwnd, cname, vbNullString)

Print hwnd

Dim ps As WINDOWPLACEMENT

GetWindowPlacement hwnd, ps

If hwnd <>0 Then

SetWindowPos hwnd, 0, ps.rcNormalPosition.Left, ps.rcNormalPosition.Top, ps.rcNormalPosition.Right - ps.rcNormalPosition.Left, ps.rcNormalPosition.Bottom - ps.rcNormalPosition.Top, 0

End

End If

End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12348358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存