The VB version of the Flip Task Bar for Vista Desktop Window Manager


In this post: Create your own Flip Task Bar with live thumbnails using Vista Desktop Window Manager DWM, I showed Fox code on creating your own Flip Task bar


 


Here is the VB.Net version that does the same thing…


File->New->Project->Visual Basic->Windows->Windows Forms Application


View->Code


 


Paste and run the code below.


 


 


See also Programming the Windows Vista DWM in C#


 


 


Imports System


Imports System.Text


Imports System.Runtime.InteropServices


Public Class Form1


      Dim ThumbWidth = 300


      Dim ThumbHeight = ThumbWidth * My.Computer.Screen.Bounds.Height / My.Computer.Screen.Bounds.Width


      Dim WithEvents cmdQuit As New Button


      Dim WithEvents cmdRefresh As New Button


      Dim WithEvents oSlider As New TrackBar


      Dim WithEvents oTimer As New Timer


 


      Delegate Function EnumWindowsCallback(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean


      Declare Ansi Function EnumChildWindows Lib “user32.dll” (ByVal hwnd As IntPtr, ByVal MyCallBack As EnumWindowsCallback, ByVal lParam As Integer) As Boolean


 


      Declare Ansi Function GetWindowTextA Lib “user32.dll” (ByVal hwnd As IntPtr, ByVal Str As StringBuilder, ByVal lSize As Integer) As Integer


      Declare Function GetWindowLongA Lib “user32.dll” (ByVal hwnd As IntPtr, ByVal num As Integer) As Integer


 


      Declare Function DwmRegisterThumbnail Lib “dwmapi.dll” (ByVal hWndDest As IntPtr, ByVal hWndSrc As IntPtr, ByRef dwThumb As IntPtr) As Integer


      Declare Function DwmUpdateThumbnailProperties Lib “dwmapi.dll” (ByVal hThumb As IntPtr, ByRef dwmp As DWM_PROPERTIES) As Integer


      Declare Function DwmUnregisterThumbnail Lib “dwmapi.dll” (ByVal hThumb As IntPtr) As Integer


 


      <StructLayout(LayoutKind.Sequential)> _


      Structure POINT


            Dim x As Integer


            Dim y As Integer


      End Structure


      <StructLayout(LayoutKind.Sequential)> _


      Structure Rect


            Sub New(ByVal _left As Integer, ByVal _top As Integer, ByVal _right As Integer, ByVal _bottom As Integer)


                  Left = _left


                  Right = _right


                  Top = _top


                  Bottom = _bottom


            End Sub


            Dim Left As Integer


            Dim Top As Integer


            Dim Right As Integer


            Dim Bottom As Integer


      End Structure


 


      <StructLayout(LayoutKind.Sequential)> _


      Structure DWM_PROPERTIES


            Dim dwFlags As Integer


            Dim rcDest As Rect


            Dim rcSrc As Rect


            Dim opacity As Byte


            Dim fVisible As Boolean


            Dim fSourceClientAreaOnly As Boolean


      End Structure


 


      Const DWM_TNP_VISIBLE = 8


      Const DWM_TNP_RECTDESTINATION = 1


      Const DWM_TNP_OPACITY = 4


 


      Const GWL_STYLE = -16


      Const WS_VISIBLE = &H10000000


      Const WS_BORDER = &H800000


      Class MyWindowItem


            Implements IDisposable


 


            Public hWnd As Integer


            Public Title As String


            Public Tid As IntPtr


            Public oPictureBox As MyPictureBox


            Public oLabel As Label


            Sub New(ByVal _hWnd As Integer, ByVal _title As String)


                  hWnd = _hWnd


                  Title = _title


            End Sub


 


            Public Sub Dispose1() Implements System.IDisposable.Dispose


                  If Tid <> 0 Then


                        DwmUnregisterThumbnail(Tid)


                        Tid = 0


                  End If


                  If oPictureBox IsNot Nothing Then


                        Form1.Controls.Remove(oPictureBox)


                  End If


                  If oLabel IsNot Nothing Then


                        Form1.Controls.Remove(oLabel)


                  End If


            End Sub


            Protected Overrides Sub finalize()


                  Dispose1()


                  MyBase.Finalize()


            End Sub


      End Class


      Dim WindowList As New SortedList


      Dim nNewWindowsFound As Integer


      Dim nTotalWindowsFound As Integer


 


      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


            Me.Width = My.Computer.Screen.Bounds.Width


            Me.Height = 180


            Me.Left = 0


            Me.Top = 0


            Me.MinimizeBox = False ‘ no minimize box, so it stays up if user minimizes all


            With cmdQuit


                  .Text = “&Quit”


            End With


            With cmdRefresh


                  .Text = “&Refresh”


                  .Left = 100


            End With


            With oSlider


                  .Left = 200


                  .Minimum = 1


                  .Maximum = 100


                  .Value = 50


                  .LargeChange = 10


                  .SmallChange = 2


                  .Height = 400


            End With


            Me.Controls.Add(cmdQuit)


            Me.Controls.Add(cmdRefresh)


            Me.Controls.Add(oSlider)


            oTimer.Interval = 2000


            oTimer.Enabled = True


      End Sub


 


      Sub GetHWnds(Optional ByVal fForce As Boolean = True)


            If Not fForce Then


                  nNewWindowsFound = 0


                  nTotalWindowsFound = 0


                  Dim nOrigWindowCount = WindowList.Count


                  EnumChildWindows(0, AddressOf MyCallBack, 0)


                  If 0 = nNewWindowsFound And nOrigWindowCount = nTotalWindowsFound Then  ‘ no new windows found or none deleted


                        Return


                  End If


            End If


 


            Dim indx As Integer


            Dim cWind As MyWindowItem


            For indx = 0 To WindowList.Count – 1


                  cWind = WindowList.GetByIndex(indx)


                  cWind.Dispose1()


            Next


            WindowList.Clear()


            nNewWindowsFound = 0


            EnumChildWindows(0, AddressOf MyCallBack, 0)


            Dim tid As Integer = 0


            Dim i = 0


            Dim x = 0


            Dim y = 20


            Dim nRatio = Me.oSlider.Value / Me.oSlider.Maximum


            For indx = 0 To WindowList.Count – 1


                  cWind = WindowList.GetByIndex(indx)


                  Dim oMyPictureBox As New MyPictureBox


                  Me.Controls.Add(oMyPictureBox)


                  With oMyPictureBox


                        .Left = x


                        .Top = y


                        .Height = ThumbHeight * nRatio


                        .Width = ThumbWidth * nRatio


                        .GetThumb(cWind)


                        .Visible = True


                  End With


                  cWind.oPictureBox = oMyPictureBox


                  Dim oLbl As New Label


                  With oLbl


                        .Left = x


                        .Top = y + ThumbHeight * nRatio


                        .Width = ThumbWidth * nRatio


                        .Height = 20


                        .Text = cWind.Title


                        .Visible = True


                  End With


                  cWind.oLabel = oLbl


                  Me.Controls.Add(oLbl)


                  x += Me.ThumbWidth * nRatio


                  If x + Me.ThumbWidth * nRatio > My.Computer.Screen.Bounds.Width Then


                        x = 0


                        y += Me.ThumbHeight * nRatio + 20


                  End If


            Next


      End Sub


 


      Function MyCallBack(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean


            If Me.Handle <> hWnd Then     ” if it’s not our form


                  Dim nStyle = GetWindowLongA(hWnd, GWL_STYLE)


                  If (nStyle And (WS_VISIBLE + WS_BORDER)) = (WS_VISIBLE + WS_BORDER) Then


                        Dim Caption As New StringBuilder(“”, 500)


                        GetWindowTextA(hWnd, Caption, Caption.Capacity)


                        Dim c As String = Caption.ToString


                        nTotalWindowsFound += 1


                        If Not WindowList.ContainsKey(hWnd.ToString) Then


                              WindowList.Add(hWnd.ToString, New MyWindowItem(hWnd, Caption.ToString))


                              nNewWindowsFound += 1


                        End If


                  End If


            End If


            Return True


      End Function


 


      Private Sub cmdQuit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdQuit.Click


            Me.Close()


      End Sub


 


      Private Sub cmdRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click


            GetHWnds()


      End Sub


 


      Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown


            GetHWnds()


      End Sub


 


      Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged


      End Sub


 


      Private Sub oSlider_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles oSlider.ValueChanged


            GetHWnds()


      End Sub


 


      Private Sub oTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles oTimer.Tick


            GetHWnds(False)


      End Sub


      Class MyPictureBox


            Inherits PictureBox


            Const WS_MINIMIZE = &H20000000


            Const SW_RESTORE = &H9


            Dim oMyWindow As MyWindowItem


            Sub GetThumb(ByVal _oMyWindow As MyWindowItem)


                  oMyWindow = _oMyWindow


                  DwmRegisterThumbnail(Me.Parent.Handle, oMyWindow.hWnd, oMyWindow.Tid)


                  Dim dwmp As New DWM_PROPERTIES


                  dwmp.opacity = 255


                  dwmp.fVisible = True


                  dwmp.rcDest = New Rect(Left, Top, Right, Bottom)


                          dwmp.rcSrc = New Rect(0, 0, 200, 200)


                  dwmp.dwFlags = DWM_TNP_VISIBLE + DWM_TNP_RECTDESTINATION + DWM_TNP_OPACITY


                  DwmUpdateThumbnailProperties(oMyWindow.Tid, dwmp)


            End Sub


            <StructLayout(LayoutKind.Sequential)> _


            Structure WindowPlacement


                  Dim length As Integer


                  Dim flags As Integer


                  Dim showcmd As Integer


                  Dim ptMinPosition As POINT


                  Dim ptMaxPosition As POINT


                  Dim rcNormalPosition As Rect


            End Structure


                    Declare Ansi Function GetWindowPlacement Lib “user32.dll” (ByVal hwnd As IntPtr, ByRef wp As WindowPlacement) As Integer


            <DllImport(“user32.dll”, SetLastError:=True)> Shared Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef wp As WindowPlacement) As Integer


            End Function


 


            Declare Ansi Function SetWindowPlacement Lib “user32.dll” (ByVal hwnd As IntPtr, ByRef wp As WindowPlacement) As Boolean


            Declare Function SetForegroundWindow Lib “user32.dll” (ByVal hWnd As IntPtr) As Integer


 


            Sub MyClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click


                  Dim nStyle = GetWindowLongA(oMyWindow.hWnd, GWL_STYLE)


                  If (nStyle And WS_MINIMIZE) = (WS_MINIMIZE) Then


                        Dim pPlacement As New WindowPlacement


                        pPlacement.length = 11 * 4


                        If GetWindowPlacement(oMyWindow.hWnd, pPlacement) > 0 Then


                              pPlacement.showcmd = SW_RESTORE


                              SetWindowPlacement(oMyWindow.hWnd, pPlacement)


                        Else


                              Dim n = Marshal.GetLastWin32Error()


                              Console.WriteLine(n)


                        End If


                  End If


                  SetForegroundWindow(oMyWindow.hWnd)


            End Sub


      End Class


End Class


 


 


 


End of code


 

Comments (4)

  1. Supporting the “pajama programmer” Telecommuting is a great thing, and as the environmentally-conscious

  2. " パジャマプログラマ" をサポートする 在宅勤務はすばらしいことです。環境にやさしい人でありたい私としては、現実的に実行できる場合は心から支持したいと考えます。私の現在の仕事は、人と対面するミーティングが多く、実際は在宅勤務する機会があまりありません。しかし、投稿するブログを練るときなどに夕方家で働くので、家から仕事ができる環境をセットアップすることは重要です。

  3. garry-rl says:

    <a href= http://index2.vip-salo.ru >��������� qip infium jabber</a> <a href= http://index1.vip-salo.ru >������������ ������������ �����</a> <a href= http://index5.vip-salo.ru >���������� ���� ������ ���������</a> <a href= http://index4.vip-salo.ru >www ����������� jimm ru</a> <a href= http://index3.vip-salo.ru >������� ����� 6.0</a>

  4. vb.net 2005 says:

    System.DllNotFoundException was unhandled

     Message="Unable to load DLL ‘dwmapi.dll’: The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)"

     Source="EXAM screen"

     TypeName=""

     StackTrace:

          at EXAM_screen.Form1.DwmRegisterThumbnail(IntPtr hWndDest, IntPtr hWndSrc, IntPtr& dwThumb)

          at EXAM_screen.Form1.MyPictureBox.GetThumb(MyWindowItem _oMyWindow) in C:Documents and SettingsvoidmanDesktopvb.net sourcemyEXAM screenEXAM screenForm1.vb:line 445

          at EXAM_screen.Form1.GetHWnds(Boolean fForce) in C:Documents and SettingsvoidmanDesktopvb.net sourcemyEXAM screenEXAM screenForm1.vb:line 307

          at EXAM_screen.Form1.oSlider_ValueChanged(Object sender, EventArgs e) in C:Documents and SettingsvoidmanDesktopvb.net sourcemyEXAM screenEXAM screenForm1.vb:line 419

          at System.Windows.Forms.TrackBar.OnValueChanged(EventArgs e)

          at System.Windows.Forms.TrackBar.set_Value(Int32 value)

          at EXAM_screen.Form1.Form1_Load(Object sender, EventArgs e) in C:Documents and SettingsvoidmanDesktopvb.net sourcemyEXAM screenEXAM screenForm1.vb:line 215

          at System.EventHandler.Invoke(Object sender, EventArgs e)

          at System.Windows.Forms.Form.OnLoad(EventArgs e)

          at System.Windows.Forms.Form.OnCreateControl()

          at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

          at System.Windows.Forms.Control.CreateControl()

          at System.Windows.Forms.Control.WmShowWindow(Message& m)

          at System.Windows.Forms.Control.WndProc(Message& m)

          at System.Windows.Forms.ScrollableControl.WndProc(Message& m)

          at System.Windows.Forms.ContainerControl.WndProc(Message& m)

          at System.Windows.Forms.Form.WmShowWindow(Message& m)

          at System.Windows.Forms.Form.WndProc(Message& m)

          at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

          at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

          at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

          at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)

          at System.Windows.Forms.Control.SetVisibleCore(Boolean value)

          at System.Windows.Forms.Form.SetVisibleCore(Boolean value)

          at System.Windows.Forms.Control.set_Visible(Boolean value)

          at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

          at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

          at System.Windows.Forms.Application.Run(ApplicationContext context)

          at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

          at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

          at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

          at EXAM_screen.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

          at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

          at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

          at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

          at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

          at System.Threading.ThreadHelper.ThreadStart()