Skip to content

Commit 12e8486

Browse files
committed
新增快捷键锁定功能。
1 parent 1b3aaba commit 12e8486

4 files changed

Lines changed: 83 additions & 18 deletions

File tree

EasyLockBit.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1414
<Deterministic>true</Deterministic>
1515
<IsWebBootstrapper>false</IsWebBootstrapper>
16+
<TargetFrameworkProfile />
1617
<PublishUrl>publish\</PublishUrl>
1718
<Install>true</Install>
1819
<InstallFrom>Disk</InstallFrom>
@@ -31,11 +32,10 @@
3132
<OpenBrowserOnPublish>false</OpenBrowserOnPublish>
3233
<AutorunEnabled>true</AutorunEnabled>
3334
<ApplicationRevision>0</ApplicationRevision>
34-
<ApplicationVersion>1.0.0.0</ApplicationVersion>
35+
<ApplicationVersion>1.0.1.0</ApplicationVersion>
3536
<UseApplicationTrust>false</UseApplicationTrust>
3637
<PublishWizardCompleted>true</PublishWizardCompleted>
3738
<BootstrapperEnabled>true</BootstrapperEnabled>
38-
<TargetFrameworkProfile />
3939
</PropertyGroup>
4040
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
4141
<PlatformTarget>AnyCPU</PlatformTarget>

MainWin.Designer.cs

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MainWin.cs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,42 @@ namespace Bit_Locker
1515
{
1616
public partial class MainWin : Form
1717
{
18+
// 快捷键相关 Windows API
19+
[DllImport("user32.dll")]
20+
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
21+
22+
[DllImport("user32.dll")]
23+
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
24+
1825
public string[] driveList;
1926
List<string> bitlockerDrives = new List<string>();
2027
public string targetDrive;
2128

2229
//复选框注册表,用于保持上次程序关闭前的复选框状态
2330
private const string RegistryKeyPath = @"SOFTWARE\EasyLockBit";
24-
private const string RegistryValueName = "ToDelVolCheckBoxState";
31+
private const string ToDelVol_RegName = "ToDelVolCheckBoxState";
32+
private const string IsEnableHotKeys_RegName = "IsEnableHotKeysCheckBoxState";
33+
34+
//用于注册快捷键
35+
private const int HOTKEY_ID = 9000;
36+
private const uint MOD_CONTROL = 0x0002; // 修饰键
37+
private const uint VK_B = 0x42; // 虚拟键码
2538

2639
public MainWin()
2740
{
2841
InitializeComponent();
2942
CheckForAdminRights();
43+
if(IsEnableHotKeys.Checked == true)
44+
{
45+
RegisterHotKey(this.Handle, HOTKEY_ID, MOD_CONTROL, VK_B); // 注册全局快捷键
46+
}
3047
RefleshDrivers();
3148
}
3249

3350
//检测是否以管理员身份运行
3451
private void CheckForAdminRights()
3552
{
36-
// 获取当前进程的Windows身份
3753
WindowsIdentity identity = WindowsIdentity.GetCurrent();
38-
// 创建一个WindowsPrincipal对象
3954
WindowsPrincipal principal = new WindowsPrincipal(identity);
4055

4156
// 检查当前用户是否属于管理员组
@@ -54,8 +69,11 @@ private void MainWin_Load(object sender, EventArgs e)
5469
{
5570
if (key != null)
5671
{
57-
object value = key.GetValue(RegistryValueName);
72+
object value = key.GetValue(ToDelVol_RegName);
5873
if (value != null) toDelVolLabel.Checked = Convert.ToBoolean(value);
74+
75+
value = key.GetValue(IsEnableHotKeys_RegName);
76+
if (value != null) IsEnableHotKeys.Checked = Convert.ToBoolean(value);
5977
}
6078
}
6179
}
@@ -65,8 +83,26 @@ private void MainWin_FormClosing(object sender, FormClosingEventArgs e)
6583
// 在程序关闭时保存CheckBox状态
6684
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryKeyPath))
6785
{
68-
key.SetValue(RegistryValueName, toDelVolLabel.Checked);
86+
key.SetValue(ToDelVol_RegName, toDelVolLabel.Checked);
87+
key.SetValue(IsEnableHotKeys_RegName, IsEnableHotKeys.Checked);
88+
}
89+
90+
UnregisterHotKey(this.Handle, HOTKEY_ID); // 注销全局快捷键
91+
}
92+
93+
protected override void WndProc(ref Message m)
94+
{
95+
const int WM_HOTKEY = 0x0312;
96+
97+
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == HOTKEY_ID)
98+
{
99+
// 快捷键触发
100+
object sender = null;
101+
EventArgs e = null;
102+
ContinueForceButton_Click(sender, e);
69103
}
104+
105+
base.WndProc(ref m);
70106
}
71107

72108
private void RefleshDrivers()
@@ -155,9 +191,7 @@ internal void ContinueForceButton_Click(object sender, EventArgs e)
155191
{
156192
// 创建批处理文件内容
157193
string batContent = $"%windir%\\Sysnative\\manage-bde.exe -lock {targetDrive} -ForceDismount\r\n";
158-
159194
string batFilePath = Path.Combine(Path.GetTempPath(), "lock_drive.bat");
160-
161195
File.WriteAllText(batFilePath, batContent, Encoding.ASCII);
162196

163197
try
@@ -254,7 +288,7 @@ private bool IsDriveBitlocked(string drive)
254288
{
255289
foreach (ManagementObject volume in searcher.Get())
256290
{
257-
if ((UInt32?)volume["ProtectionStatus"] == 1) return true;
291+
if ((UInt32?)volume["ProtectionStatus"] == 1) return true; // 此值必须显式转换为UInt32
258292
}
259293
}
260294
}
@@ -270,5 +304,17 @@ private void RefleshButton_Click(object sender, EventArgs e)
270304
{
271305
RefleshDrivers();
272306
}
307+
308+
private void IsEnableHotKeys_CheckedChanged(object sender, EventArgs e)
309+
{
310+
if(IsEnableHotKeys.Checked == false)
311+
{
312+
UnregisterHotKey(this.Handle, HOTKEY_ID);
313+
}
314+
else
315+
{
316+
RegisterHotKey(this.Handle, HOTKEY_ID, MOD_CONTROL, VK_B);
317+
}
318+
}
273319
}
274320
}

Properties/AssemblyInfo.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Reflection;
1+
using System.Resources;
2+
using System.Reflection;
23
using System.Runtime.CompilerServices;
34
using System.Runtime.InteropServices;
45

56
// 有关程序集的一般信息由以下
67
// 控制。更改这些特性值可修改
78
// 与程序集关联的信息。
89
[assembly: AssemblyTitle("EasyLockBit")]
9-
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyDescription("一款BitLocker快捷锁定工具。")]
1011
[assembly: AssemblyConfiguration("")]
1112
[assembly: AssemblyCompany("")]
1213
[assembly: AssemblyProduct("EasyLockBit")]
@@ -32,5 +33,6 @@
3233
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3334
//通过使用 "*",如下所示:
3435
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
36+
[assembly: AssemblyVersion("1.0.1")]
37+
[assembly: AssemblyFileVersion("1.0.1")]
38+
[assembly: NeutralResourcesLanguage("zh-Hans")]

0 commit comments

Comments
 (0)