首页 > 编程语言 >DELPHI 隐藏程序窗口,以及TListView控件,点击标题进行排序

DELPHI 隐藏程序窗口,以及TListView控件,点击标题进行排序

时间:2024-10-20 11:32:29浏览次数:1  
标签:控件 begin end Sender TListView ListView1 DELPHI TObject procedure

设置视图:

 运行效果:

 

 

 

 

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.

 

标签:控件,begin,end,Sender,TListView,ListView1,DELPHI,TObject,procedure
From: https://www.cnblogs.com/yoooos/p/18487059

相关文章

  • Delphi10.3 RadioGroup1 组件基本用法
    https://www.cnblogs.com/jijm123/p/14237063.html1]添加Radio项目 也可用代码添加RadioGroup1.Items.Add('Delphi7');RadioGroup1.Items.Add('Delphi10.3');而不是添加RadioButton11.RadioGroup的主要属性(1)Columns:属性属性Columns:用于设置单选按钮组中按......
  • lazarus三方控件注意事项
    lazarus三方控件注意事项1)EHLIBTprintDbgrideh不能用,跟它源码,forfpc没有任何打印的代码。Tdbgrideh斑马线,设好后,无效。其它问题。。不知。可以明确:EHLIBforfpc不是全功能的,使用时要注意鉴别。2)FASTREPORT使用汉化,乱码,只能使用英文的。其它问题。。不知3)dataset-seri......
  • Winform控件基础与进阶----DataGridView
    Winform控件基础之封装一个通用的DataGridView操作类1、创建Winform项目2、创建公共控件操作文件夹3、主界面1、控件布局2、提取通用方法3、静态方法类实现4、其他工具类实现1、JsonHelper工具类实现2、TxtOperateHelper工具类实现5、数据模型实现1、创建表结构模型2......
  • 【QT】常用控件(二)
    个人主页~常用控件(一)~常用控件三、按钮类控件1、PushButtonwidget.hwidget.cpp2、RadioButton3、CheckBox四、显示类控件1、label三、按钮类控件1、PushButtonQPushButton继承自QAbstractButton,它是所有按钮的父类我们从这个按钮的属性表中可以看到,QPus......
  • Delphi:传统与现代交织的开发利器
    在软件开发的浩瀚世界中,Delphi如同一位低调的大师,虽历经岁月洗礼,却依然散发着独特的魅力,并且在当今技术快速发展的时代,展现出令人惊喜的前景。一、Delphi的历史与特点Delphi是由Borland公司推出的一种集成开发环境(IDE),以其高效、快速的开发能力而著称。它使用ObjectPasca......
  • 界面控件Telerik UI for WPF 2024 Q3亮点 - 支持禁用数据过滤等
    TelerikUIforWPF拥有超过100个控件来创建美观、高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序。UIforWPF支持MVVM、触摸等,创建的应用程序可靠且结构良好,非常容易维护,其直观的API将无缝地集成VisualStudio工具箱中。本文将介绍界面组件TelerikUIforWPF在今......
  • 界面控件DevExtreme中文教程 - 如何与Amazon S3和Azure Blob存储集成?
    DevExtreme拥有高性能的HTML5/JavaScript小部件集合,使您可以利用现代Web开发堆栈(包括React,Angular,ASP.NETCore,jQuery,Knockout等)构建交互式的Web应用程序。从Angular和Reac,到ASP.NETCore或Vue,DevExtreme包含全面的高性能和响应式UI小部件集合,可在传统Web和下一代移动应用程序中......
  • c# .net8 winform 嵌入 wpf 控件
    .netframework例子很多,但.netcore的很难找,经过和gpt很长时间的沟通,完成了。关键点是vs2022似乎还不支持设计时的ElementHost,所以必须通过代码初始化。1.新建一个wpf控件库2.创建一个导圆角的TextBox,控件名为 UserControl13.winform项目添加对wpf控件库项目......
  • Delphi 中禁止 StringGrid 单元格被选中
    Delphi中禁止StringGrid单元格被选中环境Windows1123H2Delphi12Update1使用Delphi的StringGrid展示数据而不愿意某个单元格被选中时,曾经的手段是把选中位置调整到无效位置从而实际上使得单元格无法被选中。阅读文档偶然发现OnSelectCell事件提供了很简单也......
  • Delphi10.3控件水平,垂直 均匀排列
    用鼠标框选中控件, 再右键  先水平左对齐, 再垂直间隔相同  同理,水平均匀排列也一样,先顶对对齐再水平间隔相同  ......