@@ -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}
0 commit comments