get icon from exe.real?

Byak@

Active Member
Licensed User
if in basic4ppc we have IconList may be it's real to get the icon from exe?
 

Byak@

Active Member
Licensed User
what i'm find for c#
B4X:
[COLOR=blue] using [/COLOR]System;[COLOR=blue]using [/COLOR]System.Drawing;[COLOR=blue]using [/COLOR]System.Runtime.InteropServices;[COLOR=blue]using [/COLOR]System.IO;
[COLOR=blue]class [/COLOR]Program[COLOR=#454545]{[/COLOR][COLOR=blue]static [/COLOR][COLOR=blue]void [/COLOR]Main[COLOR=#454545]()[/COLOR][COLOR=#454545]    {[/COLOR][COLOR=green]// например, получаем иконку для PDF[/COLOR][COLOR=green]        // имя файла - от балды, главное это расширение[/COLOR][COLOR=blue]string [/COLOR]fileName = [COLOR=seagreen]"bla-bla-bla.pdf"[/COLOR];
        [COLOR=green]// вся кухня - здесь[/COLOR]Shell32.SHFILEINFO shfi = [COLOR=blue]new [/COLOR]Shell32.SHFILEINFO[COLOR=#454545]()[/COLOR];        Shell32.SHGetFileInfo[COLOR=#454545]([/COLOR]fileName, Shell32.FILE_ATTRIBUTE_NORMAL, [COLOR=blue]ref [/COLOR]shfi,             [COLOR=#454545]([/COLOR][COLOR=blue]uint[/COLOR][COLOR=#454545])[/COLOR]Marshal.SizeOf[COLOR=#454545]([/COLOR]shfi[COLOR=#454545])[/COLOR], Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES | Shell32.SHGFI_LARGEICON[COLOR=#454545])[/COLOR];        Icon icon = [COLOR=#454545]([/COLOR]Icon[COLOR=#454545])[/COLOR]Icon.FromHandle[COLOR=#454545]([/COLOR]shfi.hIcon[COLOR=#454545])[/COLOR].Clone[COLOR=#454545]()[/COLOR];        User32.DestroyIcon[COLOR=#454545]([/COLOR]shfi.hIcon[COLOR=#454545])[/COLOR];
        [COLOR=green]// теперь можем, например, сохранить в bmp[/COLOR]icon.ToBitmap[COLOR=#454545]()[/COLOR].Save[COLOR=#454545]([/COLOR]@[COLOR=seagreen]"c:\pdf_icon.bmp"[/COLOR][COLOR=#454545])[/COLOR];    [COLOR=#454545]}[/COLOR][COLOR=#454545]}[/COLOR]

[COLOR=blue]public [/COLOR][COLOR=blue]class [/COLOR]Shell32[COLOR=#454545]{[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]int [/COLOR]MAX_PATH = 256;    [COLOR=#454545][[/COLOR]StructLayout[COLOR=#454545]([/COLOR]LayoutKind.Sequential[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]struct [/COLOR]SHITEMID    [COLOR=#454545]{[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]ushort [/COLOR]cb;        [COLOR=#454545][[/COLOR]MarshalAs[COLOR=#454545]([/COLOR]UnmanagedType.LPArray[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]byte[/COLOR][COLOR=#454545][] [/COLOR]abID;    [COLOR=#454545]}[/COLOR]
[COLOR=#454545]    [[/COLOR]StructLayout[COLOR=#454545]([/COLOR]LayoutKind.Sequential[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]struct [/COLOR]ITEMIDLIST    [COLOR=#454545]{[/COLOR][COLOR=blue]public [/COLOR]SHITEMID mkid;    [COLOR=#454545]}[/COLOR]
[COLOR=#454545]    [[/COLOR]StructLayout[COLOR=#454545]([/COLOR]LayoutKind.Sequential[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]struct [/COLOR]SHFILEINFO    [COLOR=#454545]{[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]int [/COLOR]NAMESIZE = 80;        [COLOR=blue]public [/COLOR]IntPtr hIcon;        [COLOR=blue]public [/COLOR][COLOR=blue]int [/COLOR]iIcon;        [COLOR=blue]public [/COLOR][COLOR=blue]uint [/COLOR]dwAttributes;        [COLOR=#454545][[/COLOR]MarshalAs[COLOR=#454545]([/COLOR]UnmanagedType.ByValTStr, SizeConst = MAX_PATH[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]string [/COLOR]szDisplayName;        [COLOR=#454545][[/COLOR]MarshalAs[COLOR=#454545]([/COLOR]UnmanagedType.ByValTStr, SizeConst = NAMESIZE[COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]string [/COLOR]szTypeName;    [COLOR=#454545]}[/COLOR];
    [COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_ICON = 0x000000100;     [COLOR=green]// get icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_DISPLAYNAME = 0x000000200;     [COLOR=green]// get display name[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_TYPENAME = 0x000000400;     [COLOR=green]// get type name[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_ATTRIBUTES = 0x000000800;     [COLOR=green]// get attributes[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_ICONLOCATION = 0x000001000;     [COLOR=green]// get icon location[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_EXETYPE = 0x000002000;     [COLOR=green]// return exe type[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_SYSICONINDEX = 0x000004000;     [COLOR=green]// get system icon index[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_LINKOVERLAY = 0x000008000;     [COLOR=green]// put a link overlay on icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_SELECTED = 0x000010000;     [COLOR=green]// show icon in selected state[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_ATTR_SPECIFIED = 0x000020000;     [COLOR=green]// get only specified attributes[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_LARGEICON = 0x000000000;     [COLOR=green]// get large icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_SMALLICON = 0x000000001;     [COLOR=green]// get small icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_OPENICON = 0x000000002;     [COLOR=green]// get open icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_SHELLICONSIZE = 0x000000004;     [COLOR=green]// get shell size icon[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_PIDL = 0x000000008;     [COLOR=green]// pszPath is a pidl[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_USEFILEATTRIBUTES = 0x000000010;     [COLOR=green]// use passed dwFileAttribute[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_ADDOVERLAYS = 0x000000020;     [COLOR=green]// apply the appropriate overlays[/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]SHGFI_OVERLAYINDEX = 0x000000040;     [COLOR=green]// Get the index of the overlay[/COLOR]
[COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]FILE_ATTRIBUTE_DIRECTORY = 0x00000010;    [COLOR=blue]public [/COLOR][COLOR=blue]const [/COLOR][COLOR=blue]uint [/COLOR]FILE_ATTRIBUTE_NORMAL = 0x00000080;
    [COLOR=#454545][[/COLOR]DllImport[COLOR=#454545]([/COLOR][COLOR=seagreen]"Shell32.dll"[/COLOR][COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]static [/COLOR][COLOR=blue]extern [/COLOR]IntPtr SHGetFileInfo[COLOR=#454545]([/COLOR][COLOR=blue]string [/COLOR]pszPath,        [COLOR=blue]uint [/COLOR]dwFileAttributes,        [COLOR=blue]ref [/COLOR]SHFILEINFO psfi,        [COLOR=blue]uint [/COLOR]cbFileInfo,        [COLOR=blue]uint [/COLOR]uFlags        [COLOR=#454545])[/COLOR];[COLOR=#454545]}[/COLOR]
[COLOR=blue]public [/COLOR][COLOR=blue]class [/COLOR]User32[COLOR=#454545]{[/COLOR][COLOR=#454545]    [[/COLOR]DllImport[COLOR=#454545]([/COLOR][COLOR=seagreen]"User32.dll"[/COLOR][COLOR=#454545])][/COLOR][COLOR=blue]public [/COLOR][COLOR=blue]static [/COLOR][COLOR=blue]extern [/COLOR][COLOR=blue]int [/COLOR]DestroyIcon[COLOR=#454545]([/COLOR]IntPtr hIcon[COLOR=#454545])[/COLOR];[COLOR=#454545]}[/COLOR]

and it
B4X:
[COLOR=blue]public [/COLOR][COLOR=blue]static [/COLOR]Icon ExtractAssociatedIcon [COLOR=#454545]([/COLOR]
[COLOR=blue]string [/COLOR]filePath
[COLOR=#454545])[/COLOR]
B4X:
Icon ico = Icon.ExtractAssociatedIcon[COLOR=#454545]([/COLOR]@[COLOR=seagreen]"C:\WINDOWS\system32\notepad.exe"[/COLOR][COLOR=#454545])[/COLOR];
 

agraham

Expert
Licensed User
Longtime User
get the icon from exe?
Like this, which only works in .NET 2.0 or later and only on the desktop. Obj1 is a Door library Object.
B4X:
Obj1.New1(False)
' create a new empty Icon
Obj1.CreateNew("System.Drawing.Icon" & Obj1.System_Drawing)
' get an Icon from a file
Obj1.Value = Obj1.RunMethod2("ExtractAssociatedIcon","C:\WINDOWS\system32\notepad.exe", "System.String")
' add it to an ControlsExDesktop IconList
IconList.AddIcon(Obj1.Value)
' or turn it into a bitmap
Image1.Image = Obj1.RunMethod("ToBitmap")   
' alternatively
Image1.Image = IconList.ItemBitmap(0)
 

Byak@

Active Member
Licensed User
Top