mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Show file icons for non images
This commit is contained in:
parent
a0730b66af
commit
c4fd1f5d70
6 changed files with 257 additions and 5 deletions
|
@ -88,5 +88,18 @@ public static class NativeConstants
|
|||
public const uint MA_NOACTIVATE = 3;
|
||||
public const uint MA_NOACTIVATEANDEAT = 4;
|
||||
public const uint MOUSE_MOVE = 0xF012;
|
||||
|
||||
public const string IID_IImageList = "46EB5926-582E-4017-9FDF-E8998DAA0950";
|
||||
public const string IID_IImageList2 = "192B9D83-50FC-457B-90A0-2B82A8B5DAE1";
|
||||
|
||||
public const int SHIL_LARGE = 0x0;
|
||||
public const int SHIL_SMALL = 0x1;
|
||||
public const int SHIL_EXTRALARGE = 0x2;
|
||||
public const int SHIL_SYSSMALL = 0x3;
|
||||
public const int SHIL_JUMBO = 0x4;
|
||||
public const int SHIL_LAST = 0x4;
|
||||
|
||||
public const int ILD_TRANSPARENT = 0x00000001;
|
||||
public const int ILD_IMAGE = 0x00000020;
|
||||
}
|
||||
}
|
|
@ -3171,4 +3171,45 @@ public enum EndSessionReasons : uint
|
|||
/// </summary>
|
||||
ENDSESSION_LOGOFF = 0x80000000
|
||||
}
|
||||
|
||||
[Flags]
|
||||
enum SHGFI : uint
|
||||
{
|
||||
/// <summary>get icon</summary>
|
||||
Icon = 0x000000100,
|
||||
/// <summary>get display name</summary>
|
||||
DisplayName = 0x000000200,
|
||||
/// <summary>get type name</summary>
|
||||
TypeName = 0x000000400,
|
||||
/// <summary>get attributes</summary>
|
||||
Attributes = 0x000000800,
|
||||
/// <summary>get icon location</summary>
|
||||
IconLocation = 0x000001000,
|
||||
/// <summary>return exe type</summary>
|
||||
ExeType = 0x000002000,
|
||||
/// <summary>get system icon index</summary>
|
||||
SysIconIndex = 0x000004000,
|
||||
/// <summary>put a link overlay on icon</summary>
|
||||
LinkOverlay = 0x000008000,
|
||||
/// <summary>show icon in selected state</summary>
|
||||
Selected = 0x000010000,
|
||||
/// <summary>get only specified attributes</summary>
|
||||
Attr_Specified = 0x000020000,
|
||||
/// <summary>get large icon</summary>
|
||||
LargeIcon = 0x000000000,
|
||||
/// <summary>get small icon</summary>
|
||||
SmallIcon = 0x000000001,
|
||||
/// <summary>get open icon</summary>
|
||||
OpenIcon = 0x000000002,
|
||||
/// <summary>get shell size icon</summary>
|
||||
ShellIconSize = 0x000000004,
|
||||
/// <summary>pszPath is a pidl</summary>
|
||||
PIDL = 0x000000008,
|
||||
/// <summary>use passed dwFileAttribute</summary>
|
||||
UseFileAttributes = 0x000000010,
|
||||
/// <summary>apply the appropriate overlays</summary>
|
||||
AddOverlays = 0x000000020,
|
||||
/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
|
||||
OverlayIndex = 0x000000040
|
||||
}
|
||||
}
|
|
@ -382,6 +382,12 @@ public static extern bool CreateProcess(string lpApplicationName, string lpComma
|
|||
[DllImport("shell32.dll")]
|
||||
public static extern IntPtr SHAppBarMessage(uint dwMessage, [In] ref APPBARDATA pData);
|
||||
|
||||
[DllImport("shell32.dll", EntryPoint = "#727")]
|
||||
public extern static int SHGetImageList(int iImageList, ref Guid riid, ref IImageList ppv);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
|
||||
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
|
||||
|
||||
[DllImport("shell32.dll")]
|
||||
public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
|
||||
|
||||
|
|
|
@ -505,5 +505,46 @@ public static bool CreateProcess(string path, string arguments, CreateProcessFla
|
|||
|
||||
return CreateProcess(path, $"\"{path}\" {arguments}", ref pSec, ref tSec, false, (uint)flags, IntPtr.Zero, null, ref sInfo, out pInfo);
|
||||
}
|
||||
|
||||
public static Icon GetFileIcon(string filePath, bool isSmallIcon)
|
||||
{
|
||||
SHFILEINFO shfi = new SHFILEINFO();
|
||||
|
||||
SHGFI flags = SHGFI.Icon;
|
||||
|
||||
if (isSmallIcon)
|
||||
{
|
||||
flags |= SHGFI.SmallIcon;
|
||||
}
|
||||
else
|
||||
{
|
||||
flags |= SHGFI.LargeIcon;
|
||||
}
|
||||
|
||||
SHGetFileInfo(filePath, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)flags);
|
||||
|
||||
Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
|
||||
DestroyIcon(shfi.hIcon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
public static Icon GetJumboFileIcon(string filePath)
|
||||
{
|
||||
SHFILEINFO shfi = new SHFILEINFO();
|
||||
|
||||
SHGFI flags = SHGFI.SysIconIndex | SHGFI.UseFileAttributes;
|
||||
SHGetFileInfo(filePath, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)flags);
|
||||
|
||||
IImageList spiml = null;
|
||||
Guid guil = new Guid(NativeConstants.IID_IImageList2);
|
||||
|
||||
SHGetImageList(NativeConstants.SHIL_JUMBO, ref guil, ref spiml);
|
||||
IntPtr hIcon = IntPtr.Zero;
|
||||
spiml.GetIcon(shfi.iIcon, NativeConstants.ILD_TRANSPARENT | NativeConstants.ILD_IMAGE, ref hIcon);
|
||||
|
||||
Icon icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
||||
DestroyIcon(hIcon);
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -841,4 +841,142 @@ public struct PROCESS_INFORMATION
|
|||
public int dwProcessId;
|
||||
public int dwThreadId;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SHFILEINFO
|
||||
{
|
||||
public const int NAMESIZE = 80;
|
||||
public IntPtr hIcon;
|
||||
public int iIcon;
|
||||
public uint dwAttributes;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string szDisplayName;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
|
||||
public string szTypeName;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct IMAGELISTDRAWPARAMS
|
||||
{
|
||||
public int cbSize;
|
||||
public IntPtr himl;
|
||||
public int i;
|
||||
public IntPtr hdcDst;
|
||||
public int x;
|
||||
public int y;
|
||||
public int cx;
|
||||
public int cy;
|
||||
public int xBitmap; // x offest from the upperleft of bitmap
|
||||
public int yBitmap; // y offset from the upperleft of bitmap
|
||||
public int rgbBk;
|
||||
public int rgbFg;
|
||||
public int fStyle;
|
||||
public int dwRop;
|
||||
public int fState;
|
||||
public int Frame;
|
||||
public int crEffect;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct IMAGEINFO
|
||||
{
|
||||
public IntPtr hbmImage;
|
||||
public IntPtr hbmMask;
|
||||
public int Unused1;
|
||||
public int Unused2;
|
||||
public RECT rcImage;
|
||||
}
|
||||
|
||||
[ComImportAttribute()]
|
||||
[GuidAttribute("46EB5926-582E-4017-9FDF-E8998DAA0950")]
|
||||
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IImageList
|
||||
{
|
||||
[PreserveSig]
|
||||
int Add(IntPtr hbmImage, IntPtr hbmMask, ref int pi);
|
||||
|
||||
[PreserveSig]
|
||||
int ReplaceIcon(int i, IntPtr hicon, ref int pi);
|
||||
|
||||
[PreserveSig]
|
||||
int SetOverlayImage(int iImage, int iOverlay);
|
||||
|
||||
[PreserveSig]
|
||||
int Replace(int i, IntPtr hbmImage, IntPtr hbmMask);
|
||||
|
||||
[PreserveSig]
|
||||
int AddMasked(IntPtr hbmImage, int crMask, ref int pi);
|
||||
|
||||
[PreserveSig]
|
||||
int Draw(ref IMAGELISTDRAWPARAMS pimldp);
|
||||
|
||||
[PreserveSig]
|
||||
int Remove(int i);
|
||||
|
||||
[PreserveSig]
|
||||
int GetIcon(int i, int flags, ref IntPtr picon);
|
||||
|
||||
[PreserveSig]
|
||||
int GetImageInfo(int i, ref IMAGEINFO pImageInfo);
|
||||
|
||||
[PreserveSig]
|
||||
int Copy(int iDst, IImageList punkSrc, int iSrc, int uFlags);
|
||||
|
||||
[PreserveSig]
|
||||
int Merge(int i1, IImageList punk2, int i2, int dx, int dy, ref Guid riid, ref IntPtr ppv);
|
||||
|
||||
[PreserveSig]
|
||||
int Clone(ref Guid riid, ref IntPtr ppv);
|
||||
|
||||
[PreserveSig]
|
||||
int GetImageRect(int i, ref RECT prc);
|
||||
|
||||
[PreserveSig]
|
||||
int GetIconSize(ref int cx, ref int cy);
|
||||
|
||||
[PreserveSig]
|
||||
int SetIconSize(int cx, int cy);
|
||||
|
||||
[PreserveSig]
|
||||
int GetImageCount(ref int pi);
|
||||
|
||||
[PreserveSig]
|
||||
int SetImageCount(int uNewCount);
|
||||
|
||||
[PreserveSig]
|
||||
int SetBkColor(int clrBk, ref int pclr);
|
||||
|
||||
[PreserveSig]
|
||||
int GetBkColor(ref int pclr);
|
||||
|
||||
[PreserveSig]
|
||||
int BeginDrag(int iTrack, int dxHotspot, int dyHotspot);
|
||||
|
||||
[PreserveSig]
|
||||
int EndDrag();
|
||||
|
||||
[PreserveSig]
|
||||
int DragEnter(IntPtr hwndLock, int x, int y);
|
||||
|
||||
[PreserveSig]
|
||||
int DragLeave(IntPtr hwndLock);
|
||||
|
||||
[PreserveSig]
|
||||
int DragMove(int x, int y);
|
||||
|
||||
[PreserveSig]
|
||||
int SetDragCursorImage(ref IImageList punk, int iDrag, int dxHotspot, int dyHotspot);
|
||||
|
||||
[PreserveSig]
|
||||
int DragShowNolock(int fShow);
|
||||
|
||||
[PreserveSig]
|
||||
int GetDragImage(ref POINT ppt, ref POINT pptHotspot, ref Guid riid, ref IntPtr ppv);
|
||||
|
||||
[PreserveSig]
|
||||
int GetItemFlags(int i, ref int dwFlags);
|
||||
|
||||
[PreserveSig]
|
||||
int GetOverlayImage(int iOverlay, ref int piIndex);
|
||||
}
|
||||
}
|
|
@ -164,6 +164,8 @@ public void UpdateThumbnail()
|
|||
string filePath = Task.Info.FilePath;
|
||||
|
||||
try
|
||||
{
|
||||
if (Helpers.IsImageFile(filePath))
|
||||
{
|
||||
using (Image img = ImageHelpers.LoadImage(filePath))
|
||||
{
|
||||
|
@ -177,6 +179,17 @@ public void UpdateThumbnail()
|
|||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (Icon icon = NativeMethods.GetJumboFileIcon(filePath))
|
||||
using (Image img = icon.ToBitmap())
|
||||
{
|
||||
ThumbnailImage = ImageHelpers.ResizeImage(img, ThumbnailSize, false);
|
||||
pbThumbnail.Image = ThumbnailImage;
|
||||
pbThumbnail.Cursor = pThumbnail.Cursor = Cursors.Default;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
|
|
Loading…
Reference in a new issue