[小工具]切换桌面的小程序。
一年前写的小工具,我一直在使用,感觉还是很方便的。
程序以简单实用为目的,启动后不显示任何界面,只会再创建一个桌面,用WIN+1和WIN+2组合键在两个桌面间切换。一个桌面用于工作,一个桌面用于娱乐,互不干扰。
源代码及可执行文件下载:PharaohDesktop.zip
Delphi源代码:
//创建一个新桌面。
//By pharaoh.cnblogs.com
program myDesktop;
uses
Windows, Messages;
// {$R *.res}
const
szAppName : PChar = 'myDesktop';
var
wc : WNDCLASS; //主窗口类
HMainWnd : HWND; //主窗口句柄
AMsg : MSG; //主窗口消息
hDesktopCurrent:HDESK; //当前桌面
hmyDesktop:HDESK; //新桌面
sa:TSecurityAttributes;
function WndProc(AWnd:HWND; message:UINT; wp:WPARAM; lp:LPARAM):LRESULT;stdcall;
begin
Result := 0;
case message of
WM_DESTROY:
begin
SwitchDesktop(hDesktopCurrent);
PostQuitMessage(0);
end;
WM_HOTKEY:
if(7777 = wp) then
begin
SwitchDesktop(hDesktopCurrent);
PostQuitMessage(0);
end
else if(7778 = wp) then
SwitchDesktop(hDesktopCurrent)
else if(7779 = wp) then
SwitchDesktop(hmyDesktop);
else
Result := DefWindowProc(AWnd, message, wp, lp);
end;
end;
procedure StartMyExplore;
var
sui:TStartupInfo;
pi:TProcessInformation;
begin
FillChar(sui,sizeof(TStartupInfo),#0);
sui.cb := sizeof(TStartupInfo);
sui.lpDesktop := pchar('pharaoh.cnblogs.com');
CreateProcess(nil,'explorer',nil,nil,true,CREATE_DEFAULT_ERROR_MODE or CREATE_SEPARATE_WOW_VDM ,
nil,nil,sui,pi);
end;
begin
with wc do begin
style := CS_VREDRAW or CS_HREDRAW;
lpfnWndProc := @WndProc;
cbClsExtra := 0;
cbWndExtra := 0;
hIcon := LoadIcon(0, IDI_APPLICATION);
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := GetSysColorBrush(COLOR_WINDOW);
hInstance := HInstance;
lpszMenuName := nil;
lpszClassName := szAppName;
end;
RegisterClass(wc);
//当前桌面
hDesktopCurrent := GetThreadDesktop(GetCurrentThreadId());
//创建新桌面
hmyDesktop := 0;
hmyDesktop := OpenDesktop('pharaoh.cnblogs.com',0,false,0);
if hmyDesktop <> 0 then
begin
CloseDesktop(hmyDesktop);
end;
sa.bInheritHandle := true;
sa.nLength := sizeof(TSecurityAttributes);
sa.lpSecurityDescriptor := nil;
hmyDesktop := CreateDesktop('pharaoh.cnblogs.com',nil,nil,0,MAXIMUM_ALLOWED,nil);
if hmyDesktop = 0 then exit;
if not SetThreadDesktop(hmyDesktop) then
begin
end;
SwitchDesktop(hmyDesktop);
//创建主窗口
HMainWnd := CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT),
HWND_DESKTOP, 0,
HInstance, nil);
//注册热键
if(not RegisterHotKey(HMainWnd, 7777, MOD_WIN, ord('0'))) then exit;
if(not RegisterHotKey(HMainWnd, 7778, MOD_WIN, ord('1'))) then exit;
if(not RegisterHotKey(HMainWnd, 7779, MOD_WIN, ord('2'))) then exit;
//在新桌面启动Explore
StartMyExplore;
SwitchDesktop(hDesktopCurrent);
while GetMessage(AMsg, 0, 0, 0) do begin
TranslateMessage(AMsg);
DispatchMessage(AMsg);
end;
SwitchDesktop(hDesktopCurrent);
end.