隐藏窗口小程序源码 (DELPHI 7)

来源:互联网 发布:java final关键字 对象 编辑:程序博客网 时间:2024/06/05 12:41
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ComCtrls, CheckLst,shellAPI;const  WM_NID=WM_USER+1000;type  TForm1 = class(TForm)    GroupBox1: TGroupBox;    CheckListBox1: TCheckListBox;    CheckBox1: TCheckBox;    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    procedure CheckListBox1ClickCheck(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure CheckBox1Click(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private     procedure WMSysCommand(var Message:Tmessage); message WM_SYSCOMMAND;     procedure WMNID(var Message:TMessage); message WM_NID;    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;  g_hwnd:array[1..100] of DWORD;  x:integer;  NotifyIcon:TNotifyIconData;  //定义一个全局变量implementation{$R *.dfm}/////////////////////////////////////////////////////////////////////////////////    EnumWindows 回调函数///////////////////////////////////////////////////////////////////////////////function g_EnumWindowsProc(hwnd:HWND;lParam:LPARAM):Boolean;stdcall;var  buf:array[Byte] of Char;begin  GetWindowText(hwnd,buf,sizeof(buf));  if (IsWindowVisible(hwnd)) and (buf <> '') then  begin    if hwnd <>Form1.Handle then    //排除自己的窗口,防止把自己的窗口隐藏了    begin      Form1.CheckListBox1.Items.Add(inttostr(hwnd)+ ':     ' +buf );      g_hwnd[x]:=hwnd;             //数组下标从1开始.      x:=x+1;    end;  end;  Result:=True;end;////////////////////////////////////////////////////////////////////    CheckListBox 复选框被选中时隐藏窗口//////////////////////////////////////////////////////////////////procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);begin  if CheckListBox1.ItemIndex = -1  then exit;  if CheckListBox1.checked[checklistbox1.itemindex]=true then  begin    ShowWindow(g_hwnd[checklistbox1.ItemIndex+1],SW_HIDE);  end  else if checklistbox1.checked[checklistbox1.ItemIndex]=false then  begin    showwindow(g_hwnd[checklistbox1.ItemIndex+1],SW_RESTORE);  end;end;/////////////////////////////////////////////////////////////////////    创建应用程序窗口事件///////////////////////////////////////////////////////////////////procedure TForm1.FormCreate(Sender: TObject);begin  //遍历窗口,加入CheckListBox 中  x:=1;  EnumWindows(@g_EnumWindowsProc,0);  checkbox1.Checked:=True;  //-------------------------------------------------------加入系统托盘图标  with NotifyIcon do begin       cbSize:=Sizeof(TNotifyIconData);       Wnd:=Form1.Handle;       uID:=1;       uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;       uCallbackMessage:=WM_NID;       hIcon:=application.Icon.Handle;       szTip:='HideWindow V0.1';  end;  Shell_NotifyIcon(NIM_ADD,@NotifyIcon);  //---------------------------------------------------end;////////////////////////////////////////////////////////////////////////////    复选框 OnTop 点击事件/////////////////////////////////////////////////////////////////////////procedure TForm1.CheckBox1Click(Sender: TObject);begin  if Checkbox1.Checked =True then  begin    //窗口总在最前    SetWindowPos(form1.Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE or SWP_NOSIZE);  end  else if Checkbox1.Checked=False then  begin    SetWindowPos(form1.Handle,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE or SWP_NOSIZE);  end;end;/////////////////////////////////////////////////////////////////////////////  消息处理过程procedure TForm1.WMSysCommand(var Message:TMessage);begin    //处理最小化消息,隐藏窗口    if Message.WParam =SC_MINIMIZE then    begin      Form1.Visible:=FALSE;      //ShowWindow(Form1.Handle,SW_HIDE);   //隐藏窗口    end    else    begin      DefWindowProc(Form1.Handle,Message.Msg,Message.WParam ,Message.LParam );    end;end;//----------------------------------------------------------------------------//    处理系统托盘消息procedure Tform1.WMNID(var message:TMessage);begin    case Message.LParam of            WM_LBUTTONUP: Form1.visible:=True;    end;end;procedure TForm1.FormDestroy(Sender: TObject);begin    Shell_NotifyIcon(NIM_DELETE,@NotifyIcon);end;end.

 

程序在下面图片中,点击图片,选择另存为,用压缩包软件打开刚才保存的图片,解压就看到小程序了.


原创粉丝点击