双击启动 文件小图标修复
This commit is contained in:
@@ -36,7 +36,7 @@ namespace GeekDesk.Util
|
||||
|
||||
public static BitmapImage GetBitmapImage(string filePath)
|
||||
{
|
||||
Icon ico;
|
||||
Icon ico = null;
|
||||
//选中文件中的图标总数
|
||||
var iconTotalCount = PrivateExtractIcons(filePath, 0, 0, 0, null, null, 0, 0);
|
||||
//用于接收获取到的图标指针
|
||||
@@ -53,12 +53,20 @@ namespace GeekDesk.Util
|
||||
{
|
||||
ip = hIcons[0];
|
||||
ico = Icon.FromHandle(ip);
|
||||
if (IsMinOrTransparent(ico))
|
||||
{
|
||||
ico = null;
|
||||
}
|
||||
}
|
||||
else if (GetBlurExts().Contains(ext))
|
||||
if (ico == null && GetBlurExts().Contains(ext))
|
||||
{
|
||||
ico = Icon.ExtractAssociatedIcon(filePath);
|
||||
if (IsMinOrTransparent(ico))
|
||||
{
|
||||
ico = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (ico == null)
|
||||
{
|
||||
ip = GetJumboIcon(GetIconIndex(filePath));
|
||||
ico = Icon.FromHandle(ip);
|
||||
@@ -86,6 +94,36 @@ namespace GeekDesk.Util
|
||||
return bmpImage.Clone();
|
||||
}
|
||||
|
||||
|
||||
private static bool IsMinOrTransparent(Icon ico)
|
||||
{
|
||||
Bitmap bm = ico.ToBitmap();
|
||||
double w = bm.Width;
|
||||
double h = bm.Height;
|
||||
|
||||
Color middleColor = bm.GetPixel((int)(w * 0.50), (int)(h * 0.50));
|
||||
Color transparent = Color.FromArgb(0, 0, 0, 0);
|
||||
|
||||
//如果中间像素不为空 直接判断此icon不为空
|
||||
if (middleColor != Color.Transparent && middleColor != transparent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//判断中间一条横线像素 有不透明元素即判断 icon不为空
|
||||
Color c;
|
||||
int h2 = (int)h / 2;
|
||||
for (int i=0; i<(int)w; i++)
|
||||
{
|
||||
c = bm.GetPixel(i, h2);
|
||||
if (c!= Color.Transparent && c != transparent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static ImageCodecInfo GetEncoderInfo(String mimeType)
|
||||
{
|
||||
int j;
|
||||
@@ -416,7 +454,6 @@ namespace GeekDesk.Util
|
||||
int iOverlay,
|
||||
ref int piIndex);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GeekDesk.Util
|
||||
@@ -10,6 +12,8 @@ namespace GeekDesk.Util
|
||||
public class FileUtil
|
||||
{
|
||||
|
||||
private static readonly string NO_PATH = ".*{.*}.*";
|
||||
|
||||
public static string GetTargetPathByLnk(string filePath)
|
||||
{
|
||||
try
|
||||
@@ -21,7 +25,12 @@ namespace GeekDesk.Util
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return shortcut.TargetPath;
|
||||
string path = shortcut.TargetPath;
|
||||
if (path==null || Regex.IsMatch(path, NO_PATH))
|
||||
{
|
||||
path = ParseShortcut(filePath);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
#pragma warning disable CS0168 // 声明了变量“e”,但从未使用过
|
||||
catch (Exception e)
|
||||
@@ -30,5 +39,73 @@ namespace GeekDesk.Util
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
UINT MsiGetShortcutTarget(
|
||||
LPCTSTR szShortcutTarget,
|
||||
LPTSTR szProductCode,
|
||||
LPTSTR szFeatureId,
|
||||
LPTSTR szComponentCode
|
||||
);
|
||||
*/
|
||||
[DllImport("msi.dll", CharSet = CharSet.Auto)]
|
||||
static extern int MsiGetShortcutTarget(string targetFile, StringBuilder productCode, StringBuilder featureID, StringBuilder componentCode);
|
||||
|
||||
public enum InstallState
|
||||
{
|
||||
NotUsed = -7,
|
||||
BadConfig = -6,
|
||||
Incomplete = -5,
|
||||
SourceAbsent = -4,
|
||||
MoreData = -3,
|
||||
InvalidArg = -2,
|
||||
Unknown = -1,
|
||||
Broken = 0,
|
||||
Advertised = 1,
|
||||
Removed = 1,
|
||||
Absent = 2,
|
||||
Local = 3,
|
||||
Source = 4,
|
||||
Default = 5
|
||||
}
|
||||
|
||||
public const int MaxFeatureLength = 38;
|
||||
public const int MaxGuidLength = 38;
|
||||
public const int MaxPathLength = 1024;
|
||||
|
||||
/*
|
||||
INSTALLSTATE MsiGetComponentPath(
|
||||
LPCTSTR szProduct,
|
||||
LPCTSTR szComponent,
|
||||
LPTSTR lpPathBuf,
|
||||
DWORD* pcchBuf
|
||||
);
|
||||
*/
|
||||
[DllImport("msi.dll", CharSet = CharSet.Auto)]
|
||||
static extern InstallState MsiGetComponentPath(string productCode, string componentCode, StringBuilder componentPath, ref int componentPathBufferSize);
|
||||
|
||||
public static string ParseShortcut(string file)
|
||||
{
|
||||
StringBuilder product = new StringBuilder(MaxGuidLength + 1);
|
||||
StringBuilder feature = new StringBuilder(MaxFeatureLength + 1);
|
||||
StringBuilder component = new StringBuilder(MaxGuidLength + 1);
|
||||
|
||||
MsiGetShortcutTarget(file, product, feature, component);
|
||||
|
||||
int pathLength = MaxPathLength;
|
||||
StringBuilder path = new StringBuilder(pathLength);
|
||||
|
||||
InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength);
|
||||
if (installState == InstallState.Local)
|
||||
{
|
||||
return path.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
87
Util/Functions.cs
Normal file
87
Util/Functions.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
namespace GeekDesk.Util
|
||||
{
|
||||
|
||||
public class Functions
|
||||
{
|
||||
public static string GetShortcutTarget(string file)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (System.IO.Path.GetExtension(file).ToLower() != ".lnk")
|
||||
{
|
||||
throw new Exception("Supplied file must be a .LNK file");
|
||||
}
|
||||
|
||||
FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read);
|
||||
using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream))
|
||||
{
|
||||
fileStream.Seek(0x14, SeekOrigin.Begin); // Seek to flags
|
||||
uint flags = fileReader.ReadUInt32(); // Read flags
|
||||
if ((flags & 1) == 1)
|
||||
{ // Bit 1 set means we have to
|
||||
// skip the shell item ID list
|
||||
fileStream.Seek(0x4c, SeekOrigin.Begin); // Seek to the end of the header
|
||||
uint offset = fileReader.ReadUInt16(); // Read the length of the Shell item ID list
|
||||
fileStream.Seek(offset, SeekOrigin.Current); // Seek past it (to the file locator info)
|
||||
}
|
||||
|
||||
long fileInfoStartsAt = fileStream.Position; // Store the offset where the file info
|
||||
// structure begins
|
||||
uint totalStructLength = fileReader.ReadUInt32(); // read the length of the whole struct
|
||||
fileStream.Seek(0xc, SeekOrigin.Current); // seek to offset to base pathname
|
||||
uint fileOffset = fileReader.ReadUInt32(); // read offset to base pathname
|
||||
// the offset is from the beginning of the file info struct (fileInfoStartsAt)
|
||||
fileStream.Seek((fileInfoStartsAt + fileOffset), SeekOrigin.Begin); // Seek to beginning of
|
||||
// base pathname (target)
|
||||
long pathLength = (totalStructLength + fileInfoStartsAt) - fileStream.Position - 2; // read
|
||||
// the base pathname. I don't need the 2 terminating nulls.
|
||||
char[] linkTarget = fileReader.ReadChars((int)pathLength); // should be unicode safe
|
||||
var link = new string(linkTarget);
|
||||
|
||||
int begin = link.IndexOf("\0\0");
|
||||
if (begin > -1)
|
||||
{
|
||||
int end = link.IndexOf("\\\\", begin + 2) + 2;
|
||||
end = link.IndexOf('\0', end) + 1;
|
||||
|
||||
string firstPart = link.Substring(0, begin);
|
||||
string secondPart = link.Substring(end);
|
||||
|
||||
return firstPart + secondPart;
|
||||
}
|
||||
else
|
||||
{
|
||||
return link;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
System.Console.WriteLine("Please try again with a file path");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine("LNK File: " + args[0]);
|
||||
System.Console.WriteLine("LNK Path: " + Functions.GetShortcutTarget(args[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user