VB API函数移动窗口

2025-04-15 13:10:52
推荐回答(2个)
回答1:

前提是DNF的标题你知道不?
我写的下面的代码,点击DNF窗体后按[Home]键就能获取句柄,然后控制DNF,可移动,可改变其大小,在窗体上按住右键并移动来确定DNF在屏幕中的位置

'添加时钟控件Timer1,然后复制代码即可
''''''''''''''移动窗体
Private 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
''''''''''''''获取活动窗口句柄
Private Declare Function GetForegroundWindow Lib "user32" () As Long
''''''''''''''判断按键是否按下
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
''''''''''''''获取窗体标题
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)

Dim intGetWindowHWnd As Long
Dim myWidth, myHeight As Long
Dim getWidth As Long
Dim getHeight As Long

Public Sub TMS(hwnd As Long)
Dim ExsTyle As Long, crKey As Long, bAlpha As Byte, dwFlags As Long
ExsTyle = GetWindowLong(hwnd, GWL_EXSTYLE)
ExsTyle = ExsTyle Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, ExsTyle
dwFlags = 2
bAlpha = 150
SetLayeredWindowAttributes hwnd, 0, bAlpha, dwFlags
End Sub

Private Sub Form_Load()
On Error Resume Next
Timer1.Interval = 100
Timer1.Enabled = True
Call TMS(Me.hwnd)
Me.BackColor = vbBlack
Me.AutoRedraw = True
Me.ForeColor = vbGreen
Print: Print "请调整本窗体大小使其与须控制窗口大小相同!"
Me.ForeColor = vbYellow
Print: Print "请将鼠标点击需要控制的窗口,然后按下【Home键】"
myWidth = Width: myHeight = Height
End Sub

Private Sub Form_MouseDown(button As Integer, Shift As Integer, x As Single, y As Single)
On Error Resume Next
If button = 2 Then
Call FormMove(intGetWindowHWnd, getWidth, getHeight, x, y)
End If
End Sub

Private Sub Form_MouseMove(button As Integer, Shift As Integer, x As Single, y As Single)
If button = 2 Then
If FormMove(intGetWindowHWnd, getWidth, getHeight, x, y) Then
Cls
Me.ForeColor = vbRed
Print: Print "移动窗体成功!你可以继续以下操作"
Me.ForeColor = vbGreen
Print: Print "请调整本窗体大小使其与须控制窗口大小相同!"
Me.ForeColor = vbYellow
Print: Print "请将鼠标点击需要控制的窗口,然后按下【Home键】"
End If
End If
End Sub

Private Function FormMove(hwnd As Long, gWidth As Long, gHeight As Long, x As Single, y As Single)
Dim aLeft, aTop, aWidth, aHeight As Long
On Error Resume Next
aLeft = (Screen.Width / Width * x) / Screen.TwipsPerPixelX
aTop = (Screen.Height / Height * y) / Screen.TwipsPerPixelY
aWidth = gWidth / Screen.TwipsPerPixelX
aHeight = gHeight / Screen.TwipsPerPixelY
FormMove = MoveWindow(hwnd, aLeft, aTop, aWidth, aHeight, True)
End Function

Private Sub Timer1_Timer()
Dim strTitle As String * 1024
If GetAsyncKeyState(vbKeyHome) Then
getWidth = Me.Width: getHeight = Me.Height
Me.Width = myWidth: Me.Height = myHeight
intGetWindowHWnd = GetForegroundWindow()
Call GetWindowText(intGetWindowHWnd, strTitle, 1024)
Caption = "窗体移动:" & strTitle
Cls
Me.ForeColor = vbGreen
Print: Print "请调整本窗体大小使其与须控制窗口大小相同!"
Me.ForeColor = vbYellow
Print: Print "请将鼠标点击需要控制的窗口,然后按下【Home键】"
Me.ForeColor = vbRed
Print: Print "获取目标句柄成功成功!请按以下步骤继续操作.."
Me.ForeColor = RGB(150, 150, 255)
Print: Print "请按住鼠标右键在本窗体中移动可控制指定的窗体移动"
End If
End Sub

虽然很长,但是很完整,毕竟是我做好的程序而不仅是为了答题

回答2:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (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
说明

Dim hTMainForm as long '找句柄
hTMainForm = FindWindow("", "(游戏的标题)")

movewindow hTMainForm,0(窗口新的左侧位置),0(窗口新的顶部位置),1000(长),1000(宽),1