'WindowsAPI関数の宣言 Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" _ (ByVal hWnd As Integer, ByVal nIndex As Integer, <MarshalAs(UnmanagedType.FunctionPtr)> ByVal dwNewInteger As fncKakuDragDelegate) As Integer Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" _ (ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal dwNewInteger As Integer) As Integer Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" _ (ByVal lpPrevWndFunc As Integer, ByVal hWnd As Integer, ByVal MSG As Integer, _ ByVal wParam As Integer, ByVal lParam As Integer) As Integer Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _ (ByVal Destination As Integer, ByVal Source As Integer, ByVal Length As Integer) Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _ (ByRef Destination As RECT, ByVal Source As Integer, ByVal Length As Integer) Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _ (ByVal Destination As Integer, ByRef Source As RECT, ByVal Length As Integer) 'RECT構造体の宣言 Structure RECT Dim Left As Integer Dim Top As Integer Dim Right As Integer Dim Bottom As Integer End Structure '定数の宣言 Public Const GWL_WNDPROC = (-4) Public Const WM_SIZING = &H214 'もともとのウィンドウ関数のアドレス Public OldWp As Integer
'デリゲートの宣言 Delegate Function fncKakuDragDelegate( _ ByVal hWnd As Integer, ByVal uMsg As Integer, _ ByVal wParam As Integer, ByVal lParam As Integer) As Integer Public Function fncKakuDrag( _ ByVal hWnd As Integer, ByVal uMsg As Integer, _ ByVal wParam As Integer, ByVal lParam As Integer) As Integer '*** フックしたメッセージの振り分けと対処 Select Case uMsg Case WM_SIZING Dim wkRect As RECT Dim Length As Integer = Marshal.SizeOf(wkRect) MoveMemory(wkRect, lParam, Length) wkRect.Right = wkRect.Left + 250 wkRect.Bottom = wkRect.Top + ((wkRect.Bottom - wkRect.Top) \ 50) * 50 MoveMemory(lParam, wkRect, Length) Case Else Return CallWindowProc(OldWp, hWnd, uMsg, wParam, lParam) End Select Return 0 End Function
'フック開始 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load OldWp = SetWindowLong(Me.Handle.ToInt32, GWL_WNDPROC, AddressOf fncKakuDrag) End Sub
'フック終了 Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FormClosed OldWp = SetWindowLong(Me.Handle.ToInt32, GWL_WNDPROC, OldWp) End Sub