標準モジュール 'http://rucio.o.oo7.jp/main/tyukyu/tyukyu9.htm '□API関数 Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public 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 Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _ (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long '□SetWindowLongで使用 Private Const GWL_WNDPROC = -4 '□メッセージ Private Const WM_CONTEXTMENU = &H7B '右クリックWM_PAINT Private Const WM_PAINT = &HF 'ウィンドウを再描画する必要がある Private Const WM_MOVE = &H3 'ウィンドウ移動 '□コレクション すべてウィンドウハンドルがキー Dim colDProc As Collection '現在サブクラス化されているコントロールの元のWindowsProcのアドレス Public sy As Long Public sx As Long Public ny As Long Public nx As Long '■WindowProc '■機能:メッセージを横取りする。 '■備考:この関数はコールバック関数なので定義を変えてはいけない! Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim DefaultProc As Long Dim c As Long Select Case uMsg Case WM_PAINT 'ウィンドウを再描画する必要がある UserForm1.Label11.Caption = c c = c + 1 Exit Function 'https://oshiete.goo.ne.jp/qa/4620863.html Case WM_MOVE 'ウィンドウを再描画する必要がある UserForm1.Label4.Caption = sx UserForm1.Label5.Caption = sy UserForm1.Label6.Caption = UserForm1.Left - sx UserForm1.Label7.Caption = UserForm1.Top - sy UserForm1.Label8.Caption = UserForm1.Left UserForm1.Label9.Caption = UserForm1.Top ny = UserForm1.Top nx = UserForm1.Left Call MOVEs '・・・・・・これが再描画 Exit Function 'WM_MOVE Case WM_CONTEXTMENU '右クリック MsgBox "右" Exit Function End Select CONTINUE: '引当のWindowProcへメッセージを回す。 DefaultProc = colDProc(CStr(hwnd)) WindowProc = CallWindowProc(DefaultProc, hwnd, uMsg, wParam, lParam) End Function '■BeginSubClass '■機能:サブクラス化を開始する。 Public Sub BeginSubClass(l As Long) Static bAlready As Boolean Dim DefaultProc As Long If Not bAlready Then Set colDProc = New Collection bAlready = True End If 'サブクラス化実行 DefaultProc = SetWindowLong(l, GWL_WNDPROC, AddressOf WindowProc) '元のWindowProcのアドレスを保存 colDProc.Add DefaultProc, CStr(l) End Sub '■EndSubClass '■機能:サブクラス化を終了します。 Public Sub EndSubClass(l As Long) Dim Ret As Long Dim DefaultProc As Long 'WindowProcのアドレスを元に戻す。 DefaultProc = colDProc(CStr(l)) Ret = SetWindowLong(l, GWL_WNDPROC, DefaultProc) colDProc.Remove CStr(l) End Sub Sub MOVEs() 再描画 End Sub