ホーム > カテゴリ > Visual Basic >

マウス操作でコントロールを移動する

マウス操作でコントロールを移動するサンプルです。

サンプルの実行画面

ソースコード

[MoveCtl.frm]

'■SendMessage
'指定のウインドウにメッセージを送る

'<引数>
'hWnd     ウインドウのハンドル
'wMsg:  定数(WM_××参照)
'wParam:  定数 (HTCAPTION)
'lParam:  常に0

'<戻り値>
'通常使わない

'定数はこれ以外にもある=>APIビューワー参照

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

'■ReleaseCapture
'マウスキャプチャを解放

Private Declare Sub ReleaseCapture Lib "user32" ()

Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2


Private Sub command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Ret As Long
    
      If Button = 1 Then
          'マウスキャプチャを解放する
          ReleaseCapture

          'Form1をドラッグせよという命令を送る
          Ret = SendMessage(Command1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

      End If

End Sub


Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Ret As Long
    
      If Button = 1 Then
          'マウスキャプチャを解放する
          ReleaseCapture

          'Form1をドラッグせよという命令を送る
          Ret = SendMessage(Text1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

      End If

End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Ret As Long
    
      If Button = 1 Then
          'マウスキャプチャを解放する
          ReleaseCapture

          'Form1をドラッグせよという命令を送る
          Ret = SendMessage(Picture1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

      End If

End Sub

Private Sub File1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Ret As Long
    
      If Button = 1 Then
          'マウスキャプチャを解放する
          ReleaseCapture

          'Form1をドラッグせよという命令を送る
          Ret = SendMessage(File1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

      End If

End Sub


Private Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim Ret As Long
    
      If Button = 1 Then
          'マウスキャプチャを解放する
          ReleaseCapture

          'Form1をドラッグせよという命令を送る
          Ret = SendMessage(Option1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

      End If

End Sub

ソースコード一式のダウンロード

vbapi_movectl.zip 1.20 KB (1,231 バイト)

このサンプルの動作環境について

このサンプルは 「Windows98」及び「Microsoft Visual Basic 5.0 Professional Edition」で確認しております。環境が異なる場合は正常に動作しない場合もございますのでご了承下さい。





関連記事



公開日:2015年03月04日
記事NO:00368