Ejemplo de cómo utilizar la librería SharpShell para crear una shell-extensión, un menú contextual para nuestra aplicación: https://sharpshell.codeplex.com
La imagen de arriba no hace referencia al siguiente ejemplo, mi menú tiene la siguiente estructura:
La imagen de arriba no hace referencia al siguiente ejemplo, mi menú tiene la siguiente estructura:
Código [Seleccionar]
· Título
(Sub-menu)
· Run
· Open Files...
Código (vbnet) [Seleccionar]
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports SharpShell.Attributes
Imports SharpShell.SharpContextMenu
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Windows.Forms
Imports System.ComponentModel
#End Region
#Region " MyAppContextMenu "
''' <summary>
''' The Application Context Menu Extension.
''' </summary>
<ComVisible(True)>
<COMServerAssociation(AssociationType.ClassOfExtension, ".ext")>
Public Class MyAppContextMenu : Inherits SharpContextMenu
#Region " Objects "
''' <summary>
''' Contains the application information.
''' </summary>
Private ReadOnly application As New AppInfo With
{
.Title = "Menu Title",
.Filename = "Application Filename",
.Directory = My.Application.Info.DirectoryPath
}
#End Region
#Region " Types "
''' <summary>
''' Contains information about an application.
''' This class cannot be inherited.
''' </summary>
Protected NotInheritable Class AppInfo
''' <summary>
''' Gets or sets the application title.
''' </summary>
''' <value>The application title.</value>
Protected Friend Property Title As String
''' <summary>
''' Gets or sets the application filename.
''' </summary>
''' <value>The application filename.</value>
Protected Friend Property Filename As String
''' <summary>
''' Gets or sets the application working directory.
''' </summary>
''' <value>The application working directory.</value>
Protected Friend Property Directory As String
''' <summary>
''' Gets the full qualified application path.
''' </summary>
''' <value>The full qualified application path.</value>
Protected Friend ReadOnly Property FullPath As String
Get
Return Path.Combine(Me.Directory, Me.Filename, ".exe")
End Get
End Property
End Class
#End Region
#Region " SharpShell Methods "
''' <summary>
''' Determines whether this instance can a shell context show menu, given the specified selected file list.
''' </summary>
''' <returns>
''' <c>true</c> if this instance should show a shell context menu for the specified file list; otherwise, <c>false</c>.
''' </returns>
Protected Overrides Function CanShowMenu() As Boolean
Return True
End Function
''' <summary>
''' Creates the context menu.
''' </summary>
''' <returns>The context menu for the shell context menu.</returns>
Protected Overrides Function CreateMenu() As ContextMenuStrip
' Create the menu strip.
Dim menu As New ContextMenuStrip()
' Create the main item, this is used to show our application title.
Dim itemTitle As New ToolStripMenuItem() With
{
.Text = Me.application.Title,
.Image = My.Resources.TitleIcon
}
' Create a 'Run' item.
Dim itemRun As New ToolStripMenuItem() With
{
.Text = "Run",
.Image = My.Resources.RunIcon
}
' Create a 'Open file' item.
Dim itemOpenFile As New ToolStripMenuItem() With
{
.Text = "Open file...",
.Image = My.Resources.OpenFileIcon
}
' Create a 'Open files' item.
Dim itemOpenFiles As New ToolStripMenuItem() With
{
.Text = "Open files...",
.Image = My.Resources.OpenFileIcon
}
' Add the main item into the context menu.
menu.Items.Add(itemTitle)
' Add the 'Run' sub-item into the 'itemTitle' item.
itemTitle.DropDownItems.Add(itemRun)
' Add the 'Open file' or 'Open files' sub-item into the 'itemTitle' item.
' Depending on the amount of selected files.
itemTitle.DropDownItems.Add(If(Me.SelectedItemPaths.Count = 1, itemOpenFile, itemOpenFiles))
' Suscribe to events.
AddHandler itemRun.Click, AddressOf ItemRun_Click
AddHandler itemOpenFile.Click, AddressOf ItemOpenFile_Click
AddHandler itemOpenFiles.Click, AddressOf ItemOpenFiles_Click
' Return the menu.
Return menu
End Function
#End Region
#Region " Application Methods "
''' <summary>
''' Runs the specified application.
''' </summary>
''' <param name="fileName">The name of an application file to run in the process.</param>
''' <param name="arguments">Command-line arguments to pass when starting the process.</param>
Private Sub RunApp(ByVal fileName As String,
Optional ByVal arguments As String = "")
Try
Process.Start(fileName, arguments)
Catch ex As FileNotFoundException
' Do something.
Catch ex As InvalidOperationException
' Do something.
Catch ex As Win32Exception
' Dim errorCode As Integer = Marshal.GetLastWin32Error()
' Do something.
Catch ex As Exception
' Do something.
End Try
End Sub
''' <summary>
''' Opens the given file in the specified application.
''' </summary>
''' <param name="appPath">The application filepath to run.</param>
''' <param name="filepath">The filepath to send to the application arguments.</param>
''' <param name="stringFormat">The string format used to format the filepath.</param>
Private Sub OpenFile(ByVal appPath As String,
ByVal filepath As String,
Optional ByVal stringFormat As String = """{0}""")
Me.RunApp(appPath, String.Format(stringFormat, filepath))
End Sub
''' <summary>
''' Opens the given files in the specified application.
''' </summary>
''' <param name="appPath">The application filepath to run.</param>
''' <param name="filepaths">The filepaths to send to the application arguments.</param>
''' <param name="stringFormat">The string format used to join the filepaths.</param>
Private Sub OpenFiles(ByVal appPath As String,
ByVal filepaths As IEnumerable(Of String),
Optional ByVal stringFormat As String = """{0}"" ")
Me.RunApp(fileName:=appPath,
arguments:=Me.JoinFilePaths(filepaths, stringFormat))
End Sub
''' <summary>
''' Joins the selected filepaths in a single line, filepaths are closed with double-quotes and separated by a space.
''' eg: "File1" "File2" "File3"
''' </summary>
''' <param name="filepaths">The filepaths to join.</param>
''' <param name="joinFormat">The string format used to join the filepaths.</param>
''' <returns>The joined and formatted filepaths.</returns>
Private Function JoinFilePaths(ByVal filepaths As IEnumerable(Of String),
ByVal joinFormat As String) As String
Dim sb As New StringBuilder()
For Each filePath As String In filepaths
sb.Append(String.Format(joinFormat, filePath))
Next filePath
Return sb.ToString
End Function
#End Region
#Region " Event Handlers "
''' <summary>
''' Handles the Click event of the ItemRun menu item.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub ItemRun_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.RunApp(fileName:=Me.application.FullPath)
End Sub
''' <summary>
''' Handles the Click event of the ItemOpenFile menu item.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub ItemOpenFile_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.OpenFile(appPath:=Me.application.FullPath,
filepath:=Me.SelectedItemPaths.First)
End Sub
''' <summary>
''' Handles the Click event of the ItemOpenFiles menu item.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
Private Sub ItemOpenFiles_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.OpenFiles(appPath:=Me.application.FullPath,
filepaths:=Me.SelectedItemPaths)
End Sub
#End Region
End Class
#End Region