改了一下 Private Declare Function GetCursorPos Lib "user32 " (lpPoint As POINTAPI) As Long Private Declare Function GetWindowRect Lib "user32 " (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function PtInRect Lib "user32 " (lpRect As RECT, ByVal ptx As Long, ByVal pty As Long) As Long Private Declare Function SetWindowPos Lib "user32 " (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Declare Function MoveWindow Lib "user32 " (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As LongConst HWND_TOPMOST = -1
Private Type POINTAPI X As Long Y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypeDim Is_Move_B As Boolean Dim Is_Movestar_B As Boolean Dim MyRect As RECT Dim MyPoint As POINTAPI Dim Movex As Long, Movey As Long Dim Max As Long Private Sub Command1_Click() End End SubPrivate Sub Form_Load() Timer1.Interval = 50 Label1.Top = 0 Label1.Left = 0 Label1.Width = Form1.Width
Form1.Left = Screen.Width - 30 Max = Form1.Width GetWindowRect Form1.hwnd, MyRect End SubPrivate Sub Form_Paint() '使窗体始终置于最右面 If PtInRect(MyRect, MyPoint.X, MyPoint.Y) = 0 And Me.Top < 30 Then SetWindowPos Me.hwnd, HWND_TOPMOST, (Screen.Width - Form1.Width) / Screen.TwipsPerPixelX, Me.Top / Screen.TwipsPerPixelY, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY, &H20 End If End SubPrivate Sub Timer1_Timer() GetWindowRect Form1.hwnd, MyRect GetCursorPos MyPoint If Form1.Left > Screen.Width - Form1.Width Then Form1.Left = Screen.Width - 30 Form1.Width = 30 Exit Sub End If ' 如果鼠标移动到窗体中,则显示窗体 If (PtInRect(MyRect, MyPoint.X, MyPoint.Y) And Form1.Width = 30) Then Form1.Width = Max Form1.Left = Screen.Width - Form1.Width
'判断鼠标指针是否位于窗体拖动区 If MyPoint.X - MyRect.Left <= 10 Or Is_Movestar_B Then Screen.MousePointer = 15 Is_Move_B = True Else Screen.MousePointer = 0 Is_Move_B = False End If Else ' 显示一部分窗体标志窗体在屏幕顶部的位置 If Not Is_Movestar_B And Me.Left >= Screen.Width - 30 Then Form1.Left = Screen.Width - 30 Form1.Width = 30 End If End If End Sub ' 开始移动 Private Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Is_Move_B Then Movex = MyPoint.X - MyRect.Left Movey = MyPoint.Y - MyRect.Top Is_Movestar_B = True End If End Sub' 移动窗体 Private Sub label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Is_Movestar_B Then MoveWindow Form1.hwnd, MyPoint.X - Movex, MyPoint.Y - Movey, MyRect.Right - MyRect.Left, MyRect.Bottom - MyRect.Top, -1 End If End Sub' 结束移动 Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Is_Movestar_B = False End Sub
Private Declare Function GetCursorPos Lib "user32 " (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowRect Lib "user32 " (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function PtInRect Lib "user32 " (lpRect As RECT, ByVal ptx As Long, ByVal pty As Long) As Long
Private Declare Function SetWindowPos Lib "user32 " (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function MoveWindow Lib "user32 " (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As LongConst HWND_TOPMOST = -1
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypeDim Is_Move_B As Boolean
Dim Is_Movestar_B As Boolean
Dim MyRect As RECT
Dim MyPoint As POINTAPI
Dim Movex As Long, Movey As Long
Dim Max As Long
Private Sub Command1_Click()
End
End SubPrivate Sub Form_Load()
Timer1.Interval = 50
Label1.Top = 0
Label1.Left = 0
Label1.Width = Form1.Width
Form1.Left = Screen.Width - 30
Max = Form1.Width
GetWindowRect Form1.hwnd, MyRect
End SubPrivate Sub Form_Paint()
'使窗体始终置于最右面
If PtInRect(MyRect, MyPoint.X, MyPoint.Y) = 0 And Me.Top < 30 Then
SetWindowPos Me.hwnd, HWND_TOPMOST, (Screen.Width - Form1.Width) / Screen.TwipsPerPixelX, Me.Top / Screen.TwipsPerPixelY, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY, &H20
End If
End SubPrivate Sub Timer1_Timer()
GetWindowRect Form1.hwnd, MyRect
GetCursorPos MyPoint
If Form1.Left > Screen.Width - Form1.Width Then
Form1.Left = Screen.Width - 30
Form1.Width = 30
Exit Sub
End If
' 如果鼠标移动到窗体中,则显示窗体
If (PtInRect(MyRect, MyPoint.X, MyPoint.Y) And Form1.Width = 30) Then
Form1.Width = Max
Form1.Left = Screen.Width - Form1.Width
'判断鼠标指针是否位于窗体拖动区
If MyPoint.X - MyRect.Left <= 10 Or Is_Movestar_B Then
Screen.MousePointer = 15
Is_Move_B = True
Else
Screen.MousePointer = 0
Is_Move_B = False
End If
Else ' 显示一部分窗体标志窗体在屏幕顶部的位置
If Not Is_Movestar_B And Me.Left >= Screen.Width - 30 Then
Form1.Left = Screen.Width - 30
Form1.Width = 30
End If
End If
End Sub
' 开始移动
Private Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Is_Move_B Then
Movex = MyPoint.X - MyRect.Left
Movey = MyPoint.Y - MyRect.Top
Is_Movestar_B = True
End If
End Sub' 移动窗体
Private Sub label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Is_Movestar_B Then
MoveWindow Form1.hwnd, MyPoint.X - Movex, MyPoint.Y - Movey, MyRect.Right - MyRect.Left, MyRect.Bottom - MyRect.Top, -1
End If
End Sub' 结束移动
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Is_Movestar_B = False
End Sub