මෙන්න කේතය
Imports System.Diagnostics
Imports System.Drawing
Imports System.Windows.Forms
'Imports Microsoft.VisualBasic.FileIO
Module Module1
Function GetShortcutIcon(ByVal shortcutPath As String) As Icon
Try
' Get the target path of the shortcut
Dim targetPath As String = GetShortcutTarget(shortcutPath)
' Extract the icon associated with the target file
If targetPath IsNot Nothing AndAlso IO.File.Exists(targetPath) Then
Return Icon.ExtractAssociatedIcon(targetPath)
Else
Return Nothing
End If
Catch ex As Exception
' Handle exceptions here
Return Nothing
End Try
End Function
Function GetShortcutTarget(ByVal shortcutPath As String) As String
Try
Dim shortcut As Shell32.Shell = New Shell32.Shell()
Dim folder As Shell32.Folder = shortcut.NameSpace(System.IO.Path.GetDirectoryName(shortcutPath))
Dim folderItem As Shell32.FolderItem = folder.ParseName(System.IO.Path.GetFileName(shortcutPath))
If folderItem IsNot Nothing Then
Dim link As Shell32.ShellLinkObject = CType(folderItem.GetLink, Shell32.ShellLinkObject)
Return link.Path
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
'' Structure for the WIN32_FIND_DATA structure returned by FindFirstFile and others
'<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
'Structure WIN32_FIND_DATA
' Public dwFileAttributes As Integer
' Public ftCreationTime As Long
' Public ftLastAccessTime As Long
' Public ftLastWriteTime As Long
' Public nFileSizeHigh As Integer
' Public nFileSizeLow As Integer
' Public dwReserved0 As Integer
' Public dwReserved1 As Integer
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public cFileName As String
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> Public cAlternateFileName As String
'End Structure
'' Function declarations
'Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
'Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As IntPtr) As Boolean
'Declare Unicode Function PathCombine Lib "shlwapi.dll" Alias "PathCombineW" (ByVal pszDest As System.Text.StringBuilder, ByVal pszDir As String, ByVal pszFile As String) As IntPtr
'' Function to retrieve target path of a shortcut
'Function GetShortcutTarget(ByVal shortcutPath As String) As String
' Dim WIN32_FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
' Dim FILE_ATTRIBUTE_REPARSE_POINT As Integer = &H400
' Try
' Dim findData As New WIN32_FIND_DATA()
' Dim handle As IntPtr = FindFirstFile(shortcutPath, findData)
' If handle.ToInt64() <> -1 Then
' If (findData.dwFileAttributes And WIN32_FILE_ATTRIBUTE_DIRECTORY) <> 0 AndAlso (findData.dwFileAttributes And FILE_ATTRIBUTE_REPARSE_POINT) <> 0 Then
' Dim targetPathBuilder As New System.Text.StringBuilder(260)
' PathCombine(targetPathBuilder, shortcutPath, findData.cFileName)
' Return targetPathBuilder.ToString()
' End If
' End If
' FindClose(handle)
' Catch ex As Exception
' ' Handle exceptions here
' Return Nothing
' End Try
' Return Nothing
'End Function
Class ToolStripMenuItemEX
Inherits ToolStripMenuItem
Friend ID As Integer
End Class
Dim ls() As String
Function make_ITEM(ByRef s As String, i As Integer) As ToolStripMenuItemEX
Dim targetPath As String = GetShortcutTarget(ls(i))
Dim cx As New ToolStripMenuItemEX()
cx.Text = s
cx.ID = i
If IO.File.Exists(targetPath) = True Then
cx.Image = Icon.ExtractAssociatedIcon(targetPath).ToBitmap
ls(i) = targetPath
Else
' MsgBox("|" + targetPath + "|")
If targetPath.Contains("\Program Files (x86)\") Then
targetPath = targetPath.Replace("\Program Files (x86)\", "\Program Files\")
If IO.File.Exists(targetPath) Then
cx.Image = Icon.ExtractAssociatedIcon(targetPath).ToBitmap
ls(i) = targetPath
Else
cx.Enabled = False
End If
Else
cx.Enabled = False
End If
End If
AddHandler cx.Click, AddressOf OnClickEX
Return cx
End Function
Sub Main()
' Create a new ContextMenuStrip
Dim contextMenu As New ContextMenuStrip()
contextMenu.Items.Add("වසන්න X ", Nothing, AddressOf Close_OnClick)
If IO.Directory.Exists(Application.StartupPath + "\කෙටි මං") Then
ls = IO.Directory.GetFiles(Application.StartupPath + "\කෙටි මං", "*.lnk", IO.SearchOption.TopDirectoryOnly)
Else
ls = IO.Directory.GetFiles(Application.StartupPath, "*.lnk", IO.SearchOption.TopDirectoryOnly)
End If
' Array.Sort(ls)
Dim i As Integer
Dim Fc As Char = Chr(0)
Dim fcc As Char = Chr(0)
Dim s As String
Dim cI As ToolStripMenuItem
Dim cX As ToolStripMenuItemEX
For i = 0 To ls.Length - 1
s = IO.Path.GetFileNameWithoutExtension(ls(i))
fcc = Char.ToLower(s(0))
If Fc = fcc Then
cX = make_ITEM(s, i)
cI.DropDownItems.Add(cX)
Else
If i = ls.Length - 1 Then
cX = make_ITEM(s, i)
contextMenu.Items.Add(cX)
' Fc = s(0)
ElseIf Char.ToLower(ls(i + 1)(0)) = Fc Then
' MsgBox(Fc + vbCrLf + s)
cX = make_ITEM(s, i)
cI.DropDownItems.Add(cX)
' Fc = s(0)
Else
cI = New ToolStripMenuItem(Char.ToUpper(s(0)))
contextMenu.Items.Add(cI)
cX = make_ITEM(s, i)
cI.DropDownItems.Add(cX)
' Fc = s(0)
End If
End If
Fc = fcc
' contextMenu.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
Next
contextMenu.Items.Add("

", Nothing, AddressOf Settings_OnClick)
'=======================================================================================
' Handle the LostFocus event to close the application when focus is lost
AddHandler contextMenu.LostFocus, Sub(sender, e) Application.Exit()
' Show the context menu
contextMenu.Show(Control.MousePosition)
' Run the application message loop manually to prevent premature exit
Application.Run()
End Sub
Friend Sub OnClickEX(sender As ToolStripMenuItemEX, e As EventArgs)
'' This event handler does nothing
'' MsgBox(ls(sender.ID))
''MsgBox(ls(sender.ID) + vbCrLf + GetShortcutTarget(ls(sender.ID)))
'Dim exe_ As String = GetShortcutTarget(ls(sender.ID))
'If IO.File.Exists(exe_) = True Then
' Dim processInfo As New ProcessStartInfo(exe_)
' Process.Start(processInfo)
' Application.Exit()
'Else
' sender.Enabled = False
'End If
Dim processInfo As New ProcessStartInfo(ls(sender.ID))
Process.Start(processInfo)
Application.Exit()
End Sub
Sub Close_OnClick(sender As Object, e As EventArgs)
' This event handler does nothing
' MsgBox("sadfdsad")
Application.Exit()
End Sub
Sub Settings_OnClick(sender As Object, e As EventArgs)
' This event handler does nothing
' MsgBox("sadfdsad")
Application.Exit()
End Sub
End Module