我用一个窗体作为消息弹出窗口 现在问题是 他总是遮盖住任务栏 怎么能让它弹到任务栏以上呢?
Option ExplicitPrivate 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 LongPrivate Const HWND_TOPMOST& = -1
' 将窗口置于列表顶部,并位于任何最顶部窗口的前面
Private Const SWP_NOSIZE& = &H1
' 保持窗口大小
Private Const SWP_NOMOVE& = &H2
' 保持窗口位置
Private Sub Form_Load()
'播放声音
Dim RetVal As Integer
Dim MCI As MCICmd
Set MCI = New MCICmd
MCI.FileName = App.Path & "\sound\sound.wav"
RetVal = MCI.PlaySound
'冒泡提示
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Dim UP As Boolean
Me.Left = Screen.Width - Me.Width
Me.Top = Screen.Height
StoTimer.Enabled = True
StoTimer.Interval = 5
UP = True
End Sub
Private Sub StoTimer_Timer()
If frmMes.Top > Screen.Height - frmMes.Height Then
frmMes.Top = frmMes.Top - 10
End If
End Sub
Option ExplicitPrivate 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 LongPrivate Const HWND_TOPMOST& = -1
' 将窗口置于列表顶部,并位于任何最顶部窗口的前面
Private Const SWP_NOSIZE& = &H1
' 保持窗口大小
Private Const SWP_NOMOVE& = &H2
' 保持窗口位置
Private Sub Form_Load()
'播放声音
Dim RetVal As Integer
Dim MCI As MCICmd
Set MCI = New MCICmd
MCI.FileName = App.Path & "\sound\sound.wav"
RetVal = MCI.PlaySound
'冒泡提示
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Dim UP As Boolean
Me.Left = Screen.Width - Me.Width
Me.Top = Screen.Height
StoTimer.Enabled = True
StoTimer.Interval = 5
UP = True
End Sub
Private Sub StoTimer_Timer()
If frmMes.Top > Screen.Height - frmMes.Height Then
frmMes.Top = frmMes.Top - 10
End If
End Sub
"他总是遮盖住任务栏 怎么能让它弹到任务栏以上呢"
我运行了你的代码,窗口也确实是显示在任务栏上面的,可能,你是在窗口显示之后又点了任务栏,从而把任务栏前置了吧.
另外,你的代码中StoTimer_Timer里的frmMes宜改成Me,这样编码较好
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim lHandle As Long
'First we're going to retrieve the handle of this window
' "ThunderRT5Form" is the classname of a VB-window
lHandle = FindWindow("ThunderRT5Form", Me.Caption)
'Set this window to the foreground
lHandle = SetForegroundWindow(lHandle)
End Sub根据hwnd取位置的API可用这个GetWindowRect ,示例代码太长了,我只给你网址了
http://allapi.mentalis.org/apilist/1A4BADF4A37061033CDBCF7982126427.html
'************************************* 获取任务栏高度使用的API与常量宣告
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Type RECT '定义一个区间的结构体
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Const SPI_GETWORKAREA = 48
'*****************************************
Dim TaskBarHeight&
Private Sub Form_Load()
TaskBarHeight = GetTaskbarHeight
Me.Move Screen.Width - Me.Width, Screen.Height - TaskBarHeight - Me.Height
End SubPublic Function GetTaskbarHeight() As Long '获取最下面那排任务栏占用的高度 调用涵数
'获取最下面任务栏的高度
On Error Resume Next
Dim lRes As Long
Dim RectVal As RECT
lRes = SystemParametersInfo(SPI_GETWORKAREA, 0, RectVal, 0)
GetTaskbarHeight = ((Screen.Height / Screen.TwipsPerPixelX) - RectVal.Bottom) * 15
End Function
GetTaskbarHeight = ((Screen.Height / Screen.TwipsPerPixelX) - RectVal.Bottom) * 15
比如改为
GetTaskbarHeight = ((Screen.Height / Screen.TwipsPerPixelY) - RectVal.Bottom) * Screen.TwipsPerPixelY
你肯定不能假设TwipsPerPixelY一定为小字体时的15对不.另外,楼主,我们给的代码都是只考虑任务栏停靠在默认的下方的,你如做商业软件可能需要多考虑几种情况;)
比如说右边:)