我实现了一个AppBar来替换windows任务栏。 如果explorer.exe正在运行,AppBar将正常运行。
当explorer.exe未运行时,任何最大化窗口都将停靠在我的整个屏幕尺寸上。
无论explorer.exe是否未运行,AppBar都应正常运行。我在这里错过了什么?我附上了我正在使用的代码。
AppBar with explorer.exe running
AppBar without explorer.exe running
Public Class Form1
var params = req
.GetQueryNameValuePairs()
.Where(q => string.Compare(q.Key, "myParam", true) == 0)
.Select(q => q.Value);
结束班
Public Class NativeMethods
Public Enum AppBarStates
AutoHide = &H1
AlwaysOnTop = &H2
End Enum
Public Sub SetTaskbarState([option] As AppBarStates)
Dim msgData As New APPBARDATA()
msgData.cbSize = CType(Marshal.SizeOf(msgData), Integer)
msgData.hWnd = FindWindow("System_TrayWnd", Nothing)
msgData.lParam = DirectCast([option], Int32)
SHAppBarMessage(DirectCast(ABMsg.ABM_SETSTATE, Integer), msgData)
End Sub
Public Function GetTaskbarState() As AppBarStates
Dim msgData As New APPBARDATA()
msgData.cbSize = CType(Marshal.SizeOf(msgData), Integer)
msgData.hWnd = FindWindow("System_TrayWnd", Nothing)
Return CType(SHAppBarMessage(DirectCast(ABMsg.ABM_GETSTATE, Integer), msgData), AppBarStates)
End Function
Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
Enum ABMsg
ABM_NEW = 0
ABM_REMOVE = 1
ABM_QUERYPOS = 2
ABM_SETPOS = 3
ABM_GETSTATE = 4
ABM_GETTASKBARPOS = 5
ABM_ACTIVATE = 6
ABM_GETAUTOHIDEBAR = 7
ABM_SETAUTOHIDEBAR = 8
ABM_WINDOWPOSCHANGED = 9
ABM_SETSTATE = 10
End Enum
Enum ABNotify
ABN_STATECHANGE = 0
ABN_POSCHANGED
ABN_FULLSCREENAPP
ABN_WINDOWARRANGE
End Enum
Enum ABEdge
ABE_LEFT = 0
ABE_TOP
ABE_RIGHT
ABE_BOTTOM
End Enum
Private fBarRegistered As Boolean = False
Private uCallBack As Integer
Private Sub RegisterBar()
Dim abd As New APPBARDATA()
abd.cbSize = Marshal.SizeOf(abd)
abd.hWnd = Me.Handle
If Not fBarRegistered Then
uCallBack = RegisterWindowMessage("AppBarMessage")
abd.uCallbackMessage = uCallBack
Dim ret As Integer = SHAppBarMessage(CInt(ABMsg.ABM_NEW), abd) 'ToDo: Unsigned Integers not supported
fBarRegistered = True
ABSetPos()
Else
SHAppBarMessage(CInt(ABMsg.ABM_REMOVE), abd)
fBarRegistered = False
End If
End Sub
Private Sub ABSetPos()
Dim abd As New APPBARDATA()
abd.cbSize = Marshal.SizeOf(abd)
abd.hWnd = Me.Handle
abd.uEdge = CInt(ABEdge.ABE_BOTTOM)
If abd.uEdge = CInt(ABEdge.ABE_LEFT) Or abd.uEdge = CInt(ABEdge.ABE_RIGHT) Then
abd.rc.top = 0
abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
If abd.uEdge = CInt(ABEdge.ABE_LEFT) Then
abd.rc.left = 0
abd.rc.right = Size.Width
Else
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
abd.rc.left = abd.rc.right - Size.Width
End If
Else
abd.rc.left = 0
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
If abd.uEdge = CInt(ABEdge.ABE_TOP) Then
abd.rc.top = 0
abd.rc.bottom = Size.Height
Else
abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
abd.rc.top = abd.rc.bottom - Size.Height
End If
End If
' Query the system for an approved size and position.
SHAppBarMessage(CInt(ABMsg.ABM_QUERYPOS), abd)
' Adjust the rectangle, depending on the edge to which the
' appbar is anchored.
Select Case abd.uEdge
Case CInt(ABEdge.ABE_LEFT)
abd.rc.right = abd.rc.left + Size.Width
Case CInt(ABEdge.ABE_RIGHT)
abd.rc.left = abd.rc.right - Size.Width
Case CInt(ABEdge.ABE_TOP)
abd.rc.bottom = abd.rc.top + Size.Height
Case CInt(ABEdge.ABE_BOTTOM)
abd.rc.top = abd.rc.bottom - Size.Height
End Select
' Pass the final bounding rectangle to the system.
SHAppBarMessage(CInt(ABMsg.ABM_SETPOS), abd)
' Move and size the appbar so that it conforms to the
' bounding rectangle passed to the system.
MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, True)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = uCallBack Then
Select Case m.WParam.ToInt32()
Case CInt(ABNotify.ABN_POSCHANGED)
ABSetPos()
End Select
End If
MyBase.WndProc(m)
End Sub
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style And Not &HC00000 ' WS_CAPTION
cp.Style = cp.Style And Not &H800000 ' WS_BORDER
cp.ExStyle = &H80 Or &H8 ' WS_EX_TOOLWINDOW | WS_EX_TOPMOST
Return cp
End Get
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RegisterBar()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
RegisterBar()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If GetTaskbarState() = 1 Then
SetTaskbarState(AppBarStates.AlwaysOnTop)
Else
SetTaskbarState(AppBarStates.AutoHide)
End If
End Sub
结束班