Przeglądaj źródła

优化程序 增加功能

liufei 4 lat temu
rodzic
commit
de31210f37

Plik diff jest za duży
+ 14 - 0
Constant/Constants.cs


+ 3 - 3
Constant/DefaultConstant.cs

@@ -5,9 +5,9 @@ namespace GeekDesk.Constant
 {
     enum DefaultConstant
     {
-        WINDOW_WIDTH = 650, //默认窗体宽度
-        WINDOW_HEIGHT = 700, //默认窗体高度
-        MENU_CARD_WIDHT = 150, //默认菜单栏宽度
+        WINDOW_WIDTH = 666, //默认窗体宽度
+        WINDOW_HEIGHT = 500, //默认窗体高度
+        MENU_CARD_WIDHT = 165, //默认菜单栏宽度
         IMAGE_WIDTH = 60, //默认图标宽度
         IMAGE_HEIGHT = 60, //默认图标高度
     }

+ 18 - 0
Constant/IconStartType.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+/// <summary>
+/// 图标应用启动方式
+/// </summary>
+namespace GeekDesk.Constant
+{
+    enum IconStartType
+    {
+        DEFAULT_STARTUP = 1, //默认启动方式
+        ADMIN_STARTUP = 2,  //管理员方式启动
+        SHOW_IN_EXPLORE = 3 //打开文件所在位置
+    }
+}

+ 56 - 0
Control/IconInfoDialog.xaml

@@ -0,0 +1,56 @@
+<Border x:Class="GeekDesk.Control.IconInfoDialog"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:hc="https://handyorg.github.io/handycontrol"
+        CornerRadius="10"
+        Width="300"
+        Height="300"
+        Opacity="0.9">
+    <Border.Resources>
+        <Style x:Key="LeftTB" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBaseStyle}">
+            <Setter Property="Width" Value="40"/>
+            <Setter Property="HorizontalAlignment" Value="Left"/>
+            <Setter Property="VerticalAlignment" Value="Center"/>
+            <Setter Property="FontSize" Value="14"/>
+        </Style>
+    </Border.Resources>
+    <Border.Background>
+        <SolidColorBrush Color="AliceBlue" Opacity="0.9"/>
+    </Border.Background>
+    <hc:SimplePanel Margin="10">
+        <Grid Margin="8,20,8,20">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="93*"/>
+                <ColumnDefinition Width="38*"/>
+                <ColumnDefinition Width="126*"/>
+                <ColumnDefinition Width="7*"/>
+            </Grid.ColumnDefinitions>
+            <hc:UniformSpacingPanel Spacing="10" Grid.ColumnSpan="4">
+                <TextBlock Text="名称:"  Style="{StaticResource LeftTB}"/>
+                <TextBox x:Name="IconName" Text="{Binding Name, Mode=OneWay}" Width="180" FontSize="14"/>
+            </hc:UniformSpacingPanel>
+            <hc:Divider LineStrokeDashArray="3,3" Margin="0,50,0,71" LineStroke="Black" Grid.ColumnSpan="4"/>
+            <hc:UniformSpacingPanel Spacing="10" Margin="0,59,0,-9" Grid.ColumnSpan="4">
+                <TextBlock  Text="图标:"  Style="{StaticResource LeftTB}"/>
+                <Image x:Name="IconImg" Source="{Binding BitmapImage, Mode=OneWay}" Width="60" Height="60"/>
+                <Button Content="修改" Click="EditImage"/>
+                <Button Content="默认" Click="EditImageToDefault"/>
+            </hc:UniformSpacingPanel>
+            <hc:Divider LineStrokeDashArray="3,3" Margin="0,128,0,23" LineStroke="Black" Grid.ColumnSpan="4"/>
+            <hc:UniformSpacingPanel Spacing="10" Margin="0,150,0,10" Grid.ColumnSpan="4">
+                <CheckBox x:Name="IconIsAdmin" Content="始终以管理员方式启动" IsChecked="{Binding AdminStartUp, Mode=OneWay}">
+                    <CheckBox.Background>
+                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
+                            <GradientStop Color="#FF9EA3A6"/>
+                        </LinearGradientBrush>
+                    </CheckBox.Background>
+                </CheckBox>
+            </hc:UniformSpacingPanel>
+            <hc:Divider LineStrokeDashArray="3,3" Margin="0,200,0,-27" LineStroke="Black" Grid.ColumnSpan="4"/>
+            <hc:UniformSpacingPanel Spacing="10" Margin="0,218,0,-38" Grid.ColumnSpan="4">
+                <Button Content="保存" Command="hc:ControlCommands.Close" Click="SaveProperty" Margin="208,6,-208,-10"/>
+            </hc:UniformSpacingPanel>
+        </Grid>
+        <Button Width="22" Height="22" Command="hc:ControlCommands.Close" Style="{StaticResource ButtonIcon}" Foreground="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" hc:IconElement.Geometry="{StaticResource ErrorGeometry}" Padding="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,4,4,0"/>
+    </hc:SimplePanel>
+</Border>

+ 74 - 0
Control/IconInfoDialog.xaml.cs

@@ -0,0 +1,74 @@
+using GeekDesk.Util;
+using GeekDesk.ViewModel;
+using Microsoft.Win32;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media.Imaging;
+
+
+namespace GeekDesk.Control
+{
+    /// <summary>
+    /// TextDialog.xaml 的交互逻辑
+    /// </summary>
+    public partial class IconInfoDialog
+    {
+
+        public IconInfoDialog()
+        {
+            InitializeComponent();
+        }
+
+        public IconInfoDialog(IconInfo info)
+        {
+            this.DataContext = info;
+            InitializeComponent();
+        }
+
+        /// <summary>
+        /// 保存修改属性
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void SaveProperty(object sender, RoutedEventArgs e)
+        {
+            IconInfo info = this.DataContext as IconInfo;
+            info.BitmapImage = IconImg.Source as BitmapImage;
+            info.Name = IconName.Text;
+            info.AdminStartUp = IconIsAdmin.IsChecked.Value;
+            CommonCode.SaveAppData(MainWindow.appData);
+        }
+
+        /// <summary>
+        /// 修改图标为默认
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void EditImageToDefault(object sender, RoutedEventArgs e)
+        {
+            IconInfo info = ((Button)sender).Tag as IconInfo;
+            info.BitmapImage = ImageUtil.ByteArrToImage(info.DefaultImage);
+            CommonCode.SaveAppData(MainWindow.appData);
+        }
+
+        /// <summary>
+        /// 修改图标
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void EditImage(object sender, RoutedEventArgs e)
+        {
+            OpenFileDialog ofd = new OpenFileDialog
+            {
+                Multiselect = false, //只允许选中单个文件
+                Filter = "所有文件(*.*)|*.*"
+            };
+            if (ofd.ShowDialog() == true)
+            {
+                IconInfo info = this.DataContext as IconInfo;
+                info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName);
+                CommonCode.SaveAppData(MainWindow.appData);
+            }
+        }
+    }
+}

+ 12 - 0
GeekDesk.csproj

@@ -71,8 +71,13 @@
     <Compile Include="Command\DelegateCommand.cs" />
     <Compile Include="Command\DelegateCommandBase.cs" />
     <Compile Include="Constant\AppConstant.cs" />
+    <Compile Include="Constant\Constants.cs" />
     <Compile Include="Constant\DefaultConstant.cs" />
+    <Compile Include="Constant\IconStartType.cs" />
     <Compile Include="Constant\SortType.cs" />
+    <Compile Include="Control\IconInfoDialog.xaml.cs">
+      <DependentUpon>IconInfoDialog.xaml</DependentUpon>
+    </Compile>
     <Compile Include="DraggAnimatedPanel\DraggAnimatedPanel.cs" />
     <Compile Include="DraggAnimatedPanel\DraggAnimatedPanel.Drag.cs" />
     <Compile Include="EditTextBlock\EditableTextBlock.cs" />
@@ -82,6 +87,7 @@
     <Compile Include="Util\DragAdorner.cs" />
     <Compile Include="Util\FileIcon.cs" />
     <Compile Include="Util\HotKey.cs" />
+    <Compile Include="Util\ImageUtil.cs" />
     <Compile Include="Util\ListViewDragDropManager.cs" />
     <Compile Include="Util\MenuWidthConvert.cs" />
     <Compile Include="Util\MouseUtilities.cs" />
@@ -91,6 +97,10 @@
     <Compile Include="ViewModel\AppData.cs" />
     <Compile Include="ViewModel\IconInfo.cs" />
     <Compile Include="ViewModel\MenuInfo.cs" />
+    <Page Include="Control\IconInfoDialog.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -139,6 +149,8 @@
   </ItemGroup>
   <ItemGroup>
     <Content Include="Resource\DefaultIconfont\iconfont.svg" />
+    <Resource Include="Resource\Image\图片.png" />
+    <Resource Include="Resource\Image\文件夹.png" />
     <Resource Include="Resource\Image\Ico.png" />
     <Resource Include="Resource\Image\Ico.ico" />
     <Resource Include="Resource\Image\WindowLogo.png" />

Plik diff jest za duży
+ 92 - 65
MainWindow.xaml


+ 136 - 53
MainWindow.xaml.cs

@@ -1,11 +1,11 @@
 using DraggAnimatedPanelExample;
+using GeekDesk.Constant;
+using GeekDesk.Control;
 using GeekDesk.Util;
 using GeekDesk.ViewModel;
 using System;
-using System.Collections;
-using System.Collections.Generic;
 using System.Collections.ObjectModel;
-using System.ComponentModel;
+using System.Diagnostics;
 using System.IO;
 using System.Windows;
 using System.Windows.Controls;
@@ -21,7 +21,7 @@ namespace GeekDesk
     public partial class MainWindow : Window
     {
 
-        private AppData appData = CommonCode.GetAppDataByFile();
+        public static AppData appData = CommonCode.GetAppDataByFile();
         private int menuSelectIndexTemp = -1;
         public MainWindow()
         {
@@ -40,9 +40,10 @@ namespace GeekDesk
             this.DataContext = appData;
             if (appData.MenuList.Count == 0)
             {
-                appData.MenuList.Add(new MenuInfo() { MenuName = "NewGouop", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = (int)Visibility.Collapsed});
+                appData.MenuList.Add(new MenuInfo() { MenuName = "NewMenu", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = (int)Visibility.Collapsed});
             }
             //窗体大小
+            LeftColumn.Width = new GridLength(appData.AppConfig.MenuCardWidth);
             this.Width = appData.AppConfig.WindowWidth;
             this.Height = appData.AppConfig.WindowHeight;
             //选中 菜单
@@ -64,18 +65,13 @@ namespace GeekDesk
                         {
                             int fromS = indexes[0];
                             int to = indexes[1];
-                            var elementSource = icons.Items[to];
-                            var dragged = icons.Items[fromS];
-                            if (fromS > to)
-                            {
-                                icons.Items.Remove(dragged);
-                                icons.Items.Insert(to, dragged);
-                            }
-                            else
-                            {
-                                icons.Items.Remove(dragged);
-                                icons.Items.Insert(to, dragged);
-                            }
+                            ObservableCollection<IconInfo> iconList = appData.MenuList[menus.SelectedIndex].IconList;
+                            var elementSource = iconList[to];
+                            var dragged = iconList[fromS];
+
+                            iconList.Remove(dragged);
+                            iconList.Insert(to, dragged);
+                            CommonCode.SaveAppData(appData);
                         }
                     );
                 return _swap;
@@ -96,19 +92,11 @@ namespace GeekDesk
                             ObservableCollection<MenuInfo> menuList = appData.MenuList;
                             var elementSource = menuList[to];
                             var dragged = menuList[fromS];
-                            if (fromS > to)
-                            {
-                                menuList.Remove(dragged);
-                                menuList.Insert(to, dragged);
-                            }
-                            else
-                            {
-                                menuList.Remove(dragged);
-                                menuList.Insert(to, dragged);
-                            }
+                            menuList.Remove(dragged);
+                            menuList.Insert(to, dragged);
+                            menus.SelectedIndex = to;
                             appData.MenuList = menuList;
-                            //menus.Items.Refresh();
-
+                            CommonCode.SaveAppData(appData);
                         }
                     );
                 return _swap2;
@@ -124,33 +112,27 @@ namespace GeekDesk
             foreach (object obj in dropObject)
             {
                 string path = (string)obj;
-                if (File.Exists(path))
-                {
-                    // 文件
-                    BitmapImage bi = FileIcon.GetBitmapImage(path);
-                    IconInfo iconInfo = new IconInfo();
-                    iconInfo.Path = path;
-                    iconInfo.BitmapImage = bi;
-                    iconInfo.Name = Path.GetFileNameWithoutExtension(path);
-                    appData.MenuList[menus.SelectedIndex].IconList.Add(iconInfo);
-                    CommonCode.SaveAppData(appData);
 
-                }
-                else if (Directory.Exists(path))
+                IconInfo iconInfo = new IconInfo
                 {
-                    //文件夹
-
-                }
+                    Path = path,
+                    BitmapImage = ImageUtil.GetBitmapIconByPath(path)
+                };
+                iconInfo.DefaultImage = iconInfo.ImageByteArr;
+                iconInfo.Name = Path.GetFileNameWithoutExtension(path);
+                appData.MenuList[menus.SelectedIndex].IconList.Add(iconInfo);
             }
-            icons.Items.Refresh();
+            CommonCode.SaveAppData(appData);
         }
 
-        //菜单点击事件
-        private void MenuClick(object sender, SelectionChangedEventArgs e)
+   
+        ////菜单点击事件
+        private void MenuClick(object sender, MouseButtonEventArgs e)
         {
             //设置对应菜单的图标列表
-            icons.ItemsSource = appData.MenuList[menus.SelectedIndex].IconList;
-            appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
+            MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
+            icons.ItemsSource = mi.IconList;
+            appData.AppConfig.SelectedMenuIndex = menus.Items.IndexOf(mi);
             CommonCode.SaveAppData(appData);
         }
 
@@ -164,11 +146,81 @@ namespace GeekDesk
         private void IconClick(object sender, MouseButtonEventArgs e)
         {
             IconInfo icon = (IconInfo)((StackPanel)sender).Tag;
-            System.Diagnostics.Process.Start(icon.Path);
-            icon.Count++;
-            CommonCode.SaveAppData(appData);
+            if (icon.AdminStartUp)
+            {
+                StartIconApp(icon, IconStartType.ADMIN_STARTUP);
+            }
+            else
+            {
+                StartIconApp(icon, IconStartType.DEFAULT_STARTUP);
+            }
+        }
+
+        /// <summary>
+        /// 管理员方式启动
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void IconAdminStart(object sender, RoutedEventArgs e)
+        {
+            IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
+            StartIconApp(icon, IconStartType.ADMIN_STARTUP);
+        }
+
+        /// <summary>
+        /// 打开文件所在位置
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void ShowInExplore(object sender, RoutedEventArgs e)
+        {
+            IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
+            StartIconApp(icon, IconStartType.SHOW_IN_EXPLORE);
+        }
+
+        private void StartIconApp(IconInfo icon, IconStartType type)
+        {
+            try
+            {
+
+                if (!File.Exists(icon.Path) && !Directory.Exists(icon.Path))
+                {
+                    HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
+                    return;
+                }
+
+                Process p = new Process();
+                p.StartInfo.FileName = icon.Path;
+
+                switch (type) {
+                    case IconStartType.ADMIN_STARTUP:
+                        p.StartInfo.Arguments = "1";//启动参数
+                        p.StartInfo.Verb = "runas";
+                        p.StartInfo.CreateNoWindow = false; //设置显示窗口
+                        p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动进程
+                        p.StartInfo.ErrorDialog = false;
+                        this.Visibility = Visibility.Collapsed;
+                        break;// c#好像不能case穿透
+                    case IconStartType.DEFAULT_STARTUP:
+                        this.Visibility = Visibility.Collapsed;
+                        break;
+                    case IconStartType.SHOW_IN_EXPLORE:
+                        p.StartInfo.FileName = "Explorer.exe";
+                        p.StartInfo.Arguments = "/e,/select," + icon.Path;
+                        break;
+                }
+                p.Start();
+                icon.Count++;
+                CommonCode.SaveAppData(appData);
+            } catch (Exception)
+            {
+                HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
+            }
+            
         }
 
+        
+
         /// <summary>
         /// data选中事件 设置不可选中
         /// </summary>
@@ -313,7 +365,7 @@ namespace GeekDesk
         /// <param name="e"></param>
         private void CreateMenu(object sender, RoutedEventArgs e)
         {
-            appData.MenuList.Add(new MenuInfo() { MenuEdit = (int)Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewGouop" });
+            appData.MenuList.Add(new MenuInfo() { MenuEdit = (int)Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" });
             menus.SelectedIndex = appData.MenuList.Count - 1;
             //appData.MenuList[appData.MenuList.Count - 1].MenuEdit = (int)Visibility.Visible;
             CommonCode.SaveAppData(appData);
@@ -345,6 +397,37 @@ namespace GeekDesk
             }
         }
 
+        /// <summary>
+        /// 弹出Icon属性修改面板
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void PropertyConfig(object sender, RoutedEventArgs e)
+        {
+            HandyControl.Controls.Dialog.Show(new IconInfoDialog((IconInfo)((MenuItem)sender).Tag));
+        }
+
+        /// <summary>
+        /// 从列表删除图标
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void RemoveIcon(object sender, RoutedEventArgs e)
+        {
+            appData.MenuList[menus.SelectedIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
+            CommonCode.SaveAppData(appData);
+        }
+
+        /// <summary>
+        /// 左侧栏宽度改变 持久化
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
+        {
+            appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
+            CommonCode.SaveAppData(appData);
+        }
     }
 
 

BIN
Resource/Image/图片.png


BIN
Resource/Image/文件夹.png


+ 0 - 17
Util/FileIcon.cs

@@ -19,23 +19,6 @@ namespace GeekDesk.Util
 
         public static BitmapImage GetBitmapImage(string filePath)
         {
-            //Icon ico;
-            //BitmapImage bmpImage = null;
-            //MemoryStream strm;
-            //using (ico = GetIcon(filePath))
-            //{
-            //    Bitmap bmp = ico.ToBitmap();
-            //    using (strm = new MemoryStream())
-            //    {
-            //        bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
-            //        bmpImage = new BitmapImage();
-            //        bmpImage.BeginInit();
-            //        strm.Seek(0, SeekOrigin.Begin);
-            //        bmpImage.StreamSource = strm;
-            //        bmpImage.EndInit();
-            //    }
-            //}
-            //return bmpImage;
             Icon ico = GetIcon(filePath);
             Bitmap bmp = ico.ToBitmap();
             MemoryStream strm = new MemoryStream();

+ 207 - 0
Util/ImageUtil.cs

@@ -0,0 +1,207 @@
+using GeekDesk.Constant;
+using System;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Windows.Media.Imaging;
+
+namespace GeekDesk.Util
+{
+    class ImageUtil
+    {
+
+        /// <summary>
+        /// 图片数组转 BitmapImage
+        /// </summary>
+        /// <param name="array"></param>
+        /// <returns></returns>
+        public static BitmapImage ByteArrToImage(byte[] array)
+        {
+            using (var ms = new System.IO.MemoryStream(array))
+            {
+                var image = new BitmapImage();
+                image.BeginInit();
+                image.CacheOption = BitmapCacheOption.OnLoad; // here
+                image.StreamSource = ms;
+                image.EndInit();
+                return image;
+            }
+        }
+
+        /// <summary>
+        /// BitmapImage 转数组
+        /// </summary>
+        /// <param name="bi"></param>
+        /// <returns></returns>
+        public static byte[] BitmapImageToByte(BitmapImage bi)
+        {
+            using (MemoryStream memStream = new MemoryStream())
+            {
+                PngBitmapEncoder encoder = new PngBitmapEncoder();
+                encoder.Frames.Add(BitmapFrame.Create(bi));
+                encoder.Save(memStream);
+                return memStream.GetBuffer();
+            }
+        }
+
+        /// <summary>
+        /// 图片base64 转 BitmapImage
+        /// </summary>
+        /// <param name="base64"></param>
+        /// <returns></returns>
+        public static BitmapImage Base64ToBitmapImage(string base64)
+        {
+            byte[] byteBuffer = Convert.FromBase64String(base64);
+            return ByteArrToImage(byteBuffer);
+        }
+
+        /// <summary>
+        /// 获取文件 icon
+        /// </summary>
+        /// <param name="filePath">文件路径</param>
+        /// <returns></returns>
+        public static BitmapImage GetBitmapIconByPath(string filePath)
+        {
+            if (File.Exists(filePath))
+            {
+                if (IsImage(filePath)) {
+                    //图片
+                    return GetThumbnail(filePath, 256, 256);
+                } else
+                { //其它文件
+                    return FileIcon.GetBitmapImage(filePath);
+                }
+            } else if(Directory.Exists(filePath)) {
+                //文件夹
+                return ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_DIR_IMAGE_BASE64);
+            }
+            return null;
+        }
+
+
+        /// <summary>
+        ///
+        /// </summary>
+        /// <param name="lcFilename">需要改变大小的图片位置</param>
+        /// <param name="lnWidth">缩略图的宽度</param>
+        /// <param name="lnHeight">缩略图的高度</param>
+        /// <returns></returns>
+        public static BitmapImage GetThumbnail(string lcFilename, int lnWidth, int lnHeight)
+        {
+            Bitmap bmpOut = null;
+            try
+            {
+                Bitmap loBMP = new Bitmap(lcFilename);
+                ImageFormat loFormat = loBMP.RawFormat;
+
+                decimal lnRatio;
+                int lnNewWidth = 0;
+                int lnNewHeight = 0;
+
+                //如果图像小于缩略图直接返回原图,因为upfront
+                if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
+                    return BitmapToBitmapImage(bmpOut);
+                if (loBMP.Width > loBMP.Height)
+                {
+                    lnRatio = (decimal)lnWidth / loBMP.Width;
+                    lnNewWidth = lnWidth;
+                    decimal lnTemp = loBMP.Height * lnRatio;
+                    lnNewHeight = (int)lnTemp;
+                }
+                else
+                {
+                    lnRatio = (decimal)lnHeight / loBMP.Height;
+                    lnNewHeight = lnHeight;
+                    decimal lnTemp = loBMP.Width * lnRatio;
+                    lnNewWidth = (int)lnTemp;
+                }
+                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
+                Graphics g = Graphics.FromImage(bmpOut);
+                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
+                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
+                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
+                loBMP.Dispose();
+            }
+            catch
+            {
+                return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
+            }
+            return BitmapToBitmapImage(bmpOut);
+        }
+
+
+        /// <summary>
+        /// Bitmap to BitmapImage
+        /// </summary>
+        /// <param name="bitmap"></param>
+        /// <returns></returns>
+        private static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
+        {
+            BitmapImage bitmapImage = new BitmapImage();
+            using (MemoryStream ms = new MemoryStream())
+            {
+                bitmap.Save(ms, bitmap.RawFormat);
+                bitmapImage.BeginInit();
+                bitmapImage.StreamSource = ms;
+                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
+                bitmapImage.EndInit();
+                bitmapImage.Freeze();
+            }
+            return bitmapImage;
+        }
+
+        /// <summary>
+        /// 图片文件转base64
+        /// </summary>
+        /// <param name="Imagefilename"></param>
+        /// <returns></returns>
+        public static string FileImageToBase64(string Imagefilename, ImageFormat format)
+        {
+            try
+            {
+                Bitmap bmp = new Bitmap(Imagefilename);
+
+                MemoryStream ms = new MemoryStream();
+                bmp.Save(ms, format);
+                byte[] arr = new byte[ms.Length];
+                ms.Position = 0;
+                ms.Read(arr, 0, (int)ms.Length);
+                ms.Close();
+                return Convert.ToBase64String(arr);
+            }
+            catch (Exception)
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 判断文件是否为图片
+        /// </summary>
+        /// <param name="path">文件路径</param>
+        /// <returns></returns>
+        public static bool IsImage(string path)
+        {
+            try
+            {
+                string strExt = Path.GetExtension(path).Substring(1);
+                string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif";
+                string[] suffixArr = suffixs.Split(',');
+                foreach (string suffix in suffixArr)
+                {
+                    if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase))
+                    {
+                        return true;
+                    }
+                }
+                return false;
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+        }
+
+
+    }
+}

+ 1 - 1
ViewModel/AppConfig.cs

@@ -7,7 +7,7 @@ namespace GeekDesk.ViewModel
 {
 
     [Serializable]
-    public class AppConfig : System.ComponentModel.INotifyPropertyChanged
+    public class AppConfig : INotifyPropertyChanged
     {
         private int menuSortType = (int)SortType.CUSTOM; //菜单排序类型
         private int iconSortType = (int)SortType.CUSTOM; //图表排序类型

+ 1 - 1
ViewModel/AppData.cs

@@ -6,7 +6,7 @@ using System.ComponentModel;
 namespace GeekDesk.ViewModel
 {
     [Serializable]
-    class AppData : INotifyPropertyChanged
+    public class AppData : INotifyPropertyChanged
     {
         private ObservableCollection<MenuInfo> menuList = new ObservableCollection<MenuInfo>();
         private AppConfig appConfig = new AppConfig();

+ 35 - 27
ViewModel/IconInfo.cs

@@ -1,4 +1,5 @@
 using GeekDesk.Constant;
+using GeekDesk.Util;
 using System;
 using System.ComponentModel;
 using System.IO;
@@ -16,8 +17,38 @@ namespace GeekDesk.ViewModel
         private BitmapImage bitmapImage; //位图
         private byte[] imageByteArr; //图片 base64
         private string content; //显示信息
-        private int imageWidth = (int)DefaultConstant.IMAGE_WIDTH;
-        private int imageHeight = (int)DefaultConstant.IMAGE_HEIGHT;
+        private int imageWidth = (int)DefaultConstant.IMAGE_WIDTH; //图片宽度
+        private int imageHeight = (int)DefaultConstant.IMAGE_HEIGHT; //图片高度
+        private bool adminStartUp = false; //始终管理员方式启动  默认否
+        private byte[] defaultImage;
+
+
+
+        public byte[] DefaultImage
+        {
+            get
+            {
+                return defaultImage;
+            }
+            set
+            {
+                defaultImage = value;
+                OnPropertyChanged("DefaultImage");
+            }
+        }
+
+        public bool AdminStartUp
+        {
+            get
+            {
+                return adminStartUp;
+            }
+            set
+            {
+                adminStartUp = value;
+                OnPropertyChanged("AdminStartUp");
+            }
+        }
 
         public int Count
         {
@@ -66,12 +97,12 @@ namespace GeekDesk.ViewModel
             get
             {
 
-                return ToImage(ImageByteArr);
+                return ImageUtil.ByteArrToImage(ImageByteArr);
             }
             set
             {
                 bitmapImage = value;
-                ImageByteArr = getJPGFromImageControl(bitmapImage);
+                ImageByteArr = ImageUtil.BitmapImageToByte(bitmapImage);
                 OnPropertyChanged("BitmapImage");
             }
         }
@@ -140,29 +171,6 @@ namespace GeekDesk.ViewModel
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
 
-        public BitmapImage ToImage(byte[] array)
-        {
-            using (var ms = new System.IO.MemoryStream(array))
-            {
-                var image = new BitmapImage();
-                image.BeginInit();
-                image.CacheOption = BitmapCacheOption.OnLoad; // here
-                image.StreamSource = ms;
-                image.EndInit();
-                return image;
-            }
-        }
-
-        public byte[] getJPGFromImageControl(BitmapImage bi)
-        {
-            using (MemoryStream memStream = new MemoryStream())
-            {
-                PngBitmapEncoder encoder = new PngBitmapEncoder();
-                encoder.Frames.Add(BitmapFrame.Create(bi));
-                encoder.Save(memStream);
-                return memStream.GetBuffer();
-            }
-        }
 
     }
 }

+ 1 - 1
ViewModel/MenuInfo.cs

@@ -11,7 +11,7 @@ namespace GeekDesk.ViewModel
 {
 
     [Serializable]
-    class MenuInfo : INotifyPropertyChanged
+    public class MenuInfo : INotifyPropertyChanged
     {
        
 

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików