unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, Buttons, ExtCtrls, TrayIconForm, AppEvnts; type TShareInfo = record Actived : array [ 1..4 ] of boolean; end; PShareInfo = ^TShareInfo; TForm1 = classTForm) 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 N3ClickSender: TObject); procedure N2ClickSender: TObject); procedure BitBtn2ClickSender: TObject); procedure N4ClickSender: TObject); procedure FormCreateSender: TObject); procedure ApplicationEvents1MinimizeSender: TObject); procedure N11ClickSender: TObject); procedure TrayIcon1DblClickSender: TObject); private { Private declarations } RunningOnDesktop: integer; Failed : boolean; procedure OnHotKeyvar Msg: TMessage); message WM_HOTKEY; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} {$R UAC.res} procedure SwitchToDesktopnumber: integer); var name: string; DsktpHandle: HDESK; SysPath: array[1..MAX_PATH] of char; PPath: PChar; StrPath: string; len: integer; si: STARTUPINFO; pi: PROCESS_INFORMATION; begin if number = 1 then name := 'Default' else name := 'NewDesktop' + inttostrnumber); ZeroMemory@si, sizeofSTARTUPINFO)); si.cb := sizeofSTARTUPINFO); si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; si.wShowWindow := SW_SHOW; si.lpDesktop := PCharName); DsktpHandle := OpenDesktoppcharName), 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 = 0 then begin // 桌面不存在,创建桌面 DsktpHandle := CreateDesktopPCharName), 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 = 0 then begin ShowMessage'打开桌面失败! ' + SysErrorMessageGetLastError)); exit; end; PPath := @SysPath; len := GetWindowsDirectory@SysPath, MAX_PATH); SetStringStrPath, PPath, len); if not CreateProcessPCharStrPath + '/explorer.exe'), nil, nil, nil, True, 0, nil, nil, si, pi)) then begin ShowMessage'进程创建失败! ' + SysErrorMessageGetLastError)); CloseDeskTopDsktpHandle); exit; end; end; SwitchDesktopDsktpHandle); end; procedure TForm1.N3ClickSender: TObject); begin Close; end; procedure TForm1.N11ClickSender: TObject); begin if Sender is TMenuItem then SwitchToDesktopSender as TMenuItem).Tag); end; procedure TForm1.N2ClickSender: TObject); begin Show; end; procedure TForm1.ApplicationEvents1MinimizeSender: TObject); begin Hide; end; procedure TForm1.BitBtn2ClickSender: TObject); begin Hide; end; procedure TForm1.N4ClickSender: TObject); begin TrayForm.Show; end; procedure TForm1.OnHotKeyvar Msg: TMessage); begin if Msg.WParam >0 then if Msg.WParam <=4 then SwitchToDesktopMsg.WParam); //if Msg.WParam=5 then // TrayIcon1.Visible := not TrayIcon1.Visible; end; procedure TForm1.TrayIcon1DblClickSender: TObject); begin Show; end; procedure TForm1.FormCreateSender: TObject); begin RegisterHotKeyHandle, 1, MOD_CONTROL, VK_F1); RegisterHotKeyHandle, 2, MOD_CONTROL, VK_F2); RegisterHotKeyHandle, 3, MOD_CONTROL, VK_F3); RegisterHotKeyHandle, 4, MOD_CONTROL, VK_F4); RegisterHotKeyHandle, 5, MOD_CONTROL, VK_F5); end; end.
参考:http://www.cnblogs.com/key-ok/p/3417735.html