PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBWin7TaskbarJumpList/MainForm.vb

#
Visual Basic | 321 lines | 186 code | 56 blank | 79 comment | 0 complexity | 05184d42e2e97369af434eb8917bf533 MD5 | raw file
  1. '*************************************** Module Header ***************************************\
  2. ' Module Name: MainForm.cs
  3. ' Project: VBWin7TaskbarJumpList
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' The Jump List feature is designed to provide you with quick access to the documents and tasks
  7. ' associated with your applications. You can think of Jump Lists like little application-
  8. ' specific Start menus. Jump Lists can be found on the application icons that appear on the
  9. ' Taskbar when an application is running or on the Start menu in the recently opened programs
  10. ' section. Jump Lists can also be found on the icons of applications that have been
  11. ' specifically pinned to the Taskbar or the Start menu.
  12. '
  13. ' VBWin7TaskbarJumpList example demostrates how to set register Jump List file handle, add
  14. ' items into Recent/Frequent known categories, add/remove user tasks, and add items/links into
  15. ' custom categories in Windows 7 Taskbar Jump List using Taskbar related APIs in Windows API
  16. ' Code Pack.
  17. '
  18. ' This MainForm can register the .txt file as the application Jump List file handle, add .txt
  19. ' file into Recent/Frequent known categories, add/remove notepad.exe, mspaint.exe, calc.exe as
  20. ' user tasks, and add items/links into custom categories in Windows 7 Taskbar Jump List.
  21. '
  22. ' This source is subject to the Microsoft Public License.
  23. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  24. ' All other rights reserved.
  25. '
  26. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  27. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  28. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  29. '*********************************************************************************************/
  30. #Region "Imports directive"
  31. Imports System.Reflection
  32. Imports System.IO
  33. Imports Microsoft.WindowsAPICodePack.Taskbar
  34. Imports Microsoft.WindowsAPICodePack.Shell
  35. #End Region
  36. Public Class MainForm
  37. ' The Application ID for the current application. The AppID is necessary since we need to
  38. ' register the file extension based on the specific AppID
  39. Const AppID As String = "All-In-One Code Framework.Win7Taskbar.VBWin7TaskbarJumpList"
  40. ' Jump List custom category instance
  41. Private _currentCategory As JumpListCustomCategory
  42. Private _jumpList As JumpList ' Jump List instance
  43. ' Private readonly property of the Jump List instance
  44. Public ReadOnly Property JumpList() As JumpList
  45. Get
  46. ' Create a new Jump List instance if it is null
  47. If _jumpList Is Nothing Then
  48. _jumpList = JumpList.CreateJumpList() ' Create Jump List instance
  49. ' Set the known category based on the radio button checked value
  50. _jumpList.KnownCategoryToDisplay = _
  51. If(radRecent.Checked, JumpListKnownCategoryType.Recent, _
  52. JumpListKnownCategoryType.Frequent)
  53. _jumpList.Refresh() ' Refresh the Jump List
  54. End If
  55. Return _jumpList
  56. End Get
  57. End Property
  58. ' Check the Windows version, if it is Windows 7 or Windows Server 2008 R2, set the AppID
  59. ' and update the UI, otherwise exit the process
  60. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  61. ' Check whether the current system is Windows 7 or Windows Server 2008 R2
  62. If TaskbarManager.IsPlatformSupported Then
  63. ' Set the AppID (For detail, please see VBWin7TaskbarAppID example)
  64. TaskbarManager.Instance.ApplicationId = AppID
  65. ' Update the UI
  66. chkNotepad.Checked = True
  67. chkCalc.Checked = True
  68. chkPaint.Checked = True
  69. btnClearTask.Enabled = False
  70. btnAddItem.Enabled = False
  71. addLinkButton.Enabled = False
  72. tbItem.Enabled = False
  73. tbLink.Enabled = False
  74. Else
  75. MessageBox.Show("Jump List is not supported in your operation system!" & vbNewLine & _
  76. "Please launch the application in Windows 7 systems.")
  77. Application.Exit() ' Exit the current process
  78. End If
  79. End Sub
  80. ' Register the Jump List file handle for the application
  81. Private Sub btnRegisterFileType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegisterFileType.Click
  82. ' Check whether the application ID has been registered
  83. If HelperMethod.IsApplicationRegistered(TaskbarManager.Instance.ApplicationId) Then
  84. MessageBox.Show(".txt file type has been registered!")
  85. Return
  86. End If
  87. ' Check whether the application is runas Admin, since we need the Admin privilege
  88. ' to modify the HKCR registry values
  89. If Not HelperMethod.IsAdmin() Then
  90. ' Ask the user whether to elevate the application to Admin session
  91. Dim result = MessageBox.Show("This operation needs Admin privilege!" & vbNewLine & _
  92. "Restart and run the application as Admin?", _
  93. "Warning!", MessageBoxButtons.YesNo)
  94. If result = Windows.Forms.DialogResult.Yes Then
  95. Try
  96. ' Call helper method to restart the application and run the application as Admin
  97. HelperMethod.RestartApplicationAsAdmin()
  98. Catch
  99. Return
  100. End Try
  101. ' Kill the current application instance
  102. Application.Exit()
  103. Else
  104. Return
  105. End If
  106. End If
  107. ' If the application is runas Admin
  108. Try
  109. ' Call helper method to register the .txt file handle
  110. HelperMethod.RegisterFileAssociations(TaskbarManager.Instance.ApplicationId, False, _
  111. TaskbarManager.Instance.ApplicationId, Assembly.GetExecutingAssembly().Location, ".txt")
  112. MessageBox.Show(".txt file type is registered successfully!")
  113. Catch ex As Exception
  114. MessageBox.Show("Error registering file type association:" & vbNewLine & ex.Message)
  115. End Try
  116. End Sub
  117. ' Unregister the Jump List file handle
  118. Private Sub btnUnregisterFileType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnregisterFileType.Click
  119. ' Check whether the application ID has been registered
  120. If Not HelperMethod.IsApplicationRegistered(TaskbarManager.Instance.ApplicationId) Then
  121. MessageBox.Show(".txt file type has not been registered!")
  122. Return
  123. End If
  124. ' Check whether the application is runas Admin, since we need the Admin privilege
  125. ' to modify the HKCR registry values.
  126. If Not HelperMethod.IsAdmin() Then
  127. ' Ask the user whether to elevate the application to Admin session
  128. Dim result = MessageBox.Show("This operation needs Admin privilege!" & vbNewLine & _
  129. "Restart and run the application as Admin?", _
  130. "Warning!", MessageBoxButtons.YesNo)
  131. If result = Windows.Forms.DialogResult.OK Then
  132. Try
  133. ' Call helper method to restart the application and run the application as Admin
  134. HelperMethod.RestartApplicationAsAdmin()
  135. Catch
  136. Return
  137. End Try
  138. ' Kill the current application instance
  139. Application.Exit()
  140. Else
  141. Return
  142. End If
  143. End If
  144. ' If the application is runas Admin
  145. Try
  146. ' Call helper method to unregister the .txt file handle
  147. HelperMethod.UnregisterFileAssociations( _
  148. TaskbarManager.Instance.ApplicationId, False, _
  149. TaskbarManager.Instance.ApplicationId, _
  150. Assembly.GetExecutingAssembly().Location, ".txt")
  151. MessageBox.Show(".txt file type is unregistered successfully!")
  152. Catch ex As Exception
  153. MessageBox.Show("Error unregistering file type association:" & vbNewLine & ex.Message)
  154. End Try
  155. End Sub
  156. ' Update the known category (Recent/Frequent) based on the Radio button status
  157. Private Sub radFrequentRecent_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFrequent.CheckedChanged, radRecent.CheckedChanged
  158. If radFrequent.Checked Then
  159. JumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Frequent
  160. Else
  161. JumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent
  162. End If
  163. JumpList.Refresh()
  164. End Sub
  165. ' Open an OpenFileDialog to make the .txt file show in the known category (Recent/Frequent)
  166. Private Sub btnChooseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseFile.Click
  167. If recentFileOpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
  168. radRecent.Checked = True
  169. End If
  170. End Sub
  171. ' Add user tasks based on the three CheckBox (notepad, paint, calculator)
  172. Private Sub btnAddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTask.Click
  173. Try
  174. ' Retrieve the system folder
  175. Dim systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System)
  176. If (chkNotepad.Checked) Then
  177. ' Add the notepad.exe user task and set the icon
  178. Dim notepadTask As IJumpListTask = New JumpListLink( _
  179. Path.Combine(systemFolder, "notepad.exe"), "Open Notepad") With _
  180. {.IconReference = New IconReference(Path.Combine(systemFolder, "notepad.exe"), 0)}
  181. JumpList.AddUserTasks(notepadTask) ' Add the notepad user task into the Jump List
  182. End If
  183. If chkCalc.Checked Then
  184. ' Add the calc.exe user task and set the icon
  185. Dim calcTask As IJumpListTask = New JumpListLink( _
  186. Path.Combine(systemFolder, "calc.exe"), "Open Calculator") With _
  187. {.IconReference = New IconReference(Path.Combine(systemFolder, "calc.exe"), 0)}
  188. JumpList.AddUserTasks(calcTask) ' Add the notepad user task into the Jump List
  189. End If
  190. If chkPaint.Checked Then
  191. ' Add the mspaint.exe user task and a Jump List separator
  192. Dim paintTask As IJumpListTask = New JumpListLink( _
  193. Path.Combine(systemFolder, "mspaint.exe"), "Open Paint") With _
  194. {.IconReference = New IconReference(Path.Combine(systemFolder, "mspaint.exe"), 0)}
  195. If chkNotepad.Checked Or chkCalc.Checked Then
  196. ' Add a Jump List separator and the paint user task
  197. JumpList.AddUserTasks(New JumpListSeparator(), paintTask)
  198. Else
  199. JumpList.AddUserTasks(paintTask) ' Only add the paint user task
  200. End If
  201. End If
  202. ' Refresh the Jump List instance and update the UI
  203. JumpList.Refresh()
  204. btnClearTask.Enabled = True
  205. btnAddTask.Enabled = False
  206. Catch ex As Exception
  207. MessageBox.Show(ex.Message)
  208. End Try
  209. End Sub
  210. ' Clear all the user tasks
  211. Private Sub btnClearTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTask.Click
  212. Try
  213. JumpList.ClearAllUserTasks() ' Clear all the user tasks
  214. ' Refresh the Jump List instance and update the UI
  215. JumpList.Refresh()
  216. btnAddTask.Enabled = True
  217. btnClearTask.Enabled = False
  218. Catch ex As Exception
  219. MessageBox.Show(ex.Message)
  220. End Try
  221. End Sub
  222. ' Create a custom category
  223. Private Sub btnCreateCategory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateCategory.Click
  224. Try
  225. ' Create a new custom category based on the category name in the categoryTextBox
  226. _currentCategory = New JumpListCustomCategory(tbCategory.Text)
  227. ' Add the custom category instance into the Jump List
  228. JumpList.AddCustomCategories(_currentCategory)
  229. ' Refresh the Jump List instance and update the UI
  230. JumpList.Refresh()
  231. btnAddItem.Enabled = True
  232. addLinkButton.Enabled = True
  233. tbItem.Enabled = True
  234. tbLink.Enabled = True
  235. Catch ex As Exception
  236. MessageBox.Show(ex.Message)
  237. End Try
  238. End Sub
  239. ' Add a custom shell item to the custom category
  240. Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
  241. Try
  242. ' Check the if the file name is valid
  243. If Not HelperMethod.CheckFileName(tbItem.Text) Then Return
  244. ' Create a .txt file in the temp foler and create a shell item for this file
  245. Dim jli As JumpListItem = New JumpListItem(HelperMethod.GetTempFileName(tbItem.Text))
  246. _currentCategory.AddJumpListItems(jli) ' Add the shell item to the custom category
  247. JumpList.Refresh() ' Refresh the Jump List
  248. Catch ex As Exception
  249. MessageBox.Show(ex.Message)
  250. End Try
  251. End Sub
  252. ' Add a custom shell item to the custom category
  253. Private Sub btnAddLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addLinkButton.Click
  254. Try
  255. ' Check the if the file name is valid
  256. If Not HelperMethod.CheckFileName(tbLink.Text) Then Return
  257. ' Create a .txt file in the temp foler and create a shell link for this file
  258. Dim jli As JumpListLink = New JumpListLink(HelperMethod.GetTempFileName(tbLink.Text), tbLink.Text)
  259. _currentCategory.AddJumpListItems(jli) ' Add the shell link to the custom category
  260. JumpList.Refresh() ' Refresh the Jump List
  261. Catch ex As Exception
  262. MessageBox.Show(ex.Message)
  263. End Try
  264. End Sub
  265. End Class