D
Dickyb
Extracting an Icon and Placing It On The Desktop (C# Language)
I constructed a suite of programs in C++ several years ago that handle my
financial portfolio, and now I have converted them to C#. The only
significant problem that I have encountered in the conversion is this one -
extracting an icon from the 'KTEntryPoint' program into the software suite
and placing that icon on the PC Desktop.
I found this icon extractor in
'http://pinvoke.net/default.aspx/shell32.SHGetFileInfo':
// Summary description for ShellIcon. Get a small or large Icon with an
easy
// C# function call. Source: Alternative Managed API #1:
// http://pinvoke.net/default.aspx/shell32.SHGetFileInfo.
// Use either GetSmallIcon(string fileName) or GetLargeIcon(string
fileName).
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
#endregion
namespace AutoIconToDesktop // Trial program (local).
{
partial class Form1 : Form
{
public IntPtr LargeIconHandle; // Local add
variable.
public Form1()
{ InitializeComponent();
LargeIconHandle =
GetLargeIcon("KTEntryPoint.exe");
// Now how do I put this 'large icon'
onto the Desktop, in a persistent location?
}
}
public class ShellIcon
{
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{ public IntPtr hIcon;
public IntPtr iIcon;
public unit dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = 80)]
public string szTypeName;
}; // End of structure.
class Win32
{ public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
// Large icon.
public const uint SHGFI_SMALLICON = 0x1;
// Small icon.
[DllImport("shell32.dll")]
public static extern IntPtr
SHGetFileInfo(string
pszPath, uint dwFileAttributes, ref SHFILEINFO psfi,
uint cbSizeFileInfo, uint
uFlags);
}
public ShellIcon()
{ //
// TODO: Add constructor logic here.
//
}
public static Icon GetSmallIcon(string fileName)
{ IntPtr hImgSmall; // The handle to the system
image list.
SHFILEINFO shinfo = new SHFILEINFO();
// Use this to get the small Icon.
hImgSmall =
win32.SHGetFileInfo(fileName, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
win32.SHGFI_ICON |
win32.SHGFI_SMALLICON);
// The icon is returned in the hIcon
member of the shinfo struct.
return
System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
public static Icon GetLargeIcon(string fileName)
{ IntPtr hImgLarge; // Handle to the system image
list.
SHFILEINFO shinfo = new SHFILEINFO();
// Use this to get the large Icon.
hImgLarge =
win32.SHGetFileInfo(filename, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
win32.SHGFI_ICON |
win32.SHGFI_LARGEICON);
// The icon is returned in the hIcon
member of the shinfo struct.
return
System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
}
}
Shobjidl, April 2003 (Borland + MS C++)
This is the code we placed in a Borland program in 2003 to take an icon out
of an executable program named 'KTEntryPoint' and install it on the Desktop
(Operating system was Windows 2000; machine is a Compaq PC) Current
operating system is 'XP'; machine is a Compaq PC:
KT002: #include <vcl.h>
KT003: #pragma hrdstop
KT004: #include 'Unit1.h"
KT005: #include <shobjidl.h>
KT006: #include<stdio.h>
// The next 26 lines of code are provided by Microsoft:
KT007: HRESULT CreateLink(LPCSTR lpszPathObj, LPSTR lpszPathLink, LPSTR
lpszDesc)
KT008: {
KT009: HRESULT hres;
KT010: IShellLink* psl;
KT011: // Get a pointer to the IShellLink interface.
KT012: hres = CoCreateInstance((_GUID)CLSID_ShellLink, Null,
CLSCTX_INPROC_SERVER,
(_GUID)IID_IShellLink, (void**)&psl);
KT013: if (SUCCEEDED(hres))
KT014: {IPersistFile* ppf;
KT015: // Set the path to the shortcut target, and add the
description.
KT016: psl->SetPath(lpszPathObj);
KT017: psl->SetDescription(lpszDesc);
KT018: // Querry IShellLink for the IPersistFile interface for saving
the shortcut
KT019: // in persistent storage.
KT020: hres = psl->QueryInterface((_GUID)IID_IPersistFile,
(void**)&ppf);
KT021: if (SUCCEEDED(hres))
KT022: {WORD wsz[MAX_PATH];
KT023: // Ensure that the string is ANSI.
KT024: MultiByteToWideChar(CP_ACP, 0, lpszPatrhLink, -1,
(wchar_t*)wsz, MAX_PATH);
KT025 // Save the link by calling IPersistFile::Save.
KT026: hres = ppf->Save((wchar_t*) wsz, TRUE);
KT027: ppf->Release();
KT028: }
KT029: psl->Release();
KT030: }
KT031: return hres;
KT032: }
// End of Microsoft code.
//----------------------------------------------------------------------------------------------------------//
KT034: #pragma package(smart_init)
KT035: #pragma resource "*.dfm"
KT036: TForm1 *Form1;
KT115: // All files copied from floppy to 'Program Files\\KT'. Now
create the project icon
// on the Desktop.
KT116: CoInitialize(NULL);
KT117: CreateLink("C:\\Program Files\\KT\\KTEntryPoint.exe",
"C:\\Documents and Settings\\All
Users\\Desktop\\Keeping_Track.lnk",
"Keeping Track");
KT118: CoUnitialize();
KT119: Label1->Caption =
"All files written to 'C:\\Program Files\KT' and KT
Icon placed on Desktop.";
I constructed a suite of programs in C++ several years ago that handle my
financial portfolio, and now I have converted them to C#. The only
significant problem that I have encountered in the conversion is this one -
extracting an icon from the 'KTEntryPoint' program into the software suite
and placing that icon on the PC Desktop.
I found this icon extractor in
'http://pinvoke.net/default.aspx/shell32.SHGetFileInfo':
// Summary description for ShellIcon. Get a small or large Icon with an
easy
// C# function call. Source: Alternative Managed API #1:
// http://pinvoke.net/default.aspx/shell32.SHGetFileInfo.
// Use either GetSmallIcon(string fileName) or GetLargeIcon(string
fileName).
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
#endregion
namespace AutoIconToDesktop // Trial program (local).
{
partial class Form1 : Form
{
public IntPtr LargeIconHandle; // Local add
variable.
public Form1()
{ InitializeComponent();
LargeIconHandle =
GetLargeIcon("KTEntryPoint.exe");
// Now how do I put this 'large icon'
onto the Desktop, in a persistent location?
}
}
public class ShellIcon
{
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{ public IntPtr hIcon;
public IntPtr iIcon;
public unit dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = 80)]
public string szTypeName;
}; // End of structure.
class Win32
{ public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
// Large icon.
public const uint SHGFI_SMALLICON = 0x1;
// Small icon.
[DllImport("shell32.dll")]
public static extern IntPtr
SHGetFileInfo(string
pszPath, uint dwFileAttributes, ref SHFILEINFO psfi,
uint cbSizeFileInfo, uint
uFlags);
}
public ShellIcon()
{ //
// TODO: Add constructor logic here.
//
}
public static Icon GetSmallIcon(string fileName)
{ IntPtr hImgSmall; // The handle to the system
image list.
SHFILEINFO shinfo = new SHFILEINFO();
// Use this to get the small Icon.
hImgSmall =
win32.SHGetFileInfo(fileName, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
win32.SHGFI_ICON |
win32.SHGFI_SMALLICON);
// The icon is returned in the hIcon
member of the shinfo struct.
return
System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
public static Icon GetLargeIcon(string fileName)
{ IntPtr hImgLarge; // Handle to the system image
list.
SHFILEINFO shinfo = new SHFILEINFO();
// Use this to get the large Icon.
hImgLarge =
win32.SHGetFileInfo(filename, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
win32.SHGFI_ICON |
win32.SHGFI_LARGEICON);
// The icon is returned in the hIcon
member of the shinfo struct.
return
System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
}
}
Shobjidl, April 2003 (Borland + MS C++)
This is the code we placed in a Borland program in 2003 to take an icon out
of an executable program named 'KTEntryPoint' and install it on the Desktop
(Operating system was Windows 2000; machine is a Compaq PC) Current
operating system is 'XP'; machine is a Compaq PC:
KT002: #include <vcl.h>
KT003: #pragma hrdstop
KT004: #include 'Unit1.h"
KT005: #include <shobjidl.h>
KT006: #include<stdio.h>
// The next 26 lines of code are provided by Microsoft:
KT007: HRESULT CreateLink(LPCSTR lpszPathObj, LPSTR lpszPathLink, LPSTR
lpszDesc)
KT008: {
KT009: HRESULT hres;
KT010: IShellLink* psl;
KT011: // Get a pointer to the IShellLink interface.
KT012: hres = CoCreateInstance((_GUID)CLSID_ShellLink, Null,
CLSCTX_INPROC_SERVER,
(_GUID)IID_IShellLink, (void**)&psl);
KT013: if (SUCCEEDED(hres))
KT014: {IPersistFile* ppf;
KT015: // Set the path to the shortcut target, and add the
description.
KT016: psl->SetPath(lpszPathObj);
KT017: psl->SetDescription(lpszDesc);
KT018: // Querry IShellLink for the IPersistFile interface for saving
the shortcut
KT019: // in persistent storage.
KT020: hres = psl->QueryInterface((_GUID)IID_IPersistFile,
(void**)&ppf);
KT021: if (SUCCEEDED(hres))
KT022: {WORD wsz[MAX_PATH];
KT023: // Ensure that the string is ANSI.
KT024: MultiByteToWideChar(CP_ACP, 0, lpszPatrhLink, -1,
(wchar_t*)wsz, MAX_PATH);
KT025 // Save the link by calling IPersistFile::Save.
KT026: hres = ppf->Save((wchar_t*) wsz, TRUE);
KT027: ppf->Release();
KT028: }
KT029: psl->Release();
KT030: }
KT031: return hres;
KT032: }
// End of Microsoft code.
//----------------------------------------------------------------------------------------------------------//
KT034: #pragma package(smart_init)
KT035: #pragma resource "*.dfm"
KT036: TForm1 *Form1;
KT115: // All files copied from floppy to 'Program Files\\KT'. Now
create the project icon
// on the Desktop.
KT116: CoInitialize(NULL);
KT117: CreateLink("C:\\Program Files\\KT\\KTEntryPoint.exe",
"C:\\Documents and Settings\\All
Users\\Desktop\\Keeping_Track.lnk",
"Keeping Track");
KT118: CoUnitialize();
KT119: Label1->Caption =
"All files written to 'C:\\Program Files\KT' and KT
Icon placed on Desktop.";