我可以在对话框而不是主窗体上设置任务栏进度状态吗?

时间:2016-05-26 15:42:44

标签: vb.net taskbar

在我的VB.Net应用程序中,我有以下代码,允许我使用应用程序的任务栏图标作为进度条。

Public Sub setTaskbarProgress(ByVal upTo As Integer, ByVal outOfTotal As Integer, ByVal stateType As TaskbarProgressBarState)
If TaskbarManager.IsPlatformSupported Then
        If stateType = Nothing Then stateType = TaskbarProgressBarState.Normal
        Dim tbm As TaskbarManager = TaskbarManager.Instance
        tbm.SetProgressValue(upTo, outOfTotal)
        If upTo = 0 And outOfTotal = 100 Then
            tbm.SetProgressState(TaskbarProgressBarState.NoProgress)
        Else
            tbm.SetProgressState(stateType)
        End If
    End If
End Sub

我想扩展该功能,理想情况下它有第四个参数,例如

Public Sub setTaskbarProgress(ByVal upTo As Integer,
ByVal outOfTotal As Integer, ByVal stateType As TaskbarProgressBarState, _
ByRef target As Form)

以便将进度条(Taskbar.SetProgressValue和TaskBar.SetProgressState)应用于项目中的特定子表单或对话框,而不是始终应用​​于主表单。

有没有办法可以指定“TaskbarManager”实际应用于哪个窗口?我看不出我怎么会把它作为'目标'? Windows是否允许这样做?欢迎任何一点。

1 个答案:

答案 0 :(得分:0)

嗯,无论如何,是的,你可以做到,但你提供的代码缺乏必要的详细信息,包括知道其行为的人和方式,因为如果你已经完整,那就不一样了访问源代码以执行任何所需的修改,不清楚它是否是官方WindowsAPICodePack C#代码的VB.NET翻译,或者是其他人的自定义包装。

无论如何,你可以拥有的代码,特别是TaskbarManager.Instance成员应该实现 ITaskbarList 接口之一,可能 ITaskbarList4 (全部),这意味着您应该能够重新设计包装器以调用 ITaskbarList::SetProgressValue 方法来传递对话框的目标窗口句柄。

如果出于某种原因你可能需要ITaskbarList定义,我将它翻译成VB.NET(忽略了一些名为"伪造"我不想要的方法使用)

    ''' <remarks>
    ''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd562040(v=vs.85).aspx"/>
    ''' </remarks>
    <ComImport>
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    <Guid("C43DC798-95D1-4BEA-9030-BB99E2983A1A")>
    Public Interface ITaskbarList4

#Region " ITaskbarList "

        <PreserveSig>
        Sub HrInit()

        <PreserveSig>
        Sub AddTab(ByVal hwnd As IntPtr)

        <PreserveSig>
        Sub DeleteTab(ByVal hwnd As IntPtr)

        <PreserveSig>
        Sub ActivateTab(ByVal hwnd As IntPtr)

        <PreserveSig>
        Sub SetActiveAlt(ByVal hwnd As IntPtr)

#End Region

#Region " ITaskbarList2 "

        <PreserveSig>
        Sub MarkFullscreenWindow(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.Bool)> ByVal fFullscreen As Boolean)

#End Region

#Region " ITaskbarList3 "

        <PreserveSig>
        Sub SetProgressValue(ByVal hwnd As IntPtr, ByVal ullCompleted As UInt64, ByVal ullTotal As UInt64)

        <PreserveSig>
        Sub SetProgressState(ByVal hwnd As IntPtr, ByVal tbpFlags As TaskbarProgressBarState)

        <PreserveSig>
        Sub RegisterTab(ByVal hwndTab As IntPtr, ByVal hwndMdi As IntPtr)

        <PreserveSig>
        Sub UnregisterTab(ByVal hwndTab As IntPtr)

        <PreserveSig>
        Sub SetTabOrder(ByVal hwndTab As IntPtr, ByVal hwndInsertBefore As IntPtr)

        <PreserveSig>
        Sub SetTabActive(ByVal hwndTab As IntPtr, ByVal hwndInsertBefore As IntPtr, ByVal dwReserved As UInteger)

        <PreserveSig>
        Function Fake1() As HResult
        ' Function ThumbBarAddButtons(byval hwnd As IntPtr, byval cButtons As UInteger, <MarshalAs(UnmanagedType.LPArray)> byval pButtons As ThumbButton()) As HResult

        <PreserveSig>
        Function Fake2() As HResult
        ' Function ThumbBarUpdateButtons(byval hwnd As IntPtr, byval cButtons As UInteger, <MarshalAs(UnmanagedType.LPArray)> byval pButtons As ThumbButton()) As HResult

        <PreserveSig>
        Sub Fake3()
        ' Sub ThumbBarSetImageList(byval hwnd As IntPtr, byval himl As IntPtr)

        <PreserveSig>
        Sub SetOverlayIcon(ByVal hwnd As IntPtr, ByVal hIcon As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszDescription As String)

        <PreserveSig>
        Sub SetThumbnailTooltip(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszTip As String)

        <PreserveSig>
        Sub SetThumbnailClip(ByVal hwnd As IntPtr, ByVal prcClip As IntPtr)

#End Region

#Region " ITaskbarList4 "

        Sub Fake4()
        ' Sub SetTabProperties(byval hwndTab As IntPtr, byval stpFlags As SetTabPropertiesOption)

#End Region

    End Interface

另外这个枚举:

''' <remarks>
''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd391697%28v=vs.85%29.aspx"/>
''' </remarks>
Public Enum TaskbarProgressBarState
    NoProgress = 0
    Indeterminate = &H1
    Normal = &H2
    [Error] = &H4
    Paused = &H8
End Enum