程序控制桌面切换

用向导建立一个Win32程序“hello, world!”,然后用下面的内容替换整个CPP文件内容就可以了

#include “stdafx.h”

#include “resource.h”

#include “ShellApi.h”

HINSTANCE hInst = NULL;

HDESK hDesktopCurrent; HDESK hDesktopLlx;

LONG APIENTRY WndProc( HWND hWnd, UINT message, // type of message WPARAM wParam, // additional information LPARAM lParam) // additional information { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc;

switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps;); // TODO: Add any drawing code here… RECT rt; GetClientRect(hWnd, &rt;); //DrawText(hdc, szHello, strlen(szHello), &rt;, DT_CENTER); EndPaint(hWnd, &ps;); break; / case WM_DESTROY: PostQuitMessage(0); break; /// case WM_LBUTTONDOWN: break; case WM_HOTKEY: if(7777 == wParam) { PostQuitMessage(0); } else if(7778 == wParam) { SwitchDesktop(hDesktopCurrent); } else if(7779 == wParam) { SwitchDesktop(hDesktopLlx); } break; case WM_QUIT: case WM_DESTROY: SwitchDesktop(hDesktopCurrent); return DefWindowProc(hWnd, message, wParam, lParam); default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }

void StartMyExplore(void) { STARTUPINFO sui; // Process startup info PROCESS_INFORMATION pi; // info returned from CreateProcess // // Most sui members will be 0 // ZeroMemory ((PVOID)&sui;, sizeof(sui));

sui.cb = sizeof (sui); // // Need the lpDesktop member so the new process runs on this desktop // The lpDesktop member was reserved in previous versions of NT // sui.lpDesktop = _T(“llx”); CreateProcess (NULL, // image name “explorer”, // command line NULL, // process security attributes NULL, // thread security attributes TRUE, // inherit handles CREATE_DEFAULT_ERROR_MODE|CREATE_SEPARATE_WOW_VDM, NULL, // environment block NULL, // current directory &sui;, // STARTUPINFO π); // PROCESS_INFORMATION }

int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, “IDI_SETTHREADDESKTOP”); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = “lilinxiang”; if(!RegisterClass(&wc;)) { return TRUE; }

hDesktopCurrent = NULL; hDesktopCurrent = GetThreadDesktop(GetCurrentThreadId());

hDesktopLlx = NULL; hDesktopLlx = OpenDesktop(“llx”, 0, FALSE, NULL); if (hDesktopLlx != NULL) { CloseDesktop(hDesktopLlx); }

SECURITY_ATTRIBUTES sa; sa.bInheritHandle = TRUE; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL;

hDesktopLlx = CreateDesktop(“llx”, NULL, NULL,0,MAXIMUM_ALLOWED, NULL); if(hDesktopLlx == NULL) { return 0; }

if(!SetThreadDesktop(hDesktopLlx)) { char szError[256] = {0}; ltoa( (long)(GetLastError()) , szError, 10); } SwitchDesktop(hDesktopLlx);

HWND hWnd = NULL; hWnd = CreateWindow (“lilinxiang”, “hello, world!”, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if(NULL == hWnd) { return TRUE; }

//register hotkey for exit this desktop or switch to another desktop //ShowWindow(hWnd, SW_SHOW); //UpdateWindow(hWnd); if(!RegisterHotKey(hWnd, 7777, MOD_CONTROL, ‘Q’)) {//exit process return TRUE; } if(!RegisterHotKey(hWnd, 7778, MOD_CONTROL | MOD_SHIFT, ‘Q’)) {//switch to new desktop return TRUE; } if(!RegisterHotKey(hWnd, 7779, MOD_CONTROL | MOD_SHIFT, ‘W’)) {//switch to original desktop return TRUE; }

StartMyExplore();

MSG msg; while (GetMessage(&msg;, NULL, 0, 0)) { TranslateMessage(&msg;);// Translates virtual key codes DispatchMessage(&msg;); // Dispatches message to window }

SwitchDesktop(hDesktopCurrent); return TRUE; }

// : ) 好东西噢

用CreateDesktop新建一个桌面,什么都没有的桌面,然后在新桌面环境中运行了explorer所以就有了和windows一样功能的桌面,这个时候你就有了两个桌面了,下面要做的就是用SwitchDesktop切换不同的桌面了

在不同桌面中打开的程序在其他桌面的任务栏包括系统托盘上不可见,不过任务管理器中还是可见的这样你就非常方便的做很多事了,很多… 而且还可以非常快的转到正常状态上可以让会责骂你的人比如老板什么的无话可说,因为他们什么也看不到 : )

或者你也可以给你用户定制一个个性化的桌面。改改上面的代码可以得到你要的效果的。

支持原创技术分享,据说打赏我的人,都找到了女朋友!