完全控制你的Windows桌面

    技术2022-05-11  108

    完全控制你的Windows桌面    对于Windows的桌面界面相信大家已经十分熟悉了,占据屏幕大半部分的是桌面,在上面排列的是桌面图标。任务栏一般位于桌面的下面,也可以在桌面其它边缘。在最左边是“开始按钮”,接下来是“快速启动按钮”区、程序按钮区,再下来是任务栏图标区,在上面一般会有音量调节图标和输入法调节图标和时钟等。    本文首先介绍如何隐藏任务栏中的上面介绍的部分。我们知道利用Windows的Api函数ShowWindow可以隐藏或者显示窗口,关键是如何得到窗口的句柄。在Windows下的每一个窗口不但有一个窗口句柄标示窗口,还有一个称为类名的字符串标示窗口。如果知道窗口的类名,通过FindWindow函数就可以获得窗口的句柄。而Windows桌面本身就是一个窗口,桌面图标区、任务栏以及任务栏下的开始按钮等都是它的子窗口。我们可以通过FindWindowEx函数来寻找这些窗口。再利用ShowWindow函数隐藏或显示窗口。下面通过一个Delphi的范例来演示如何控制任务栏。    首先建立一个新的Delphi工程,然后在Form1中加入7个CheckBox控件,然后在Form1中添加下面的代码:

    unit Unit1;

    interface

    uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls;

    type  TForm1 = class(TForm)    CheckBox1: TCheckBox;    CheckBox2: TCheckBox;    CheckBox3: TCheckBox;    CheckBox4: TCheckBox;    CheckBox5: TCheckBox;    CheckBox6: TCheckBox;    CheckBox7: TCheckBox;    procedure FormCreate(Sender: TObject);  private    procedure CheckButtonClick(Sender:TObject);    { Private declarations }  public    { Public declarations }  end;Constfile://定义不同窗口的类名sTrayWindow = 'Shell_TrayWnd';sTrayNotify = 'TrayNotifyWnd';sStartButton = 'Button';sAppSwitchBar = 'ReBarWindow32';sAppSwitch = 'MSTaskSwWClass';sAppIcon = 'ToolbarWindow32';sTrayClock = 'TrayClockWClass';sDesktopIcon = 'ShellDll_DefView';sProgman = 'Progman';

    var  Form1: TForm1;  wnd:Integer;

    implementation

    {$R *.DFM}

    procedure TForm1.FormCreate(Sender: TObject);begin  CheckBox1.Caption := '隐藏任务栏';  CheckBox1.OnClick := CheckButtonClick;  CheckBox2.Caption := '隐藏开始按钮';  CheckBox2.OnClick := CheckButtonClick;  CheckBox3.Caption := '隐藏任务栏图标';  CheckBox3.OnClick := CheckButtonClick;  CheckBox4.Caption := '隐藏程序按钮';  CheckBox4.OnClick := CheckButtonClick;  CheckBox5.Caption := '隐藏任务栏时钟';  CheckBox5.OnClick := CheckButtonClick;  CheckBox6.Caption := '隐藏桌面图标';  CheckBox6.OnClick := CheckButtonClick;  CheckBox7.Caption := '隐藏快速运行图标';  CheckBox7.OnClick := CheckButtonClick;end;

    file://7个CheckBox控件的Click处理函数procedure TForm1.CheckButtonClick(Sender:TObject);var  i:Integer;begin  file://找到任务栏窗口的窗口句柄  wnd := FindWindow(sTrayWindow, nil);

      if (TCheckBox(Sender).Name)=  'CheckBox2' then    wnd := FindWindowEx(wnd, 0, sStartButton, nil);

      if (TCheckBox(Sender).Name)=  'CheckBox3' then    wnd := FindWindowEx(wnd, 0, sTrayNotify, nil);

      if (TCheckBox(Sender).Name)=  'CheckBox4' then begin    wnd := FindWindowEx(wnd, 0, sAppSwitchBar, nil);    wnd := FindWindowEx(wnd, 0, sAppSwitch, nil);  end;

      if (TCheckBox(Sender).Name)=  'CheckBox5' then begin    wnd := FindWindowEx(wnd, 0, sTrayNotify, nil);    wnd := FindWindowEx(wnd, 0, sTrayClock, nil);  end;

      if (TCheckBox(Sender).Name)=  'CheckBox6' then begin    wnd := FindWindow(sProgman, nil);    wnd := FindWindowEx(wnd, 0, sDesktopIcon, nil);  end;

      if (TCheckBox(Sender).Name)=  'CheckBox7' then begin    wnd := FindWindowEx(wnd, 0, sAppSwitchBar, nil);    wnd := FindWindowEx(wnd, 0, sAppIcon, nil);  end;

      if TCheckBox(Sender).Checked then    ShowWindow (wnd, SW_HIDE)  Else    ShowWindow (wnd, SW_SHOW);end;end.

        运行程序,分别点击不同的选择框,可以分别隐藏任务栏或任务栏上的不同部分。

        下面再来介绍如何操控桌面图标。设置图标文本的背景和颜色以及设置图标的排列。通过上面的介绍上面我们知道,Windows的桌面也是一个窗口,不同的它是一个ListView类窗口,对于ListView类窗口,有一系列的以LVM_开头的消息,通过Windows API函数SendMessage向ListView类窗口发送这些消息就可以控制窗口的一些属性,而且在Delphi中还有一系列的以ListView_开头的函数,这些函数可以代替LVM_类消息。    具体的范例如下:首先建立一个新的Delphi工程,然后在Form1中加入两个CommandButton控件,然后在Form1中加入以下的代码:unit Unit1;

    interface

    uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  ComCtrls, StdCtrls,Commctrl;

    type  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;

    var  Form1: TForm1;

    implementation

    {$R *.DFM}procedure SetDesktopIconColor(Forground, Background: TColor; Trans: Boolean);var  Window: HWND;begin  Window := FindWindow('Progman', 'Program Manager');  file://找到桌面窗口  Window := FindWindowEx(Window, HWND(nil), 'SHELLDLL_DefView', '');  file://找到放置桌面图标的ListView窗口  Window := FindWindowEx(Window, HWND(nil), 'SysListView32', '');

      if Trans then         file://设置透明的文字背景色    ListView_SetTextBkColor(Window, $ffffffff) // back color  else                  file://设置不透明的文字背景色    ListView_SetTextBkColor(Window, Background); // back color

      ListView_SetTextColor(Window, Forground); // foreground color  file://重新绘制桌面图标  ListView_RedrawItems(Window, 0, ListView_GetItemCount(Window) - 1);  UpdateWindow(Window);   file://重新绘制窗口end;

    procedure SetDeskTopIconArr(iWidth,iHeight:Integer);var  Window: HWND;  i,i1,i2,iCount:integer;begin  Window := FindWindow('Progman', 'Program Manager');  Window := FindWindowEx(Window, HWND(nil), 'SHELLDLL_DefView', '');  Window := FindWindowEx(Window, HWND(nil), 'SysListView32', '');

      file://设置图标与边界的距离。  i1:=20;i2:=20;

      file://获得桌面图标的个数  iCount:=ListView_GetItemCount(Window)-1;  for i:=0 to iCount do begin    file://设置图标位置    ListView_SetItemPosition(Window,i,i1,i2);    i1:=i1+iWidth;    if i1>(Screen.Width-32) then begin      i1:=20;      i2:=i2+iHeight;    end;  end;  ListView_RedrawItems(Window, 0, ListView_GetItemCount(Window) - 1);  UpdateWindow(Window);end;

    procedure SetDefaultIconColors;var  Kind: Integer;  Color: TColor;begin  Kind := COLOR_DESKTOP;  Color := GetSysColor(COLOR_DESKTOP);  SetSysColors(1, Kind, Color);end;

    procedure TForm1.Button1Click(Sender: TObject);begin  file://你可以改变clWhite,clBlack为其它的颜色值看看  file://图标文本颜色的变化  SetDesktopIconColor(clWhite,clBlack,True);end;

    procedure TForm1.Button2Click(Sender: TObject);begin  file://设置图标的间距为100个像素  SetDeskTopIconArr(100,100);end;

    procedure TForm1.FormCreate(Sender: TObject);begin  Button1.Caption := '设置图标文本颜色';  Button2.Caption := '设置图标排列';end;

    end.

        在上面的程序中,函数SetDesktopIconColor设置图标文本的前景色、背景色和透明,参数ForeGroundBackGround分别指定文本的前景色和背景色,参数Trans指定文本的背景是否透明(如果有背景图案的话)。函数SetDeskTopIconArr排列桌面图标,参数iWidth,iHeight分别指定图标之间的横向纵向距离。如果要使SetDeskTopIconArr函数生效,就需要将桌面图标的自动排列选项去掉。另外ListView类还有其它的控制消息利用这些消息可以控制更多的桌面图标选项。有兴趣的朋友可以察看MSDN库。    以上程序由Delphi5编写,在Windows98 Windows2000下运行通过。

     


    最新回复(0)