在模块中声明 Public Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As LongPublic Type pointapi x As Long y As Long End Type 在窗体中声明Option Explicit Dim M As pointapi Dim a As Long Private Sub Form_Load() Me.Height = Screen.Height Me.Top = 0 Me.Left = 0 - Me.Width End SubPrivate Sub Timer1_Timer() a = GetCursorPos(M) If M.x = 0 Then Me.Left = 0 End If If M.x > Me.ScaleWidth Then Me.Left = 0 - Me.Width End If End Sub 运行就知道了
新建一个模块,下面是里面的代码: Attribute VB_Name = "mdlWindow" Public 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 Option ExplicitSub Main() Dim lonerrorcode As Long frmWindow.Show lonerrorcode = SetWindowPos(frmWindow.hwnd, -1, frmWindow.Left, _ frmWindow.Top, frmWindow.Left + frmWindow.Width, _ frmWindow.Top + frmWindow.Height, &H1) DoEvents End Sub 再建一个窗体,代码: VERSION 5.00 Begin VB.Form frmWindow BackColor = &H00800080& BorderStyle = 0 'None Caption = "浮动窗体示例" ClientHeight = 1005 ClientLeft = 105 ClientTop = 105 ClientWidth = 3405 FillColor = &H00000080& LinkTopic = "Form1" ScaleHeight = 1005 ScaleWidth = 3405 ShowInTaskbar = 0 'False StartUpPosition = 3 '窗口缺省 Begin VB.Timer tmrGetMouseXY Enabled = 0 'False Interval = 1000 Left = 1680 Top = 120 End Begin VB.Timer tmrWindow Interval = 1000 Left = 360 Top = 240 End Begin VB.Label lblWindow BackColor = &H0080FF80& Caption = "Label1" BeginProperty Font Name = "宋体" Size = 14.25 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 375 Index = 0 Left = 2040 TabIndex = 0 Top = 480 Width = 1215 End Begin VB.Label lblWindow BackColor = &H0080FF80& Caption = "现在的时间是:" BeginProperty Font Name = "宋体" Size = 14.25 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 375 Index = 3 Left = 120 TabIndex = 3 Top = 480 Width = 2175 End Begin VB.Label lblWindow BackColor = &H00404040& Caption = "Label1" BeginProperty Font Name = "宋体" Size = 14.25 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 495 Index = 2 Left = 1200 TabIndex = 2 Top = 120 Width = 2055 End Begin VB.Label lblWindow BackColor = &H0080C0FF& Caption = "今天是:" BeginProperty Font Name = "宋体" Size = 14.25 Charset = 134 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 495 Index = 1 Left = 120 TabIndex = 1 Top = 120 Width = 1215 End End Attribute VB_Name = "frmWindow" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Private Type POINTAPI x As Long y As Long End Type Private Declare Function GetCursorPos Lib "user32" _ (lpPoint As POINTAPI) As LongDim sinMouseUpXPosition As Single Dim sinMouseMoveXPosition As Single Dim sinMouseDownXPosition As Single Dim sinMouseUpYPosition As Single Dim sinMouseMoveYPosition As Single Dim sinMouseDownYPosition As Single Dim blnFormHide As Boolean '表示隐藏窗体。 Dim blnMouseDown As Boolean '表示鼠标按下! Dim blnMouseRightButtonDown As Boolean '表示鼠标右键按下! Const LINEWIDTH = 50 '以下两个变量是为了表示lblWindow标签的颜色的! Dim lonColor1 As Long Dim lonColor2 As Long '以下这四个变量,是为了调用API得到鼠标的位置的。 Dim sinMouseXPositionOld As Single Dim sinMouseYPositionOld As Single Dim sinMouseXPositionNew As Single Dim sinMouseYPositionNew As SingleDim blnRight As Boolean Dim blnLeft As Boolean Dim blnTop As Boolean Dim blnButton As Boolean Private Sub Form_DblClick() If blnMouseRightButtonDown Then End If End SubPrivate Sub Form_Load() lblWindow(0).Caption = Time lblWindow(2).Caption = Date lonColor1 = lblWindow(1).BackColor lonColor2 = lblWindow(3).BackColor lblWindow(2).BackColor = lblWindow(1).BackColor lblWindow(0).BackColor = lblWindow(3).BackColor End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) sinMouseDownXPosition = x sinMouseDownYPosition = y blnMouseDown = True If Button = 2 Then If MsgBox("确实要退出吗?", vbYesNo) = vbYes Then End End If blnMouseRightButtonDown = True blnMouseDown = False End If End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If blnFormHide Then With Me If .Top < 0 Then .Top = 0 If .Top > Screen.Height - LINEWIDTH - 10 Then .Top = Screen.Height - .Height End If If .Left < 0 Then .Left = 0 If .Left > Screen.Width - LINEWIDTH - 10 Then .Left = Screen.Width - .Width End If End With blnFormHide = False tmrGetMouseXY.Enabled = True End If sinMouseMoveXPosition = x sinMouseMoveYPosition = y If blnMouseDown Then Me.Left = Me.Left + sinMouseMoveXPosition - sinMouseDownXPosition Me.Top = Me.Top + sinMouseMoveYPosition - sinMouseDownYPosition End If End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) sinMouseUpXPosition = x sinMouseUpYPosition = y blnMouseDown = False blnMouseRightButtonDown = False blnFormHide = False With Me If .Left < 10 Then .Left = LINEWIDTH - .Width blnFormHide = True ' blnLeft = True End If If .Top < 10 Then .Top = LINEWIDTH - .Height blnFormHide = True ' blnTop = True End If If .Top + .Height > Screen.Height Then .Top = Screen.Height - LINEWIDTH blnFormHide = True ' blnTop = True End If If .Left + .Width > Screen.Width Then .Left = Screen.Width - LINEWIDTH blnFormHide = True ' blnButton = True End If End With End SubPrivate Sub lblWindow_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single) Call Form_MouseDown(Button, Shift, x, y) End SubPrivate Sub lblWindow_MouseMove(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single) Call Form_MouseMove(Button, Shift, x, y) End SubPrivate Sub lblWindow_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single) Call Form_MouseUp(Button, Shift, x, y) End Sub'Private Sub Timer1_Timer() 'GetCursorPos mouseposition 'If mouseposition.x = 1 Then 'If mouseposition.y = 2 Then 'End If 'End If'End SubPrivate Sub tmrGetMouseXY_Timer() Dim lonerrorcode As Long Dim typMouseXY As POINTAPI lonerrorcode = GetCursorPos(typMouseXY) With Me If typMouseXY.x > .Left + .Width Then Call HideForm End If If typMouseXY.x < .Left Then Call HideForm End If If typMouseXY.y > .Top + .Height Then Call HideForm End If If typMouseXY.y < .Top Then Call HideForm End If End With End SubPrivate Sub tmrWindow_Timer() lblWindow(0).Caption = Time lblWindow(2).Caption = Date If lblWindow(1).BackColor = lonColor1 Then lblWindow(1).BackColor = lonColor2 lblWindow(2).BackColor = lonColor2 lblWindow(0).BackColor = lonColor1 lblWindow(3).BackColor = lonColor1 Else lblWindow(1).BackColor = lonColor1 lblWindow(2).BackColor = lonColor1 lblWindow(0).BackColor = lonColor2 lblWindow(3).BackColor = lonColor2 End If End Sub Private Sub HideForm() With Me If .Top = 0 Then .Top = LINEWIDTH - .Height blnFormHide = True ' blnTop = True End If If .Left = 0 Then .Left = LINEWIDTH - .Width blnFormHide = True ' blnLeft = True End If If .Left + .Width = Screen.Width Then .Left = Screen.Width - LINEWIDTH blnFormHide = True ' blnButton = True End If If .Top + .Height = Screen.Height Then .Top = Screen.Height - LINEWIDTH blnFormHide = True ' blnTop = True End If End With tmrGetMouseXY.Enabled = False End Sub 》》》》》》》》》》》》》》》》》》》》 补充说明,我原来是编一个在桌面上显示时间和日期的小程序, 其中用到了自动隐藏和自动出现的功能, 和你的要求有点类似,于是拿出来献丑,你自己还得改改。。 呵呵。
Public Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As LongPublic Type pointapi
x As Long
y As Long
End Type
在窗体中声明Option Explicit
Dim M As pointapi
Dim a As Long
Private Sub Form_Load()
Me.Height = Screen.Height
Me.Top = 0
Me.Left = 0 - Me.Width
End SubPrivate Sub Timer1_Timer()
a = GetCursorPos(M)
If M.x = 0 Then
Me.Left = 0
End If
If M.x > Me.ScaleWidth Then
Me.Left = 0 - Me.Width
End If
End Sub
运行就知道了
Attribute VB_Name = "mdlWindow"
Public 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
Option ExplicitSub Main()
Dim lonerrorcode As Long
frmWindow.Show
lonerrorcode = SetWindowPos(frmWindow.hwnd, -1, frmWindow.Left, _
frmWindow.Top, frmWindow.Left + frmWindow.Width, _
frmWindow.Top + frmWindow.Height, &H1)
DoEvents
End Sub
再建一个窗体,代码:
VERSION 5.00
Begin VB.Form frmWindow
BackColor = &H00800080&
BorderStyle = 0 'None
Caption = "浮动窗体示例"
ClientHeight = 1005
ClientLeft = 105
ClientTop = 105
ClientWidth = 3405
FillColor = &H00000080&
LinkTopic = "Form1"
ScaleHeight = 1005
ScaleWidth = 3405
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VB.Timer tmrGetMouseXY
Enabled = 0 'False
Interval = 1000
Left = 1680
Top = 120
End
Begin VB.Timer tmrWindow
Interval = 1000
Left = 360
Top = 240
End
Begin VB.Label lblWindow
BackColor = &H0080FF80&
Caption = "Label1"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 375
Index = 0
Left = 2040
TabIndex = 0
Top = 480
Width = 1215
End
Begin VB.Label lblWindow
BackColor = &H0080FF80&
Caption = "现在的时间是:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 375
Index = 3
Left = 120
TabIndex = 3
Top = 480
Width = 2175
End
Begin VB.Label lblWindow
BackColor = &H00404040&
Caption = "Label1"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 495
Index = 2
Left = 1200
TabIndex = 2
Top = 120
Width = 2055
End
Begin VB.Label lblWindow
BackColor = &H0080C0FF&
Caption = "今天是:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 495
Index = 1
Left = 120
TabIndex = 1
Top = 120
Width = 1215
End
End
Attribute VB_Name = "frmWindow"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As LongDim sinMouseUpXPosition As Single
Dim sinMouseMoveXPosition As Single
Dim sinMouseDownXPosition As Single
Dim sinMouseUpYPosition As Single
Dim sinMouseMoveYPosition As Single
Dim sinMouseDownYPosition As Single
Dim blnFormHide As Boolean
'表示隐藏窗体。
Dim blnMouseDown As Boolean
'表示鼠标按下!
Dim blnMouseRightButtonDown As Boolean
'表示鼠标右键按下!
Const LINEWIDTH = 50
'以下两个变量是为了表示lblWindow标签的颜色的!
Dim lonColor1 As Long
Dim lonColor2 As Long
'以下这四个变量,是为了调用API得到鼠标的位置的。
Dim sinMouseXPositionOld As Single
Dim sinMouseYPositionOld As Single
Dim sinMouseXPositionNew As Single
Dim sinMouseYPositionNew As SingleDim blnRight As Boolean
Dim blnLeft As Boolean
Dim blnTop As Boolean
Dim blnButton As Boolean
Private Sub Form_DblClick()
If blnMouseRightButtonDown Then
End If
End SubPrivate Sub Form_Load()
lblWindow(0).Caption = Time
lblWindow(2).Caption = Date
lonColor1 = lblWindow(1).BackColor
lonColor2 = lblWindow(3).BackColor
lblWindow(2).BackColor = lblWindow(1).BackColor
lblWindow(0).BackColor = lblWindow(3).BackColor
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
sinMouseDownXPosition = x
sinMouseDownYPosition = y
blnMouseDown = True
If Button = 2 Then
If MsgBox("确实要退出吗?", vbYesNo) = vbYes Then
End
End If
blnMouseRightButtonDown = True
blnMouseDown = False
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If blnFormHide Then
With Me
If .Top < 0 Then .Top = 0
If .Top > Screen.Height - LINEWIDTH - 10 Then
.Top = Screen.Height - .Height
End If
If .Left < 0 Then .Left = 0
If .Left > Screen.Width - LINEWIDTH - 10 Then
.Left = Screen.Width - .Width
End If
End With
blnFormHide = False
tmrGetMouseXY.Enabled = True
End If
sinMouseMoveXPosition = x
sinMouseMoveYPosition = y
If blnMouseDown Then
Me.Left = Me.Left + sinMouseMoveXPosition - sinMouseDownXPosition
Me.Top = Me.Top + sinMouseMoveYPosition - sinMouseDownYPosition
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
sinMouseUpXPosition = x
sinMouseUpYPosition = y
blnMouseDown = False
blnMouseRightButtonDown = False
blnFormHide = False
With Me
If .Left < 10 Then
.Left = LINEWIDTH - .Width
blnFormHide = True
' blnLeft = True
End If
If .Top < 10 Then
.Top = LINEWIDTH - .Height
blnFormHide = True
' blnTop = True
End If
If .Top + .Height > Screen.Height Then
.Top = Screen.Height - LINEWIDTH
blnFormHide = True
' blnTop = True
End If
If .Left + .Width > Screen.Width Then
.Left = Screen.Width - LINEWIDTH
blnFormHide = True
' blnButton = True
End If
End With
End SubPrivate Sub lblWindow_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
Call Form_MouseDown(Button, Shift, x, y)
End SubPrivate Sub lblWindow_MouseMove(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
Call Form_MouseMove(Button, Shift, x, y)
End SubPrivate Sub lblWindow_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
Call Form_MouseUp(Button, Shift, x, y)
End Sub'Private Sub Timer1_Timer()
'GetCursorPos mouseposition
'If mouseposition.x = 1 Then
'If mouseposition.y = 2 Then
'End If
'End If'End SubPrivate Sub tmrGetMouseXY_Timer()
Dim lonerrorcode As Long
Dim typMouseXY As POINTAPI
lonerrorcode = GetCursorPos(typMouseXY)
With Me
If typMouseXY.x > .Left + .Width Then
Call HideForm
End If
If typMouseXY.x < .Left Then
Call HideForm
End If
If typMouseXY.y > .Top + .Height Then
Call HideForm
End If
If typMouseXY.y < .Top Then
Call HideForm
End If
End With
End SubPrivate Sub tmrWindow_Timer()
lblWindow(0).Caption = Time
lblWindow(2).Caption = Date
If lblWindow(1).BackColor = lonColor1 Then
lblWindow(1).BackColor = lonColor2
lblWindow(2).BackColor = lonColor2
lblWindow(0).BackColor = lonColor1
lblWindow(3).BackColor = lonColor1
Else
lblWindow(1).BackColor = lonColor1
lblWindow(2).BackColor = lonColor1
lblWindow(0).BackColor = lonColor2
lblWindow(3).BackColor = lonColor2
End If
End Sub
Private Sub HideForm()
With Me
If .Top = 0 Then
.Top = LINEWIDTH - .Height
blnFormHide = True
' blnTop = True
End If
If .Left = 0 Then
.Left = LINEWIDTH - .Width
blnFormHide = True
' blnLeft = True
End If
If .Left + .Width = Screen.Width Then
.Left = Screen.Width - LINEWIDTH
blnFormHide = True
' blnButton = True
End If
If .Top + .Height = Screen.Height Then
.Top = Screen.Height - LINEWIDTH
blnFormHide = True
' blnTop = True
End If
End With
tmrGetMouseXY.Enabled = False
End Sub
》》》》》》》》》》》》》》》》》》》》
补充说明,我原来是编一个在桌面上显示时间和日期的小程序,
其中用到了自动隐藏和自动出现的功能,
和你的要求有点类似,于是拿出来献丑,你自己还得改改。。
呵呵。