472,988 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

Extracting an Icon and Placing It On The Desktop (C# Language)

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.";
Nov 17 '05 #1
2 2759
Try to make your post, let's say, 8 pages shorter, and maybe someone will
find time to read and answer ;-)
Nov 17 '05 #2
Hi

Based on my understanding, you wants to create a shortcut for certain exe
file in C#.
If so, here is a link for you reference.

ShellLink
http://www.msjogren.net/dotnet/eng/s..._shelllink.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Cooper | last post by:
Hi all, I am attempting to deploy a vb.net application. That is working fine. I also configured the installer so shortcuts for the application would appear on the desktop and the user program...
0
by: pithhelmet | last post by:
Hello, Is there a way to position an icon on the desktop from within C# programatically?? thanks tony
1
by: QBuildPete | last post by:
VB5 setup used to create the application icon on the desktop. I can't find anywhere in the VB6 deployment wizard where I can do this. Can anybody help please.
3
by: Mark | last post by:
Hi I'm using vb.Net in VS2005 and want to extract the icon for file refence records. I saw an example a while back that searched the registry for the default icon etc. It was a little long...
4
by: Mayur1234 | last post by:
hi buddies, i have developed an application (for myself) and now i want to create an icon and link it to the java class of first screen. please help me on that..... i am stuck at it since many...
1
by: Hutch | last post by:
Is there a way to change the Icon of accessto a different picture? I want to do this in the program some how so when i rewrite the MDE file it will appear as a change on everyones desktop. If i just...
2
by: =?Utf-8?B?bGVvcmFt?= | last post by:
My OS is XP. Is it possible to remove recycle bin icon from desktop? If so, how. Normally, a right click delete will do it but there is no delete when I right click the icon. Thanks. RickC
1
by: =?Utf-8?B?bGVvcmFt?= | last post by:
Is it possible to delete the recycle bin icon from the desktop on XP OS? If so, how is it done? Normally, a right click and delete will do it but when I right click there is no delete option. ...
2
by: kim | last post by:
Hi there is it possible to make a link on a webpage - that users can click on And then get/download a direct shortcut to a this website ?? Like: <a...
13
by: serdar | last post by:
I had a faded IE7 icon on my (XP) desktop. I restarted in safe mode and switched back, it turned to normal. Anyone knows what does a faded IE7 desktop icon mean? Thanks.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.