Visual Basic Tips


フォーム・移動(API)


タイトルバーを消去したフォームは移動できなくなりますが、そのようなフォームを移動させるサンプルです。

'-------------------------------------------------------
'
'-------------------------------------------------------
Dim mlngOLdPos As POINTAPI

Private Sub
Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  mlngOLdPos.x = x / Screen.TwipsPerPixelX
  mlngOLdPos.y = y / Screen.TwipsPerPixelY

  
'スクリーン座標に変換
  
Call ClientToScreen(Me.hwnd, mlngOLdPos)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  
Dim lngNewPos As POINTAPI

  
If Button = vbLeftButton Then
  
Else
    
Exit Sub
  
End If

  lngNewPos.x = x / Screen.TwipsPerPixelX
  lngNewPos.y = y / Screen.TwipsPerPixelY

  
Call ClientToScreen(Me.hwnd, lngNewPos)

  Me.Top = Me.Top + ((lngNewPos.y - mlngOLdPos.y) * Screen.TwipsPerPixelY)
  Me.Left = Me.Left + ((lngNewPos.x - mlngOLdPos.x) * Screen.TwipsPerPixelX)

  mlngOLdPos = lngNewPos
End Sub

'-------------------------------------------------------
' Module
'-------------------------------------------------------
Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Type POINTAPI
  x
As Long
  y
As Long
End Type


DownLoad vbtips101.lzh 2KB (VB6.0)