Windows多桌面切换(CreateDesktop,SwitchDesktop函数)

unit Unit1;  

interface  

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, Buttons, ExtCtrls,
TrayIconForm, AppEvnts; type
TShareInfo = record
Actived : array [ .. ] of boolean;
end;
PShareInfo = ^TShareInfo; TForm1 = class(TForm)
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
N41: TMenuItem;
BitBtn2: TBitBtn;
Panel1: TPanel;
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Label3: TLabel;
Label4: TLabel;
N4: TMenuItem;
PopupMenu1: TPopupMenu;
ApplicationEvents1: TApplicationEvents;
procedure N3Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure TrayIcon1DblClick(Sender: TObject);
private
{ Private declarations }
RunningOnDesktop: integer;
Failed : boolean;
procedure OnHotKey(var Msg: TMessage); message WM_HOTKEY;
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm}
{$R UAC.res} procedure SwitchToDesktop(number: integer);
var name: string;
DsktpHandle: HDESK;
SysPath: array[..MAX_PATH] of char;
PPath: PChar;
StrPath: string;
len: integer;
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
begin
if number = then
name := 'Default'
else
name := 'NewDesktop' + inttostr(number); ZeroMemory(@si, sizeof(STARTUPINFO));
si.cb := sizeof(STARTUPINFO);
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.wShowWindow := SW_SHOW;
si.lpDesktop := PChar(Name); DsktpHandle := OpenDesktop(pchar(Name),
DF_ALLOWOTHERACCOUNTHOOK, true,
DESKTOP_CREATEMENU or DESKTOP_CREATEWINDOW or
DESKTOP_ENUMERATE or DESKTOP_HOOKCONTROL or
DESKTOP_JOURNALPLAYBACK or
DESKTOP_JOURNALRECORD or DESKTOP_READOBJECTS or
DESKTOP_SWITCHDESKTOP or DESKTOP_WRITEOBJECTS);
if DsktpHandle = then
begin
// 桌面不存在,创建桌面
DsktpHandle := CreateDesktop(PChar(Name), nil, nil,
DF_ALLOWOTHERACCOUNTHOOK,
DESKTOP_CREATEMENU or DESKTOP_CREATEWINDOW or
DESKTOP_ENUMERATE or DESKTOP_HOOKCONTROL or
DESKTOP_JOURNALPLAYBACK or
DESKTOP_JOURNALRECORD or DESKTOP_READOBJECTS or
DESKTOP_SWITCHDESKTOP or DESKTOP_WRITEOBJECTS,
nil);
if DsktpHandle = then
begin
ShowMessage('打开桌面失败! ' + SysErrorMessage(GetLastError));
exit;
end; PPath := @SysPath;
len := GetWindowsDirectory(@SysPath, MAX_PATH);
SetString(StrPath, PPath, len);
if (not CreateProcess(PChar(StrPath + '/explorer.exe'), nil, nil, nil, True, , nil, nil, si, pi)) then
begin
ShowMessage('进程创建失败! ' + SysErrorMessage(GetLastError));
CloseDeskTop(DsktpHandle);
exit;
end; end; SwitchDesktop(DsktpHandle);
end; procedure TForm1.N3Click(Sender: TObject);
begin
Close;
end; procedure TForm1.N11Click(Sender: TObject);
begin
if Sender is TMenuItem then
SwitchToDesktop((Sender as TMenuItem).Tag);
end; procedure TForm1.N2Click(Sender: TObject);
begin
Show;
end; procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
Hide;
end; procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Hide;
end; procedure TForm1.N4Click(Sender: TObject);
begin
TrayForm.Show;
end; procedure TForm1.OnHotKey(var Msg: TMessage);
begin
if Msg.WParam > then
if Msg.WParam <= then
SwitchToDesktop(Msg.WParam); //if Msg.WParam=5 then
// TrayIcon1.Visible := not TrayIcon1.Visible;
end; procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
Show;
end; procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, , MOD_CONTROL, VK_F1);
RegisterHotKey(Handle, , MOD_CONTROL, VK_F2);
RegisterHotKey(Handle, , MOD_CONTROL, VK_F3);
RegisterHotKey(Handle, , MOD_CONTROL, VK_F4);
RegisterHotKey(Handle, , MOD_CONTROL, VK_F5);
end; end.

参考:http://www.cnblogs.com/key-ok/p/3417735.html

上一篇:CRLF攻击的一篇科普:新浪某站CRLF Injection导致的安全问题(转)


下一篇:mysql格式化整数类型时间生成年月日时分秒格式(long或string接收)