按下全域熱鍵 (Ctrl+Alt+F12) 隱藏目前前景視窗 (不顯示在工作列),再按一次還原。需在 DLL 中設置系統鉤子 (WH_KEYBOARD) 攔截全域鍵盤訊息。
EnableHideHook/DisableHideHook。
function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
const KeyPressMask = $80000000;
var F: TextFile; S: String; H: Longint;
begin
Result := 0;
if iCode < 0 then begin Result := CallNextHookEx(hNextHookHide, iCode, wParam, lParam); Exit; end;
// 偵測 Ctrl+Alt+F12 按下 (lParam 最高位=0 表示按下)
if (lParam and KeyPressMask = 0) and (GetKeyState(VK_CONTROL) < 0) and (GetKeyState(VK_MENU) < 0) and (wParam = VK_F12) then
begin
Result := 1; // 攔截訊息不再傳遞
if not FileExists('C:\test.txt') then
begin
AssignFile(F, 'C:\test.txt'); Rewrite(F); WriteLn(F, 0); CloseFile(F);
end
else
begin
AssignFile(F, 'C:\test.txt'); Reset(F); ReadLn(F, S); H := StrToInt(S);
if H = 0 then
begin
H := GetActiveWindow; // 取得目前前景視窗
WriteLn(F, IntToStr(H)); ShowWindow(H, SW_HIDE);
end
else
begin
ShowWindow(H, SW_SHOW); WriteLn(F, '0');
end;
CloseFile(F);
end;
end
else
Result := CallNextHookEx(hNextHookHide, iCode, wParam, lParam);
end;
function EnableHideHook: BOOL;
begin
if hNextHookHide <> 0 then Exit(False);
hNextHookHide := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookHandler, HInstance, 0);
Result := hNextHookHide <> 0;
end;
function DisableHideHook: BOOL;
begin
if hNextHookHide <> 0 then
begin
UnhookWindowsHookEx(hNextHookHide); hNextHookHide := 0; Result := True;
end else Result := False;
end;
function EnableHideHook: BOOL; external 'HKPHide.dll';
function DisableHideHook: BOOL; external 'HKPHide.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin if EnableHideHook then ShowMessage('熱鍵已啟用:Ctrl+Alt+F12 隱藏/顯示前景視窗'); end;
procedure TForm1.Button2Click(Sender: TObject);
begin if DisableHideHook then ShowMessage('熱鍵已停用'); end;
C:\test.txt 儲存視窗句柄,僅供示範;正式專案建議用共享記憶體 (CreateFileMapping) 或全域原子表。RegisterHotKey (僅限特定視窗) 或低階鍵盤過濾驅動。