vb 拖动窗体时怎么让另一个窗体也跟着移动,用windows api

kaibaoma 2011-11-16 08:52:00
在vb中怎么实现,拖动一个窗体时,另一个窗体也随之改变。类似于千千静听一样。
注:用windows api实现。
大侠们,帮帮忙。在此先谢过了
...全文
279 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
贝隆 2011-11-18
  • 打赏
  • 举报
回复
不用Hook就能实现,没有那么复杂,看看我在1楼给出的代码就是。
kaibaoma 2011-11-18
  • 打赏
  • 举报
回复
哪里有源码,给个链接呗
哟嚯 2011-11-18
  • 打赏
  • 举报
回复
嗯。要用到Hook。
chinaboyzyq 2011-11-18
  • 打赏
  • 举报
回复

'form1代码
Private Sub Form_Load()
prevWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProc
Form2.Show

End Sub

Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, prevWndProc
End Sub




' ' '新建一个模块,代码如下:

Declare Function CallWindowProc Lib "user32 " Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32 " Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_MOVE = &H3
Public Const WM_SIZE = &H5

Public prevWndProc As Long ' ' ' '默认窗口程序地址


Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error GoTo ShowErr
' ' ' ' ' '处理窗体移动的消息

If Msg = WM_MOVE Or Msg = WM_SIZE Then
''''
Form2.Move Form1.Left, Form1.Top + Form1.Height
End If

' ' ' ' ' '
WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam)
Exit Function
ShowErr:
MsgBox Err.Source & "- " & Err.Description
End Function

rjzhangjun 2011-11-18
  • 打赏
  • 举报
回复
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
Public Declare Function ReleaseCapture Lib "user32" () As Long
这俩是可以解决,看你怎么写了,首先要不停的判断那个所谓的QQ有没有移动
然后
ReleaseCapture
SendMessage hwnd, &HA1, 2, 0 '让被点中的(mousedown)窗口跟着鼠标移动的轨痕走
咸清 2011-11-17
  • 打赏
  • 举报
回复
这个要用Hook⋯⋯
呵呵
我就是说说,我这里没存代码。
kaibaoma 2011-11-16
  • 打赏
  • 举报
回复
如果桌面上有qq窗口(主窗口),和另一个窗口(我写的,副窗口)。当主窗口移动的时候,副窗口也一起移动,用windows api能实现吗
贝隆 2011-11-16
  • 打赏
  • 举报
回复

Option Explicit
Private Const HWND_TOPMOST = -1
Private Const SWP_NOSIZE = &H1
Private Const WM_SYSCOMMAND = &H112&
Private Const SC_MOVE = &HF012&



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 ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then
Call ReleaseCapture
SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0
Form2.Left = Form1.Left
Form2.Top = Form1.Top + Form1.Height + 5
End If
End Sub


Private Sub Form_Load()
Me.BorderStyle = 0
Form2.Show
Form2.BorderStyle = 0
Form2.Left = Form1.Left
Form2.Top = Form1.Top + Form1.Height + 5
End Sub

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧