Skip to content

Commit

Permalink
添加两个新选项
Browse files Browse the repository at this point in the history
添加双击切换
放大列表
  • Loading branch information
huiyadanli committed Oct 5, 2021
1 parent e2bd60a commit 9ce5416
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 61 deletions.
15 changes: 15 additions & 0 deletions GenshinAccount/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="GenshinAccount.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<GenshinAccount.Properties.Settings>
<setting name="AutoRestartYSEnabled" serializeAs="String">
<value>False</value>
</setting>
<setting name="SkipTipsEnabled" serializeAs="String">
<value>False</value>
</setting>
</GenshinAccount.Properties.Settings>
</userSettings>
</configuration>
21 changes: 21 additions & 0 deletions GenshinAccount/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 103 additions & 7 deletions GenshinAccount/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class FormMain : Form
{
private readonly string userDataPath = Path.Combine(Application.StartupPath, "UserData");
private string thisVersion;
private string installPath = FindInstallPathFromRegistry();
public FormMain()
{
InitializeComponent();
Expand All @@ -35,8 +36,16 @@ private void FormMain_Load(object sender, EventArgs e)
this.Text += currentVersion;
GAHelper.Instance.RequestPageView($"/acct/main/{thisVersion}", $"进入{thisVersion}版本原神账户切换工具主界面");


chkAutoStartYS.Checked = Properties.Settings.Default.AutoRestartYSEnabled;
chkSkipTips.Checked = Properties.Settings.Default.SkipTipsEnabled;

lvwAcct.Columns[0].Width = lvwAcct.Width;
ImageList imageList = new ImageList();
imageList.ImageSize = new Size(10, 20);
lvwAcct.SmallImageList = imageList;
RefreshList();

}

private void btnSaveCurr_Click(object sender, EventArgs e)
Expand All @@ -63,7 +72,7 @@ private void RefreshList()
});
}

if(lvwAcct.Items.Count > 0)
if (lvwAcct.Items.Count > 0)
{
btnDelete.Enabled = true;
btnSwitch.Enabled = true;
Expand All @@ -83,27 +92,66 @@ private void btnSwitch_Click(object sender, EventArgs e)
return;
}
string name = lvwAcct.SelectedItems[0]?.Text;
Switch(name, chkAutoStartYS.Checked);
}

private void Switch(string name, bool autoRestart)
{
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("请选择要切换的账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (YuanShenIsRunning())
if (!autoRestart)
{
MessageBox.Show("原神正在运行,请先关闭原神进程后再切换账号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
if (YuanShenIsRunning())
{
MessageBox.Show("原神正在运行,请先关闭原神进程后再切换账号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
if (MessageBox.Show($"是否要切换为[{name}]", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

if (chkSkipTips.Checked || MessageBox.Show($"是否要切换为[{name}]", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (autoRestart)
{
var pros = Process.GetProcessesByName("YuanShen");
if (pros.Any())
{
pros[0].Kill();
}
}
YSAccount acct = YSAccount.ReadFromDisk(name);
acct.WriteToRegedit();
}

if (autoRestart)
{
if (string.IsNullOrEmpty(installPath))
{
MessageBox.Show("未找到原神安装信息,无法自动启动原神", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
chkAutoStartYS.Checked = false;
}
else
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Path.Combine(installPath, "Genshin Impact Game", "YuanShen.exe");
startInfo.Verb = "runas";
Process.Start(startInfo);
}
}

if (!chkSkipTips.Checked)
{
MessageBox.Show($"账户[{name}]切换成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
if(lvwAcct.SelectedItems.Count == 0)
if (lvwAcct.SelectedItems.Count == 0)
{
MessageBox.Show("请选择要切换的账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
Expand Down Expand Up @@ -136,5 +184,53 @@ private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
{
Process.Start("https://github.com/babalae/genshin-account");
}

private void lvwAcct_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = lvwAcct.HitTest(e.X, e.Y);
if (info.Item != null)
{
Switch(info.Item.Text, chkAutoStartYS.Checked);
}
}

/// <summary>
/// 从注册表中寻找安装路径
/// </summary>
/// <param name="uninstallKeyName">
/// 安装信息的注册表键名 原神
/// </param>
/// <returns>安装路径</returns>
public static string FindInstallPathFromRegistry()
{
try
{
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神"))
{
if (key == null)
{
return null;
}
object installLocation = key.GetValue("InstallPath");
if (installLocation != null && !string.IsNullOrEmpty(installLocation.ToString()))
{
return installLocation.ToString();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}

private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.AutoRestartYSEnabled = chkAutoStartYS.Checked;
Properties.Settings.Default.SkipTipsEnabled = chkSkipTips.Checked;
Properties.Settings.Default.Save();
}
}
}
Loading

0 comments on commit 9ce5416

Please sign in to comment.