vb窗体同步移动

'创建一个工程,包含Form1、Form2和一个标准模块,Form1是启动对象
'From1代码
Option Explicit
 
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
'标准模块代码
Option Explicit
Public Declare Function CallWindowProc Lib "user32 " Alias "CallWindowProcA" (ByVal lpPrevWndFunc As LongByVal hwnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As LongAs Long
Public Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA" (ByVal hwnd As LongByVal nIndex As LongByVal dwNewLong As LongAs Long
Public Declare Function GetWindowLong Lib "user32 " Alias "GetWindowLongA" (ByVal hwnd As LongByVal nIndex As LongAs 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 LongByVal Msg As LongByVal wParam As LongByVal lParam As LongAs 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
posted @ 2015-11-30 10:20  不嫌不闲  阅读(299)  评论(0)    收藏  举报