Browse Source

添加开机自启动

liufei 4 years ago
parent
commit
81f3ad0fdc

+ 5 - 83
App.xaml.cs

@@ -1,4 +1,5 @@
-using Microsoft.Win32;
+using GeekDesk.Constant;
+using Microsoft.Win32;
 using System;
 using System.IO;
 using System.Windows;
@@ -23,96 +24,17 @@ namespace GeekDesk
         {
 
             bool ret;
-            mutex = new System.Threading.Mutex(true, "GeekDesk", out ret);
+            mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
             if (!ret)
             {
                 Environment.Exit(0);
             }
-            #region 设置程序开机自动运行(+注册表项)
-            try
-            {
-                //SetSelfStarting(true, "GeekDesk.exe");
-            }
-            catch (Exception ex)
-            {
-            }
-
-            #endregion
         }
+    }
 
 
 
-        #region 注册表开机自启动
-
-
-        /// <summary>
-        /// 开机自动启动
-        /// </summary>
-        /// <param name="started">设置开机启动,或取消开机启动</param>
-        /// <param name="exeName">注册表中的名称</param>
-        /// <returns>开启或停用是否成功</returns>
-        public bool SetSelfStarting(bool started, string exeName)
-        {
-            RegistryKey key = null;
-            try
-            {
-                string exeDir = System.Windows.Forms.Application.ExecutablePath;
-                //RegistryKey HKLM = Registry.CurrentUser;
-                //key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
-                key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
-
-                if (key == null)//如果该项不存在的话,则创建该子项
-                {
-                    key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
-                }
-                if (started)
-                {
-                    try
-                    {
-                        object ob = key.GetValue(exeName, -1);
-
-                        if (!ob.ToString().Equals(exeDir))
-                        {
-                            if (!ob.ToString().Equals("-1"))
-                            {
-                                key.DeleteValue(exeName);//取消开机启动
-                            }
-                            key.SetValue(exeName, exeDir);//设置为开机启动
-                        }
-                        key.Close();
-
-                    }
-                    catch (Exception ex)
-                    {
-                        return false;
-                    }
-                }
-                else
-                {
-                    try
-                    {
-                        key.DeleteValue(exeName);//取消开机启动
-                        key.Close();
-                    }
-                    catch (Exception ex)
-                    {
-                        return false;
-                    }
-                }
-                return true;
-            }
-            catch (Exception ex)
-            {
-                if (key != null)
-                {
-                    key.Close();
-                }
-                return false;
-            }
-        }
-
-        #endregion
-    }
+      
     //    private void WriteLog(object exception)
     //    {
     //        Exception ex = exception as Exception;

+ 3 - 0
Constant/Constants.cs

@@ -9,6 +9,9 @@ namespace GeekDesk.Constant
     public class Constants
     {
         public static string APP_DIR = AppDomain.CurrentDomain.BaseDirectory.Trim();
+
+        public static string MY_NAME = "GeekDesk";
+
         /// <summary>
         /// app数据文件路径
         /// </summary>

+ 3 - 3
Control/UserControls/Config/AboutControl.xaml

@@ -25,7 +25,7 @@
                            Command="hc:ControlCommands.OpenLink" 
                            CommandParameter="https://github.com/Demo-Liu/GeekDesk"
                            Margin="0,0,10,0" Color="#24292F"/>
-                    <hc:Shield Subject="码云" Status="Demo-liu" 
+                    <hc:Shield Subject="Gitee" Status="Demo-liu" 
                            Command="hc:ControlCommands.OpenLink" 
                            CommandParameter="" 
                            Margin="0,5,10,0" Color="#C71D23"/>
@@ -63,7 +63,7 @@
             </hc:UniformSpacingPanel>
             <TextBlock Margin="0,20,0,0" FontSize="13" Width="200" TextAlignment="Center" Text="这是个人开发的程序,所有人可任意修改和免费使用(商用请联系作者)" TextWrapping="Wrap"/>
 
-            <hc:UniformSpacingPanel Spacing="10" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
+            <!--<hc:UniformSpacingPanel Spacing="10" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
                 <TextBlock Text="更新源:" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                 <RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}" 
                          Style="{StaticResource RadioButtonIcon}" Content="Gitee"
@@ -71,7 +71,7 @@
                 <RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}"
                          Style="{StaticResource RadioButtonIcon}" Content="GitHub" 
                          IsChecked="{Binding UpdateType, Mode=TwoWay, Converter={StaticResource UpdateTypeConvert}, ConverterParameter=2}"/>
-            </hc:UniformSpacingPanel>
+            </hc:UniformSpacingPanel>-->
         </StackPanel>
     </hc:SimplePanel>
     

+ 45 - 0
Control/UserControls/Config/OtherControl.xaml

@@ -0,0 +1,45 @@
+<UserControl x:Class="GeekDesk.Control.UserControls.Config.OtherControl"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:cvt="clr-namespace:GeekDesk.Converts"
+             xmlns:local="clr-namespace:GeekDesk.Control.UserControls.PannelCard"
+             xmlns:hc="https://handyorg.github.io/handycontrol"
+             mc:Ignorable="d" 
+             Background="AliceBlue"
+             d:DesignHeight="400" d:DesignWidth="500"
+             >
+
+    <UserControl.Resources>
+        <cvt:UpdateTypeConvert x:Key="UpdateTypeConvert"/>
+    </UserControl.Resources>
+    <hc:SimplePanel Margin="20">
+        <StackPanel >
+            <TextBlock Text="程序设置" />
+            <hc:UniformSpacingPanel Spacing="10" Margin="20,5,0,0">
+                <CheckBox x:Name="SelfStartUpBox" Content="开机自启动" IsChecked="{Binding SelfStartUp}" Click="SelfStartUpBox_Click">
+                    <CheckBox.Background>
+                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
+                            <GradientStop Color="#FF9EA3A6"/>
+                        </LinearGradientBrush>
+                    </CheckBox.Background>
+                </CheckBox>
+            </hc:UniformSpacingPanel>
+            <TextBlock Text="更新源"  Margin="0,20,0,0"/>
+            <hc:UniformSpacingPanel Spacing="10" Margin="20,5,0,0">
+                <RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}" 
+                             Style="{StaticResource RadioButtonIcon}" Content="Gitee"
+                             hc:IconElement.Geometry="{StaticResource Gitee}"
+                             Foreground="#B32225"
+                             IsChecked="{Binding UpdateType, Mode=TwoWay, Converter={StaticResource UpdateTypeConvert}, ConverterParameter=1}"/>
+                <RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}"
+                             hc:IconElement.Geometry="{StaticResource GitHub}"
+                             Style="{StaticResource RadioButtonIcon}" Content="GitHub" 
+                             Foreground="Black"
+                             IsChecked="{Binding UpdateType, Mode=TwoWay, Converter={StaticResource UpdateTypeConvert}, ConverterParameter=2}"/>
+            </hc:UniformSpacingPanel>
+        </StackPanel>
+    </hc:SimplePanel>
+
+</UserControl>

+ 37 - 0
Control/UserControls/Config/OtherControl.xaml.cs

@@ -0,0 +1,37 @@
+using GeekDesk.Constant;
+using GeekDesk.Util;
+using GeekDesk.ViewModel;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace GeekDesk.Control.UserControls.Config
+{
+    /// <summary>
+    /// OtherControl.xaml 的交互逻辑
+    /// </summary>
+    public partial class OtherControl : UserControl
+    {
+        public OtherControl()
+        {
+            InitializeComponent();
+        }
+
+        private void SelfStartUpBox_Click(object sender, RoutedEventArgs e)
+        {
+            AppConfig appConfig = MainWindow.appData.AppConfig;
+            RegisterUtil.SetSelfStarting(appConfig.SelfStartUp, Constants.MY_NAME);
+        }
+    }
+}

+ 15 - 0
Control/Windows/ConfigWindow.xaml

@@ -103,6 +103,21 @@
                             />
                         </hc:SideMenuItem.Icon>
                     </hc:SideMenuItem>
+                    <hc:SideMenuItem Header="其它"
+                                     Tag="Other"
+                                     Selected="MemuClick">
+                        <hc:SideMenuItem.Icon>
+                            <Button Background="Transparent" 
+                                    IsEnabled="False"
+                                    Opacity="1"
+                            BorderThickness="0"
+                            hc:IconElement.Geometry="{StaticResource Other}"
+                            hc:IconElement.Height="18"
+                            hc:IconElement.Width="18"
+                            HorizontalAlignment="Right"
+                            />
+                        </hc:SideMenuItem.Icon>
+                    </hc:SideMenuItem>
                 </hc:SideMenu>
             </hc:Card>
             <hc:ScrollViewer  Grid.Row="0" Grid.Column="1">

+ 4 - 0
Control/Windows/ConfigWindow.xaml.cs

@@ -18,6 +18,7 @@ namespace GeekDesk.Control.Windows
         private static AboutControl about = new AboutControl();
         private static ThemeControl theme = new ThemeControl();
         private static MotionControl motion = new MotionControl();
+        private static OtherControl other = new OtherControl();
         public MainWindow mainWindow;
 
         private ConfigWindow(AppConfig appConfig, MainWindow mainWindow)
@@ -64,6 +65,9 @@ namespace GeekDesk.Control.Windows
                 case "Theme":
                     RightCard.Content = theme;
                     break;
+                case "Other":
+                    RightCard.Content = other;
+                    break;
                 default:
                     RightCard.Content = about;
                     break;

+ 8 - 0
GeekDesk.csproj

@@ -102,6 +102,9 @@
     <Compile Include="Control\Other\CustomIconUrlDialog.xaml.cs">
       <DependentUpon>CustomIconUrlDialog.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Control\UserControls\Config\OtherControl.xaml.cs">
+      <DependentUpon>OtherControl.xaml</DependentUpon>
+    </Compile>
     <Compile Include="Control\UserControls\ToDo\TodoControl.xaml.cs">
       <DependentUpon>TodoControl.xaml</DependentUpon>
     </Compile>
@@ -165,6 +168,7 @@
     <Compile Include="Converts\MenuWidthConvert.cs" />
     <Compile Include="Util\MouseUtil.cs" />
     <Compile Include="Util\MouseUtilities.cs" />
+    <Compile Include="Util\RegisterUtil.cs" />
     <Compile Include="Util\StringUtil.cs" />
     <Compile Include="Util\SvgToGeometry.cs" />
     <Compile Include="Util\SystemIcon.cs" />
@@ -182,6 +186,10 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="Control\UserControls\Config\OtherControl.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="Control\UserControls\ToDo\TodoControl.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

+ 5 - 0
MainWindow.xaml.cs

@@ -69,6 +69,11 @@ namespace GeekDesk
             }
             RegisterHotKey(true);
             //RegisterCreateToDoHotKey(true);
+
+            if (!appData.AppConfig.SelfStartUped)
+            {
+                RegisterUtil.SetSelfStarting(appData.AppConfig.SelfStartUp, Constants.MY_NAME);
+            }
             UpdateThread.Update();
 
         }

File diff suppressed because it is too large
+ 0 - 0
Resource/Dictionary/Geometry.xaml


+ 82 - 0
Util/RegisterUtil.cs

@@ -0,0 +1,82 @@
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace GeekDesk.Util
+{
+    class RegisterUtil
+    {
+
+        #region 注册表开机自启动
+        /// <summary>
+        /// 开机自动启动
+        /// </summary>
+        /// <param name="started">设置开机启动,或取消开机启动</param>
+        /// <param name="exeName">注册表中的名称</param>
+        /// <returns>开启或停用是否成功</returns>
+        public static bool SetSelfStarting(bool started, string exeName)
+        {
+            RegistryKey key = null;
+            try
+            {
+                string exeDir = System.Windows.Forms.Application.ExecutablePath;
+                //RegistryKey HKLM = Registry.CurrentUser;
+                //key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
+                key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
+
+                if (key == null)//如果该项不存在的话,则创建该子项
+                {
+                    key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
+                }
+                if (started)
+                {
+                    try
+                    {
+                        object ob = key.GetValue(exeName, -1);
+
+                        if (!ob.ToString().Equals(exeDir))
+                        {
+                            if (!ob.ToString().Equals("-1"))
+                            {
+                                key.DeleteValue(exeName);//取消开机启动
+                            }
+                            key.SetValue(exeName, exeDir);//设置为开机启动
+                        }
+                        key.Close();
+
+                    }
+                    catch (Exception ex)
+                    {
+                        return false;
+                    }
+                }
+                else
+                {
+                    try
+                    {
+                        key.DeleteValue(exeName);//取消开机启动
+                        key.Close();
+                    }
+                    catch (Exception ex)
+                    {
+                        return false;
+                    }
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                if (key != null)
+                {
+                    key.Close();
+                }
+                return false;
+            }
+        }
+
+        #endregion
+    }
+}

+ 30 - 0
ViewModel/AppConfig.cs

@@ -54,9 +54,39 @@ namespace GeekDesk.ViewModel
 
         private UpdateType updateType = UpdateType.Gitee; //更新源 默认gitee源
 
+        private bool selfStartUp = true; //开机自启动设置
+        private bool selfStartUped = false;  //是否已设置
+
         #region GetSet
 
 
+        public bool SelfStartUped
+        {
+            get
+            {
+                return selfStartUped;
+            }
+            set
+            {
+                selfStartUped = value;
+                OnPropertyChanged("SelfStartUped");
+            }
+        }
+
+        public bool SelfStartUp
+        {
+            get
+            {
+                return selfStartUp;
+            }
+            set
+            {
+                selfStartUp = value;
+                selfStartUped = true;
+                OnPropertyChanged("SelfStartUp");
+            }
+        }
+
         public Key ToDoHotkey
         {
             get

Some files were not shown because too many files changed in this diff