DELPHI 隱藏程序窗口,以及TListView控件,點擊標題進行排序
設置視圖:

運行效果:

unit HideWindown; interface uses Windows, Messages, SysUtils, Classes, Forms, StdCtrls, ActiveX, ComObj, ShellAPI, Tlhelp32, Vcl.Controls, Vcl.ComCtrls, psapi, Vcl.ExtCtrls; type TForm1 = class(TForm) GetWList: TButton; ListView1: TListView; HideBtn: TButton; ShowBtn: TButton; RadioGroup1: TRadioGroup; Edit1: TEdit; QueryBtn: TButton; procedure GetWListClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure HideBtnClick(Sender: TObject); procedure ShowBtnClick(Sender: TObject); procedure RadioGroup1Click(Sender: TObject); procedure QueryBtnClick(Sender: TObject); procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn); private procedure SetWindowState(isVisible: Boolean); procedure AddListItem(hwnd: HWND; State, Title: string); function FoundTitle(beginIndex, endIndex: Integer): Boolean; var public { Public declarations } end; var Form1: TForm1; FSortColumn: Integer; //當前哪一列正在被排序 FSortAscending: Boolean; //排序的狀態 implementation {$R *.dfm} function GetWindowTitle(hwnd: hwnd): string; var Buffer: array[0..255] of Char; begin Result := ''; if GetWindowText(hwnd, Buffer, SizeOf(Buffer)) > 0 then begin Result := Buffer; end; end; procedure EnumWindowsProc(hwnd: hwnd; lParam: lParam); stdcall; const MAX_PATH = 260; // 確保常量定義明確 var ProcessID: DWORD; ProcessHandle: THandle; Title: array[0..MAX_PATH] of Char; ListItem: TListItem; State: string; ShouldAdd: Boolean; begin // 獲取進程 ID 和窗口標題 GetWindowThreadProcessId(hwnd, @ProcessID); GetWindowText(hwnd, Title, MAX_PATH); // 如果窗口標題不為空 if Title <> '' then begin // 打開進程以獲取更多信息 ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID); if ProcessHandle <> 0 then try // 檢查窗口是否可見 ShouldAdd := IsWindowVisible(hwnd); if ShouldAdd then State := '顯示' else State := '隱藏'; // 根據 RadioGroup 的選擇決定是否添加項 case Form1.RadioGroup1.ItemIndex of 0: // 顯示所有窗口 ShouldAdd := True; 1: // 只顯示可見窗口 ShouldAdd := ShouldAdd; 2: // 只顯示隱藏窗口 ShouldAdd := not ShouldAdd; else ShouldAdd := False; end; // 如果符合條件,添加到 ListView if ShouldAdd then begin Form1.AddListItem(hwnd, State, Title); end; finally CloseHandle(ProcessHandle); end; end; end; procedure TForm1.AddListItem(hwnd: hwnd; State, Title: string); var ListItem: TListItem; begin ListItem := Form1.ListView1.Items.Add; ListItem.Caption := IntToStr(hwnd); ListItem.SubItems.Add(State); ListItem.SubItems.Add(Title); end; procedure TForm1.GetWListClick(Sender: TObject); begin ListView1.Clear; ListView1.Items.BeginUpdate; try EnumWindows(@EnumWindowsProc, 0); finally ListView1.Items.EndUpdate; end; end; procedure TForm1.SetWindowState(isVisible: Boolean); begin var n := ListView1.Items.Count; if (n = 0) or (ListView1.ItemIndex = -1) then begin Application.MessageBox('請先選中一個目標', '錯誤!', MB_OK + MB_ICONSTOP); exit; end; // 遍歷Listview1中的所有選中項 for var i := 0 to ListView1.Items.Count - 1 do begin var selectedItem := ListView1.items[i]; if selectedItem.Selected then begin if isVisible then begin selectedItem.SubItems[0] := '顯示'; ShowWindow(StrToInt64(selectedItem.Caption), SW_SHOW); end else begin selectedItem.SubItems[0] := '隱藏'; ShowWindow(StrToInt64(selectedItem.Caption), SW_HIDE); end; Break; end; end; end; procedure TForm1.ShowBtnClick(Sender: TObject); begin SetWindowState(TRUE); //顯示應用 end; procedure TForm1.HideBtnClick(Sender: TObject); //隱藏應用 begin SetWindowState(False); end; function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall; begin if FSortAscending then begin if ColumnIndex = 0 then Result := CompareText(Item1.Caption, Item2.Caption) else Result := CompareText(Item1.SubItems[ColumnIndex - 1], Item2.SubItems[ColumnIndex - 1]); end else begin if ColumnIndex = 0 then Result := CompareText(Item2.Caption, Item1.Caption) else Result := CompareText(Item2.SubItems[ColumnIndex - 1], Item1.SubItems[ColumnIndex - 1]); end; end; procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin if Column.Index = FSortColumn then FSortAscending := not FSortAscending else begin FSortColumn := Column.Index; FSortAscending := True; end; ListView1.CustomSort(@CustomSortProc, FSortColumn); end; function TForm1.FoundTitle(beginIndex, endIndex: Integer): Boolean; begin for var I := beginIndex to endIndex do begin // 檢查當前項的標題或子項是否包含Edit1.Text if ansiPos(LowerCase(Edit1.Text), LowerCase(ListView1.Items[I].SubItems[1])) > 0 then begin Result := True; ListView1.Items[I].Selected := True; ListView1.Items[I].MakeVisible(False); Break; end else begin ListView1.Items[I].Selected := False; end; end; end; procedure TForm1.QueryBtnClick(Sender: TObject); var I, indx: Integer; Found: Boolean; label tip; begin indx := ListView1.ItemIndex; if indx = -1 then indx := 0 else if indx <> ListView1.Items.Count - 1 then indx := indx + 1; // 遍歷ListView中的所有項 Found := FoundTitle(indx, ListView1.Items.Count - 1); if not Found then begin if indx <> 0 then begin Found := FoundTitle(0, indx - 1); if not Found then goto tip else exit; end; tip: Application.MessageBox('我翻到底了,但是沒找到匹配的項.', '錯誤!', MB_OK + MB_ICONSTOP); end; end; procedure TForm1.RadioGroup1Click(Sender: TObject); begin GetWListClick(Sender); end; procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_RETURN then QueryBtnClick(Sender); //按下回車,執行查詢 end; procedure TForm1.FormCreate(Sender: TObject); begin ListView1.ViewStyle := vsReport; //設置視圖模式 ListView1.Columns.Add.Caption := '窗口句柄'; ListView1.Columns.Add.Caption := '顯示狀態'; ListView1.Columns.Add.Caption := '應用程序名稱'; //設置列寬.-1表示匹配最長內容,-2表示匹配標題長度 ListView1.Columns[0].Width := -2; ListView1.Columns[1].Width := -2; ListView1.Columns[2].Width := -2; GetWListClick(Sender); FSortColumn := 0; FSortAscending := true; end; end.

浙公網安備 33010602011771號