feat: add auto start

dev_deconstruction 0.08
DESKTOP-3BO4HSG\ksat 2 years ago
parent 92c7987eb6
commit a40d6d287e

@ -368,17 +368,15 @@
Grid.Row="2" Grid.Row="2"
HorizontalAlignment="Center" HorizontalAlignment="Center"
Orientation="Horizontal"> Orientation="Horizontal">
<Label Content="当前语言:" <TextBlock Text="当前语言: "
Background="Transparent" VerticalAlignment="Center"/>
BorderThickness="0" />
<ComboBox x:Name="InCombo" <ComboBox x:Name="InCombo"
Style="{StaticResource cmbstyle}" Style="{StaticResource cmbstyle}"
ItemsSource="{Binding InputCombo}" ItemsSource="{Binding InputCombo}"
SelectedItem="{Binding InputComboSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> SelectedItem="{Binding InputComboSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<Separator Margin="20,0"/> <Separator Margin="20,0"/>
<Label Content="目标语言:" <TextBlock Text="当前语言: "
Background="Transparent" VerticalAlignment="Center"/>
BorderThickness="0" />
<ComboBox x:Name="OutCombo" <ComboBox x:Name="OutCombo"
Style="{StaticResource cmbstyle}" Style="{StaticResource cmbstyle}"
ItemsSource="{Binding OutputCombo}" ItemsSource="{Binding OutputCombo}"

@ -50,20 +50,33 @@ namespace STranslate
System.Windows.Forms.MenuItem OpenMainWinBTN = new System.Windows.Forms.MenuItem("显示主界面"); System.Windows.Forms.MenuItem OpenMainWinBTN = new System.Windows.Forms.MenuItem("显示主界面");
OpenMainWinBTN.Click += new EventHandler(OpenMainWin_Click); OpenMainWinBTN.Click += new EventHandler(OpenMainWin_Click);
System.Windows.Forms.MenuItem AutoStartBTN = new System.Windows.Forms.MenuItem("开机自启");
AutoStartBTN.Click += new EventHandler(AutoStart_Click);
AutoStartBTN.Checked = AutoStart.IsAutoStart();
System.Windows.Forms.MenuItem exitButton = new System.Windows.Forms.MenuItem("退出"); System.Windows.Forms.MenuItem ExitBTN = new System.Windows.Forms.MenuItem("退出");
exitButton.Click += new EventHandler(Exit_Click); ExitBTN.Click += new EventHandler(Exit_Click);
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] {
CrossWordTranslateMenuItemBTN, CrossWordTranslateMenuItemBTN,
ScreenshotTranslateMenuItemBTN, ScreenshotTranslateMenuItemBTN,
InputTranslateMenuItemBTN, InputTranslateMenuItemBTN,
OpenMainWinBTN, OpenMainWinBTN,
exitButton, AutoStartBTN,
ExitBTN,
}; };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
} }
private void AutoStart_Click(object sender, EventArgs e)
{
if (AutoStart.IsAutoStart()) AutoStart.UnSetAutoStart();
else AutoStart.SetAutoStart();
(sender as System.Windows.Forms.MenuItem).Checked = AutoStart.IsAutoStart();
}
/// <summary> /// <summary>
/// 移动 /// 移动

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STranslate.Model
{
public class TranslationInterface
{
public string Name { get; set; }
public string Api { get; set; }
}
}

@ -47,6 +47,6 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示: //通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.7.0")] [assembly: AssemblyVersion("0.0.8.0")]
[assembly: AssemblyFileVersion("0.0.7.0")] [assembly: AssemblyFileVersion("0.0.8.0")]
[assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] [assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")]

@ -82,12 +82,14 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Model\TranslateInterface.cs" />
<Compile Include="Model\VisibilityConverter.cs" /> <Compile Include="Model\VisibilityConverter.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="Utils\AutoStart.cs" />
<Compile Include="Utils\GetWordsUtil.cs" /> <Compile Include="Utils\GetWordsUtil.cs" />
<Compile Include="Utils\HotKeysUtil.cs" /> <Compile Include="Utils\HotKeysUtil.cs" />
<Compile Include="Utils\HttpUtil.cs" /> <Compile Include="Utils\HttpUtil.cs" />
@ -182,5 +184,16 @@
<Install>false</Install> <Install>false</Install>
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </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" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using IWshRuntimeLibrary;
namespace STranslate.Utils
{
public class AutoStart
{
private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
private static readonly string appPath = Assembly.GetEntryAssembly().Location;
private static readonly string appShortcutPath = Path.Combine(StartUpPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk");
/// <summary>
/// 设置开机自启
/// </summary>
public static void SetAutoStart()
{
ShortCutCreate();
}
/// <summary>
/// 检查是否已经设置开机自启
/// </summary>
/// <returns>true: 开机自启 false: 非开机自启</returns>
public static bool IsAutoStart()
{
return ShortCutExist(appPath, StartUpPath);
}
/// <summary>
/// 取消开机自启
/// </summary>
public static void UnSetAutoStart()
{
ShortCutDelete(appPath, StartUpPath);
}
/// <summary>
/// 获取指定文件夹下的所有快捷方式(不包括子文件夹)
/// </summary>
/// <param target="">目标文件夹(绝对路径)</param>
/// <returns></returns>
public static List<string> GetDirectoryFileList(string target)
{
List<string> list = new List<string>();
list.Clear();
string[] files = Directory.GetFiles(target, "*.lnk");
if (files == null || files.Length == 0)
{
return list;
}
for (int i = 0; i < files.Length; i++)
{
list.Add(files[i]);
}
return list;
}
/// <summary>
/// 判断快捷方式是否存在
/// </summary>
/// <param name="path">快捷方式目标(可执行文件的绝对路径)</param>
/// <param target="">目标文件夹(绝对路径)</param>
/// <returns></returns>
public static bool ShortCutExist(string path, string target)
{
bool Result = false;
List<string> list = GetDirectoryFileList(target);
foreach (var item in list)
{
if (path == GetAppPathViaShortCut(item))
{
Result = true;
}
}
return Result;
}
/// <summary>
/// 删除快捷方式(通过快捷方式目标进行删除)
/// </summary>
/// <param name="path">快捷方式目标(可执行文件的绝对路径)</param>
/// <param target="">目标文件夹(绝对路径)</param>
/// <returns></returns>
public static bool ShortCutDelete(string path, string target)
{
bool Result = false;
List<string> list = GetDirectoryFileList(target);
foreach (var item in list)
{
if (path == GetAppPathViaShortCut(item))
{
System.IO.File.Delete(item);
Result = true;
}
}
return Result;
}
/// <summary>
/// 为本程序创建一个快捷方式。
/// </summary>
public static bool ShortCutCreate()
{
bool Result = false;
try
{
ShortCutDelete(appPath, StartUpPath);
var shellType = Type.GetTypeFromProgID("WScript.Shell");
dynamic shell = Activator.CreateInstance(shellType);
var shortcut = shell.CreateShortcut(appShortcutPath);
shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
shortcut.Arguments = string.Empty;
shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
shortcut.Save();
Result = true;
}
catch
{
Result = false;
}
return Result;
}
/// <summary>
/// 获取快捷方式中的目标(可执行文件的绝对路径)
/// </summary>
/// <param name="shortCutPath">快捷方式的绝对路径</param>
/// <returns></returns>
/// <remarks>需引入 COM 组件 Windows Script Host Object Model</remarks>
public static string GetAppPathViaShortCut(string shortCutPath)
{
try
{
WshShell shell = new WshShell();
IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortCutPath);
//快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
//快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
return shortct.TargetPath;
}
catch
{
return null;
}
}
}
}

@ -12,14 +12,6 @@ namespace STranslate.ViewModel
{ {
public class MainVM : BaseVM public class MainVM : BaseVM
{ {
/// <summary>
/// 引入Go项目库
/// </summary>
/// <returns></returns>
//[System.Runtime.InteropServices.DllImport("deepl.dll", EntryPoint = "run")]
//extern static void run();
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
public MainVM() public MainVM()
{ {
@ -264,14 +256,8 @@ namespace STranslate.ViewModel
public List<TranslationInterface> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); } public List<TranslationInterface> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); }
private TranslationInterface _SelectedTranslationInterface; private TranslationInterface _SelectedTranslationInterface;
public TranslationInterface SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); } public TranslationInterface SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); }
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
#endregion Params #endregion Params
} }
public class TranslationInterface
{
public string Name { get; set; }
public string Api { get; set; }
}
} }
Loading…
Cancel
Save