FindWindow查找窗口,SHowWindow隱藏和顯示窗口。
我下面的例子程序搜索所有窗口,並且通過右鍵點擊系統圖標進行顯示/隱藏的切換,由於使用了TRAYICON空間,妳可能編譯不能通過,但是可以了解相關語句的語法:
unit?winmanu;
interface
uses
Windows,?Messages,?SysUtils,?Variants,?Classes,?Graphics,?Controls,?Forms, Dialogs,?Menus,?TrayIcon;type
TForm1?=?class(TForm)TrayIcon1:?TTrayIcon;
PopupMenu1:?TPopupMenu;
Exit1:?TMenuItem;
N1:?TMenuItem;
procedure?Exit1Click(Sender:?TObject);
procedure?FormCreate(Sender:?TObject);
procedure?PopupMenu1Popup(Sender:?TObject);
private{?Private?declarations?}
procedure?ShowHideWind(Sender:?TObject);
public{?Public?declarations?}
end;var
Form1:?TForm1;implementation
{$R?*.dfm}
procedure?TForm1.Exit1Click(Sender:?TObject);
begin
Closeend;
procedure?TForm1.FormCreate(Sender:?TObject);
begin
TrayIcon1.Icon?:=?Application.Icon?;end;
procedure?TForm1.PopupMenu1Popup(Sender:?TObject);
var
i:?integer; hCurrentWindow,?hActWind:?HWnd; szText:?array?[0..255]?of?char; Item:?TMenuItem;begin
i:=PopupMenu1.Items.Count-1; while?i>=0?do?begin//Application.MessageBox(PChar(PopupMenu1.Items[i].Caption),'Delete?Menu?Item');
if?(PopupMenu1.Items[i].Caption?<>?'E&xit')and(PopupMenu1.Items[i].Caption?<>?'-')?then?PopupMenu1.Items.Delete(i);
dec(i);
end; hActWind:=FindWindow('Progman',nil); hCurrentWindow?:=?GetWindow(hActWind,?GW_HWNDFIRST); while?hCurrentWindow?<>?0?do beginif?(GetWindowText(hCurrentWindow,?@szText,?sizeof(szText)-1)>0)?and
(szText<>'Default?IME')and?IsWindow(hCurrentWindow)?then
begin
Item:=TMenuItem.Create(PopupMenu1); Item.Caption:=szText; Item.Checked:=IsWindowVisible(hCurrentWindow); Item.Tag:=hCurrentWindow; Item.OnClick:=ShowHideWind; PopUpMenu1.Items.Add(Item);end;
hCurrentWindow:=GetWindow(hCurrentWindow,?GW_HWNDNEXT);
end;end;
procedure?TForm1.ShowHideWind(Sender:?TObject);
begin
with?Sender?as?TMenuItem?do if?Checkedthen?ShowWindow(Tag,SW_HIDE)
else?SHowWindow(Tag,SW_SHOW);
end;
end.