交互优化, 断网更新弹框删除,图标显示优化,待办任务bug修复

This commit is contained in:
liufei
2021-07-27 16:53:28 +08:00
parent 15a214aad0
commit 94f32fdb15
19 changed files with 916 additions and 253 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
@@ -9,6 +9,18 @@
<assemblyIdentity name="CommonServiceLocator" publicKeyToken="489b6accfaf20ef0" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.6.0" newVersion="2.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>

View File

@@ -8,7 +8,9 @@ namespace GeekDesk.Constant
WINDOW_WIDTH = 666, //默认窗体宽度
WINDOW_HEIGHT = 500, //默认窗体高度
MENU_CARD_WIDHT = 165, //默认菜单栏宽度
IMAGE_WIDTH = 60, //默认图标宽度
IMAGE_HEIGHT = 60, //默认图标高度
IMAGE_WIDTH = 50, //默认图标宽度
IMAGE_HEIGHT = 50, //默认图标高度
IMAGE_WIDTH_AM = 60, //动画变换宽度
IMAGE_HEIGHT_AM = 60 //动画变换高度
}
}

View File

@@ -43,9 +43,9 @@
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
<Trigger Property="IsMouseOver" Value="True">
<!--<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFE4DBDB"/>
</Trigger>
</Trigger>-->
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="#FFECECEC"/>
<Setter Property="Foreground" Value="Black"/>
@@ -84,7 +84,7 @@
SelectionChanged="menus_SelectionChanged"
>
<ListBox.Resources>
<ContextMenu x:Key="menuDialog" Width="200">
<ContextMenu x:Key="MenuDialog" Width="200">
<MenuItem Header="新建菜单" Click="CreateMenu"/>
<MenuItem Header="重命名" Click="RenameMenu" Tag="{Binding}"/>
<MenuItem Header="修改图标" Click="EditMenuGeometry" Tag="{Binding}"/>
@@ -94,7 +94,7 @@
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource MenuStyle}">
<Setter Property="ContextMenu" Value="{StaticResource menuDialog}"/>
<Setter Property="ContextMenu" Value="{StaticResource MenuDialog}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Background>
@@ -110,9 +110,6 @@
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel MouseLeftButtonDown="MenuClick" MouseRightButtonDown="MenuClick" Tag="{Binding}">
<StackPanel.Background>
<SolidColorBrush Color="AliceBlue" Opacity="0.01"/>
</StackPanel.Background>
<hc:TextBox Text="{Binding Path=MenuName, Mode=TwoWay}"
HorizontalAlignment="Left"
Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox},AncestorLevel=1},Path=Tag, Mode=TwoWay, Converter={StaticResource MenuWidthConvert}}"

View File

@@ -11,33 +11,21 @@
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<!--右侧栏样式动画-->
<Style x:Key="imageStyle" TargetType="Image">
<Style x:Key="ImageStyle" TargetType="Image">
<Setter Property="Width" Value="{Binding ImageWidth}"/>
<Setter Property="Height" Value="{Binding ImageHeight}"/>
<Setter Property="Source" Value="{Binding BitmapImage}"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="80" Duration="0:0:0.001" Storyboard.TargetProperty="Width"/>
<DoubleAnimation To="80" Duration="0:0:0.001" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="60" Duration="0:0:0.5" Storyboard.TargetProperty="Width"/>
<DoubleAnimation To="60" Duration="0:0:0.5" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
</Style.Triggers>
</Style>
<Style x:Key="MyListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<cvt:MenuWidthConvert x:Key="MenuWidthConvert"/>
@@ -47,7 +35,6 @@
<!--右侧栏-->
<hc:Card AllowDrop="True"
Drop="Wrap_Drop"
BorderThickness="1"
Effect="{DynamicResource EffectShadow2}"
Margin="5,0,5,5" Grid.ColumnSpan="2">
@@ -58,49 +45,56 @@
<SolidColorBrush Color="#FFFFFFFF" Opacity="0"/>
</hc:Card.BorderBrush>
<WrapPanel Orientation="Horizontal">
<ListBox x:Name="icons" ItemsSource="{Binding AppConfig.SelectedMenuIcons, Mode=TwoWay}"
<ListBox x:Name="IconListBox" ItemsSource="{Binding AppConfig.SelectedMenuIcons, Mode=TwoWay}"
BorderThickness="0"
SelectionChanged="IconSelectionChanged "
VirtualizingPanel.VirtualizationMode="Recycling"
>
<ListBox.Background>
<SolidColorBrush Opacity="0"/>
</ListBox.Background>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<DraggAnimatedPanel:DraggAnimatedPanel ItemsHeight="115" ItemsWidth="100" HorizontalAlignment="Center" SwapCommand="{Binding SwapCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
<DraggAnimatedPanel:DraggAnimatedPanel ItemsHeight="95"
ItemsWidth="95"
Background="#00FFFFFF"
HorizontalAlignment="Center"
SwapCommand="{Binding SwapCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<ContextMenu x:Key="iconDialog" Width="200">
<ContextMenu x:Key="IconDialog" Width="200">
<MenuItem Header="管理员方式运行" Click="IconAdminStart" Tag="{Binding}"/>
<MenuItem Header="打开文件所在位置" Click="ShowInExplore" Tag="{Binding}"/>
<MenuItem Header="资源管理器菜单" Click="MenuItem_Click" Tag="{Binding}"/>
<!--<MenuItem Header="资源管理器菜单" Click="MenuItem_Click" Tag="{Binding}"/>-->
<MenuItem Header="属性" Click="PropertyConfig" Tag="{Binding}"/>
<MenuItem Header="从列表移除" Click="RemoveIcon" Tag="{Binding}"/>
</ContextMenu>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource iconDialog}"/>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource MyListBoxItemStyle}">
<Setter Property="ContextMenu" Value="{StaticResource IconDialog}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Tag="{Binding}"
MouseLeftButtonUp="IconClick"
Height="95"
Width="95"
HorizontalAlignment="Center"
Margin="5,5,5,5"
hc:Poptip.HitMode="None"
hc:Poptip.IsOpen="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}"
hc:Poptip.Content="{Binding Content}"
hc:Poptip.Placement="BottomLeft"
Background="#00FFFFFF"
MouseLeftButtonDown="IconClick"
MouseEnter="StackPanel_MouseEnter"
MouseLeave="StackPanel_MouseLeave"
>
<Image Style="{StaticResource imageStyle}" />
<Image Style="{StaticResource ImageStyle}"/>
<TextBlock MaxWidth="80"
Margin="0,5,0,0"
MaxHeight="40"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"

View File

@@ -18,6 +18,7 @@ using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
@@ -154,15 +155,6 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
/// <summary>
/// data选中事件 设置不可选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void IconSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (icons.SelectedIndex != -1) icons.SelectedIndex = -1;
}
private void Wrap_Drop(object sender, DragEventArgs e)
@@ -175,6 +167,17 @@ namespace GeekDesk.Control.UserControls.PannelCard
//string base64 = ImageUtil.FileImageToBase64(path, ImageFormat.Jpeg);
string ext = System.IO.Path.GetExtension(path).ToLower();
if (".lnk".Equals(ext))
{
string targetPath = FileUtil.GetTargetPathByLnk(path);
if (targetPath!=null)
{
path = targetPath;
}
}
IconInfo iconInfo = new IconInfo
{
Path = path,
@@ -182,6 +185,10 @@ namespace GeekDesk.Control.UserControls.PannelCard
};
iconInfo.DefaultImage = iconInfo.ImageByteArr;
iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(path);
if (StringUtil.IsEmpty(iconInfo.Name))
{
iconInfo.Name = path;
}
MainWindow.appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Add(iconInfo);
}
}
@@ -214,5 +221,40 @@ namespace GeekDesk.Control.UserControls.PannelCard
{
HandyControl.Controls.Dialog.Show(new IconInfoDialog((IconInfo)((MenuItem)sender).Tag));
}
private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
{
ImgStroyBoard(sender, (int)MainWindowEnum.IMAGE_HEIGHT_AM, (int)MainWindowEnum.IMAGE_WIDTH_AM, 1);
}
private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
{
ImgStroyBoard(sender, (int)MainWindowEnum.IMAGE_HEIGHT, (int)MainWindowEnum.IMAGE_WIDTH, 500);
}
private void ImgStroyBoard(object sender, int height, int width, int milliseconds)
{
StackPanel sp = sender as StackPanel;
Image img = sp.Children[0] as Image;
DoubleAnimation heightAnimation = new DoubleAnimation();
DoubleAnimation widthAnimation = new DoubleAnimation();
heightAnimation.From = img.Height;
widthAnimation.From = img.Width;
heightAnimation.To = height;
widthAnimation.To = width;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds));
widthAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds));
img.BeginAnimation(HeightProperty, heightAnimation);
img.BeginAnimation(WidthProperty, widthAnimation);
}
}
}

View File

@@ -79,8 +79,6 @@ namespace GeekDesk.Control.Windows
/// <param name="e"></param>
private void Save_Button_Click(object sender, RoutedEventArgs e)
{
if (Title.Text.Trim() == "" || ExeTime.Text.Trim() == "")
{
Growl.Warning("任务标题 和 待办时间不能为空!");
@@ -107,13 +105,12 @@ namespace GeekDesk.Control.Windows
appData.ToDoList.Add(info);
} else
{
int index =appData.ToDoList.IndexOf(info);
appData.ToDoList.Remove(info);
appData.HiToDoList.Remove(info);
info.Title = Title.Text;
info.Msg = Msg.Text;
info.ExeTime = ExeTime.Text;
info.DoneTime = DoneTime.Text;
appData.ToDoList.Insert(index, info);
appData.ToDoList.Add(info);
}
CommonCode.SaveAppData(MainWindow.appData);
this.Close();

View File

@@ -66,8 +66,8 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Drawing.Common, Version=4.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Drawing.Common.6.0.0-preview.3.21201.4\lib\net461\System.Drawing.Common.dll</HintPath>
<Reference Include="System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Drawing.Common.6.0.0-preview.6.21352.12\lib\net461\System.Drawing.Common.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
@@ -161,6 +161,7 @@
<Compile Include="Util\ConsoleManager.cs" />
<Compile Include="Util\DragAdorner.cs" />
<Compile Include="Util\FileIcon.cs" />
<Compile Include="Util\FileUtil.cs" />
<Compile Include="Util\HotKey.cs" />
<Compile Include="Util\HttpUtil.cs" />
<Compile Include="Util\ImageUtil.cs" />
@@ -171,7 +172,6 @@
<Compile Include="Util\RegisterUtil.cs" />
<Compile Include="Util\StringUtil.cs" />
<Compile Include="Util\SvgToGeometry.cs" />
<Compile Include="Util\SystemIcon.cs" />
<Compile Include="ViewModel\AppConfig.cs" />
<Compile Include="ViewModel\AppData.cs" />
<Compile Include="ViewModel\ToDoInfo.cs" />
@@ -312,5 +312,16 @@
<ItemGroup>
<Resource Include="Taskbar.ico" />
</ItemGroup>
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -57,7 +57,8 @@ namespace GeekDesk.Thread
}
} catch (Exception e)
{
MessageBox.Show(e.Message);
//不做处理
//MessageBox.Show(e.Message);
}
}
}

52
Util/DefaultIcons.cs Normal file
View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace GeekDesk.Util
{
public static class DefaultIcons
{
private static Icon folderIcon;
public static Icon FolderLarge => folderIcon ?? (folderIcon = GetStockIcon(SHGSI_ICON, SHGSI_LARGEICON));
public static Icon GetStockIcon(uint type, uint size)
{
var info = new SHSTOCKICONINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
SHGetStockIconInfo(type, SHGSI_ICON | size, ref info);
var icon = (Icon)Icon.FromHandle(info.hIcon).Clone(); // Get a copy that doesn't use the original handle
DestroyIcon(info.hIcon); // Clean up native icon to prevent resource leak
return icon;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHSTOCKICONINFO
{
public uint cbSize;
public IntPtr hIcon;
public int iSysIconIndex;
public int iIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szPath;
}
[DllImport("shell32.dll")]
public static extern int SHGetStockIconInfo(uint siid, uint uFlags, ref SHSTOCKICONINFO psii);
[DllImport("user32.dll")]
public static extern bool DestroyIcon(IntPtr handle);
public static uint SHSIID_FOLDER = 0x3;
public static uint SHGSI_ICON = 0x100;
public static uint SHGSI_LARGEICON = 0x0;
public static uint SHGSI_SMALLICON = 0x1;
}
}

View File

@@ -1,4 +1,6 @@
using System;
using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
@@ -6,20 +8,61 @@ using System.Windows.Media.Imaging;
namespace GeekDesk.Util
{
class FileIcon
public class FileIcon
{
public static Icon GetIcon(string filePath)
private static List<string> GetBlurExts()
{
IntPtr hIcon = GetJumboIcon(GetIconIndex(filePath));
Icon ico = Icon.FromHandle(hIcon);
return ico;
List<string> list = new List<string>();
list.Add(".exe");
list.Add(".cer");
list.Add(".lnk");
return list;
}
[DllImport("User32.dll")]
public static extern int PrivateExtractIcons(
string lpszFile, //文件名可以是exe,dll,ico,cur,ani,bmp
int nIconIndex, //从第几个图标开始获取
int cxIcon, //获取图标的尺寸x
int cyIcon, //获取图标的尺寸y
IntPtr[] phicon, //获取到的图标指针数组
int[] piconid, //图标对应的资源编号
int nIcons, //指定获取的图标数量,仅当文件类型为.exe 和 .dll时候可用
int flags //标志默认0就可以具体可以看LoadImage函数
);
public static BitmapImage GetBitmapImage(string filePath)
{
Icon ico = GetIcon(filePath);
Icon ico;
//选中文件中的图标总数
var iconTotalCount = PrivateExtractIcons(filePath, 0, 0, 0, null, null, 0, 0);
//用于接收获取到的图标指针
IntPtr[] hIcons = new IntPtr[iconTotalCount];
//对应的图标id
int[] ids = new int[iconTotalCount];
//成功获取到的图标个数
var successCount = PrivateExtractIcons(filePath, 0, 256, 256, hIcons, ids, iconTotalCount, 0);
string ext = Path.GetExtension(filePath).ToLower();
IntPtr ip = IntPtr.Zero;
if (successCount > 0)
{
ip = hIcons[0];
ico = Icon.FromHandle(ip);
}
else if (GetBlurExts().Contains(ext))
{
ico = Icon.ExtractAssociatedIcon(filePath);
}
else
{
ip = GetJumboIcon(GetIconIndex(filePath));
ico = Icon.FromHandle(ip);
}
Bitmap bmp = ico.ToBitmap();
MemoryStream strm = new MemoryStream();
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
@@ -28,10 +71,15 @@ namespace GeekDesk.Util
strm.Seek(0, SeekOrigin.Begin);
bmpImage.StreamSource = strm;
bmpImage.EndInit();
if (ip != IntPtr.Zero)
{
Shell32.DestroyIcon(ip);
}
return bmpImage.Clone();
}
public static int GetIconIndex(string pszFile)
{
SHFILEINFO sfi = new SHFILEINFO();

32
Util/FileUtil.cs Normal file
View File

@@ -0,0 +1,32 @@
using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeekDesk.Util
{
public class FileUtil
{
public static string GetTargetPathByLnk(string filePath)
{
try
{
WshShell shell = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
if (StringUtil.IsEmpty(shortcut.TargetPath))
{
return null;
}
return shortcut.TargetPath;
}
catch (Exception e)
{
return null;
}
}
}
}

95
Util/IconHelper.cs Normal file
View File

@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace GeekDesk.Util
{
public class IconHelper
{
[DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo
(
string pszPath, //һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȡ<D2AA><C8A1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><E5A1A3><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ע[1]
uint dwFileAttributes,//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>uFlags<67>а<EFBFBD><D0B0><EFBFBD>SHGFI_USEFILEATTRIBUTES<45><53>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(һ<>ʹ<E3B2BB><CAB9>)<29><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><E6B5B5>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC>ϵͳ<CFB5>ȡ<EFBFBD>
out SHFILEINFO psfi,
uint cbfileInfo,//<2F>򵥵ظ<F2B5A5B5><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijߴ
SHGFI uFlags//<2F><><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܵı<DCB5>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD>Ԧ<EFBFBD><D4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʵ<EFBFBD>ʵصõ<D8B5><C3B5><EFBFBD>Ϣ<EFBFBD><CFA2>
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;//ͼ<><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public int iIcon;//ϵͳͼ<CDB3><CDBC><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public uint dwAttributes; //<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;//<2F>ļ<EFBFBD><C4BC><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD> <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>256<35><36>ANSI<53><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̷<EFBFBD><CCB7><EFBFBD>X:\<5C><>3<EFBFBD>ֽڣ<D6BD>259<35>ֽڣ<D6BD><DAA3>ټ<EFBFBD><D9BC>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD>260
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>̶<EFBFBD>80<38>ֽ<EFBFBD>
};
private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,//Retrieve the display name for the file, which is the name as it appears in Windows Explorer. The name is copied to the szDisplayName member of the structure specified in psfi. The returned display name uses the long file name, if there is one, rather than the 8.3 form of the file name. Note that the display name can be affected by settings such as whether extensions are shown.
Typename = 0x00000400, //Retrieve the string that describes the file's type. The string is copied to the szTypeName member of the structure specified in psfi.
SysIconIndex = 0x00004000, //Retrieve the index of a system image list icon. If successful, the index is copied to the iIcon member of psfi. The return value is a handle to the system image list. Only those images whose indices are successfully copied to iIcon are valid. Attempting to access other images in the system image list will result in undefined behavior.
UseFileAttributes = 0x00000010 //Indicates that the function should not attempt to access the file specified by pszPath. Rather, it should act as if the file specified by pszPath exists with the file attributes passed in dwFileAttributes. This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags.
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>չ<EFBFBD><D5B9><EFBFBD>õ<EFBFBD>ϵͳ<CFB5><CDB3>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
/// </summary>
/// <param name="fileName"><3E>ļ<EFBFBD><C4BC><EFBFBD>(<28>磺win.rar;setup.exe;temp.txt)</param>
/// <param name="largeIcon">ͼ<><CDBC><EFBFBD>Ĵ<EFBFBD>С</param>
/// <returns></returns>
public static Icon GetFileIcon(string fileName, bool largeIcon)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
IntPtr IconIntPtr = SHGetFileInfo(fileName, 256, out info, (uint)cbFileInfo, flags);
if (IconIntPtr.Equals(IntPtr.Zero))
return null;
return Icon.FromHandle(info.hIcon);
}
/// <summary>
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ͼ<EFBFBD><CDBC>
/// </summary>
/// <returns>ͼ<><CDBC></returns>
public static Icon GetDirectoryIcon(string path, bool largeIcon)
{
SHFILEINFO _SHFILEINFO = new SHFILEINFO();
int cbFileInfo = Marshal.SizeOf(_SHFILEINFO);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon;
else
flags = SHGFI.Icon | SHGFI.SmallIcon;
IntPtr IconIntPtr = SHGetFileInfo(path, 256, out _SHFILEINFO, (uint)cbFileInfo, flags);
if (IconIntPtr.Equals(IntPtr.Zero))
return null;
Icon _Icon = Icon.FromHandle(_SHFILEINFO.hIcon);
return _Icon;
}
}
}

17
Util/IconUtil.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GeekDesk.Util
{
public class IconUtil
{
const string IID_IImageList = "46EB5926-582E-4017-9FDF-E8998DAA0950";
const string IID_IImageList2 = "192B9D83-50FC-457B-90A0-2B82A8B5DAE1";
}
}

View File

@@ -268,21 +268,13 @@ namespace GeekDesk.Util
public static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapImage retval;
try
{
retval = (BitmapImage)Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap);
}
return retval;
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bit3 = new BitmapImage();
bit3.BeginInit();
bit3.StreamSource = ms;
bit3.EndInit();
return bit3;
}

72
Util/NativeMethods.cs Normal file
View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GeekDesk.Util
{
/// <summary>
/// 保存文件信息的结构体
/// </summary>
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SHFILEINFO
{
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;
}
class NativeMethods
{
[DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
#region API
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; //大图标 32×32
public const uint SHGFI_SMALLICON = 0x1; //小图标 16×16
public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
#endregion
/// <summary>
/// 获取文件类型的关联图标
/// </summary>
/// <param name="fileName">文件类型的扩展名或文件的绝对路径</param>
/// <param name="isLargeIcon">是否返回大图标</param>
/// <returns>获取到的图标</returns>
static Icon GetIcon(string fileName, bool isLargeIcon)
{
SHFILEINFO shfi = new SHFILEINFO();
IntPtr hI;
if (isLargeIcon)
hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_LARGEICON);
else
hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SMALLICON);
Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
NativeMethods.DestroyIcon(shfi.hIcon); //释放资源
return icon;
}
}
}

View File

@@ -1,145 +0,0 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace GeekDesk.Util
{
class SystemIcon
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEINFO
{
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;
}
[DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
#region API
public enum FileInfoFlags : uint
{
SHGFI_ICON = 0x000000100, // get icon
SHGFI_DISPLAYNAME = 0x000000200, // get display name
SHGFI_TYPENAME = 0x000000400, // get type name
SHGFI_ATTRIBUTES = 0x000000800, // get attributes
SHGFI_ICONLOCATION = 0x000001000, // get icon location
SHGFI_EXETYPE = 0x000002000, // return exe type
SHGFI_SYSICONINDEX = 0x000004000, // get system icon index
SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon
SHGFI_SELECTED = 0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes
SHGFI_LARGEICON = 0x000000000, // get large icon
SHGFI_SMALLICON = 0x000000001, // get small icon
SHGFI_OPENICON = 0x000000002, // get open icon
SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon
SHGFI_PIDL = 0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x000000010, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
}
public enum FileAttributeFlags : uint
{
FILE_ATTRIBUTE_READONLY = 0x00000001,
FILE_ATTRIBUTE_HIDDEN = 0x00000002,
FILE_ATTRIBUTE_SYSTEM = 0x00000004,
FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
FILE_ATTRIBUTE_DEVICE = 0x00000040,
FILE_ATTRIBUTE_NORMAL = 0x00000080,
FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
FILE_ATTRIBUTE_OFFLINE = 0x00001000,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
FILE_ATTRIBUTE_ENCRYPTED = 0x00004000
}
#endregion
/// <summary>
/// 获取文件类型的关联图标
/// </summary>
/// <param name="fileName">文件类型的扩展名或文件的绝对路径</param>
/// <param name="isLargeIcon">是否返回大图标</param>
/// <returns>获取到的图标</returns>
public static Icon GetIcon(string fileName, bool isLargeIcon)
{
//SHFILEINFO shfi = new SHFILEINFO();
//IntPtr hI;
//if (isLargeIcon)
// hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_LARGEICON);
//else
// hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_SMALLICON);
//Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
//DestroyIcon(shfi.hIcon); //释放资源
//选中文件中的图标总数
//var iconTotalCount = PrivateExtractIcons(fileName, 0, 0, 0, null, null, 1, 0);
//用于接收获取到的图标指针
//IntPtr[] hIcons = new IntPtr[1];
////对应的图标id
//int[] ids = new int[1];
////成功获取到的图标个数
//int successCount = PrivateExtractIcons(fileName, 0, 0, 0, hIcons, ids, 1, 0);
//Icon ico = Icon.FromHandle(hIcons[0]);
//var myIcon = ico.ToBitmap();
//myIcon.Save("D:\\" + ids[0].ToString("000") + ".png", ImageFormat.Png);
IntPtr hIcon = FileIcon.GetJumboIcon(FileIcon.GetIconIndex(fileName));
//IntPtr hIcon = GetJumboIcon(GetIconIndex("*." + ext));
// from native to managed
Icon ico = Icon.FromHandle(hIcon);
string path = "D:\\test\\" + System.Guid.NewGuid().ToString() + ".png";
//using ( ico = (Icon)Icon.FromHandle(hIcon).Clone())
//{
// // save to file (or show in a picture box)
// ico.ToBitmap().Save(path, ImageFormat.Png);
//}
//FileIcon.Shell32.DestroyIcon(hIcon); // don't forget to cleanup
return ico;
}
/// <summary>
/// 获取文件夹图标
/// </summary>
/// <returns>图标</returns>
public static Icon GetDirectoryIcon(bool isLargeIcon)
{
SHFILEINFO _SHFILEINFO = new SHFILEINFO();
IntPtr _IconIntPtr;
if (isLargeIcon)
{
_IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_LARGEICON));
}
else
{
_IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_SMALLICON));
}
if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
return _Icon;
}
[DllImport("User32.dll")]
public static extern int PrivateExtractIcons(
string lpszFile, //file name
int nIconIndex, //The zero-based index of the first icon to extract.
int cxIcon, //The horizontal icon size wanted.
int cyIcon, //The vertical icon size wanted.
IntPtr[] phicon, //(out) A pointer to the returned array of icon handles.
int[] piconid, //(out) A pointer to a returned resource identifier.
int nIcons, //The number of icons to extract from the file. Only valid when *.exe and *.dll
int flags //Specifies flags that control this function.
);
//[DllImport("User32.dll")]
//public static extern bool DestroyIcon(
// IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use.
// );
}
}

View File

@@ -0,0 +1,442 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Diagnostics;
using System.Drawing;
namespace GeekDesk.Util
{
public class WindowsThumbnailProvider
{
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private struct POINT
{
public int x;
public int y;
}
// Constants that we need in the function call
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
private const int SHIL_JUMBO = 0x4;
private const int SHIL_EXTRALARGE = 0x2;
// This structure will contain information about the file
public struct SHFILEINFO
{
// Handle to the icon representing the file
public IntPtr hIcon;
// Index of the icon within the image list
public int iIcon;
// Various attributes of the file
public uint dwAttributes;
// Path to the file
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szDisplayName;
// File type
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern Boolean CloseHandle(IntPtr handle);
private 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)]
private struct IMAGEINFO
{
public IntPtr hbmImage;
public IntPtr hbmMask;
public int Unused1;
public int Unused2;
public RECT rcImage;
}
#region Private ImageList COM Interop (XP)
[ComImportAttribute()]
[GuidAttribute("46EB5926-582E-4017-9FDF-E8998DAA0950")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
//helpstring("Image List"),
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);
};
#endregion
///
/// SHGetImageList is not exported correctly in XP. See KB316931
/// http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316931
/// Apparently (and hopefully) ordinal 727 isn't going to change.
///
[DllImport("shell32.dll", EntryPoint = "#727")]
private extern static int SHGetImageList(
int iImageList,
ref Guid riid,
out IImageList ppv
);
// The signature of SHGetFileInfo (located in Shell32.dll)
[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(IntPtr pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
[DllImport("shell32.dll", SetLastError = true)]
static extern int SHGetSpecialFolderLocation(IntPtr hwndOwner, Int32 nFolder,
ref IntPtr ppidl);
[DllImport("user32")]
public static extern int DestroyIcon(IntPtr hIcon);
public struct pair
{
public System.Drawing.Icon icon { get; set; }
public IntPtr iconHandleToDestroy { set; get; }
}
public static int DestroyIcon2(IntPtr hIcon)
{
return DestroyIcon(hIcon);
}
private static BitmapSource bitmap_source_of_icon(System.Drawing.Icon ic)
{
var ic2 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(ic.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
ic2.Freeze();
return ((BitmapSource)ic2);
}
//public static BitmapSource SystemIcon(bool small, ShellLib.ShellApi.CSIDL csidl)
//{
// IntPtr pidlTrash = IntPtr.Zero;
// int hr = SHGetSpecialFolderLocation(IntPtr.Zero, (int)csidl, ref pidlTrash);
// Debug.Assert(hr == 0);
// SHFILEINFO shinfo = new SHFILEINFO();
// uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
// // Get a handle to the large icon
// uint flags;
// uint SHGFI_PIDL = 0x000000008;
// if (!small)
// {
// flags = SHGFI_PIDL | SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES;
// }
// else
// {
// flags = SHGFI_PIDL | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES;
// }
// var res = SHGetFileInfo(pidlTrash, 0, ref shinfo, Marshal.SizeOf(shinfo), flags);
// Debug.Assert(res != 0);
// var myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
// Marshal.FreeCoTaskMem(pidlTrash);
// var bs = bitmap_source_of_icon(myIcon);
// myIcon.Dispose();
// bs.Freeze(); // importantissimo se no fa memory leak
// DestroyIcon(shinfo.hIcon);
// CloseHandle(shinfo.hIcon);
// return bs;
//}
public static BitmapSource icon_of_path(string FileName, bool small, bool checkDisk, bool addOverlay)
{
SHFILEINFO shinfo = new SHFILEINFO();
uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
uint SHGFI_LINKOVERLAY = 0x000008000;
uint flags;
if (small)
{
flags = SHGFI_ICON | SHGFI_SMALLICON;
}
else
{
flags = SHGFI_ICON | SHGFI_LARGEICON;
}
if (!checkDisk)
flags |= SHGFI_USEFILEATTRIBUTES;
if (addOverlay)
flags |= SHGFI_LINKOVERLAY;
var res = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), flags);
if (res == 0)
{
throw (new System.IO.FileNotFoundException());
}
var myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
var bs = bitmap_source_of_icon(myIcon);
myIcon.Dispose();
bs.Freeze(); // importantissimo se no fa memory leak
DestroyIcon(shinfo.hIcon);
CloseHandle(shinfo.hIcon);
return bs;
}
public static Icon icon_of_path_large(string FileName, bool jumbo, bool checkDisk)
{
SHFILEINFO shinfo = new SHFILEINFO();
uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
uint SHGFI_SYSICONINDEX = 0x4000;
int FILE_ATTRIBUTE_NORMAL = 0x80;
uint flags;
flags = SHGFI_SYSICONINDEX;
if (!checkDisk) // This does not seem to work. If I try it, a folder icon is always returned.
flags |= SHGFI_USEFILEATTRIBUTES;
var res = SHGetFileInfo(FileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), flags);
if (res == 0)
{
throw (new System.IO.FileNotFoundException());
}
var iconIndex = shinfo.iIcon;
// Get the System IImageList object from the Shell:
Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
IImageList iml;
int size = jumbo ? SHIL_JUMBO : SHIL_EXTRALARGE;
var hres = SHGetImageList(size, ref iidImageList, out iml); // writes iml
//if (hres == 0)
//{
// throw (new System.Exception("Error SHGetImageList"));
//}
IntPtr hIcon = IntPtr.Zero;
int ILD_TRANSPARENT = 1;
hres = iml.GetIcon(iconIndex, ILD_TRANSPARENT, ref hIcon);
//if (hres == 0)
//{
// throw (new System.Exception("Error iml.GetIcon"));
//}
var myIcon = System.Drawing.Icon.FromHandle(hIcon);
var bs = bitmap_source_of_icon(myIcon);
myIcon.Dispose();
bs.Freeze(); // very important to avoid memory leak
//DestroyIcon(hIcon);
//CloseHandle(hIcon);
return myIcon;
}
}
}

View File

@@ -141,7 +141,8 @@ namespace GeekDesk.ViewModel
{
get
{
return imageWidth;
// 为了兼容旧版 暂时使用默认
return (int)MainWindowEnum.IMAGE_WIDTH;
}
set
{
@@ -154,7 +155,8 @@ namespace GeekDesk.ViewModel
{
get
{
return imageHeight;
// 为了兼容旧版 暂时使用默认
return (int)MainWindowEnum.IMAGE_HEIGHT;
}
set
{

View File

@@ -3,5 +3,5 @@
<package id="CommonServiceLocator" version="2.0.6" targetFramework="net452" requireReinstallation="true" />
<package id="HandyControl" version="3.1.0" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="System.Drawing.Common" version="6.0.0-preview.3.21201.4" targetFramework="net472" />
<package id="System.Drawing.Common" version="6.0.0-preview.6.21352.12" targetFramework="net472" />
</packages>