C++ 虚拟桌面
C++ 打开一个虚拟桌面的代码
看不明白的地方 请查看demo: http://download.csdn.net/detail/allh45601/7224205
QQ群:103197177 C++进阶讨论;欢迎喜欢编程朋友加入进来一起讨论学习;
// Run.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "Run.h" #include <atlstr.h> #include "shellapi.h" #pragma comment("lib", "shell32.lib"); #define dim(x) (sizeof(x) / sizeof(x[0])) // 全局变量: HINSTANCE g_hInst = NULL; // 当前实例 TCHAR g_szTitle[] = TEXT("Running"); // 窗口名称 TCHAR g_szWindowClass[] = TEXT("Run-2012"); // 主窗口类名 HWND g_hWnd = NULL; CString g_strDirPath; // 注册dll批处理路径 HDESK g_hPreDesk = NULL; // 原始桌面 HDESK g_hVirtualDesk = NULL; // 虚拟桌面 HANDLE g_hEvent = NULL; LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT OnShowWindow (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT OnCreate (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT OnDestroy (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT GoVirualDesk (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT GoPreviousDesk (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); struct MSGMAP_ENTRY { UINT nMessage; LRESULT (*pfn) (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); }; struct MSGMAP_ENTRY _messageEntries[] = { WM_DESTROY, OnDestroy, WM_SHOWWINDOW, OnShowWindow, WM_CREATE, OnCreate, GO_VIRTUAL_DESK, GoVirualDesk, GO_PREVIOUS_DESK, GoPreviousDesk, }; // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); void Run (void); void GoPreDeskThread (void); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { for (int i=0; i<dim(_messageEntries); i++) { if (uMsg == _messageEntries[i].nMessage) { return (*_messageEntries[i].pfn)(hwnd, uMsg, wParam, lParam); } } return DefWindowProc(hwnd, uMsg, wParam, lParam); } LRESULT OnShowWindow (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Run(); return 0; } LRESULT OnCreate (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { // 删除WS_EX_APPWINDOW风格 LONG lStyle = ::GetWindowLong(hwnd, GWL_EXSTYLE); ::SetWindowLong(hwnd, GWL_EXSTYLE, lStyle & ~WS_EX_APPWINDOW); // 添加WS_EX_TOOLWINDOW lStyle = ::GetWindowLong(hwnd, GWL_EXSTYLE); ::SetWindowLong(hwnd, GWL_EXSTYLE, lStyle | WS_EX_TOOLWINDOW); return 0; } LRESULT OnDestroy (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { CloseDesktop(g_hVirtualDesk); CloseHandle(g_hEvent); HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, _T("Run_EXE_EXIT")); if (hEvent) { SetEvent(hEvent); } PostQuitMessage(0); return 0; } LRESULT GoVirualDesk (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { SwitchDesktop(g_hVirtualDesk); return 0; } LRESULT GoPreviousDesk (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { SwitchDesktop(g_hPreDesk); return 0; } void Run (void) { HANDLE hHand = CreateThread(NULL, 0, LPTHREAD_START_ROUTINE(GoPreDeskThread), NULL, 0, NULL); CloseHandle(hHand); STARTUPINFO si; PROCESS_INFORMATION pi; TCHAR DeskName[]= _T("RUNNING"); g_hPreDesk = GetThreadDesktop(GetCurrentThreadId()); g_hVirtualDesk = CreateDesktop( DeskName, NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL); SetThreadDesktop(g_hVirtualDesk); ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); si.lpDesktop = DeskName; si.wShowWindow = SW_SHOW; si.dwFlags = STARTF_USESHOWWINDOW; ZeroMemory( &pi, sizeof(pi) ); TCHAR strPath[MAX_PATH] = {0}; GetModuleFileName(NULL, strPath, MAX_PATH); TCHAR dir[_MAX_DIR] = {0}; TCHAR drive[_MAX_DIR] = {0}; _tsplitpath(strPath, drive, dir, NULL, NULL); CString strDirPath(drive); strDirPath += dir; strDirPath += _T("GoPreviousDeskTop.exe"); CreateProcess(strDirPath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); CStringA strBuffer; strBuffer.Format("%s %d", "GoVirtualDeskTop.exe", g_hWnd); WinExec(strBuffer, SW_SHOW); } void GoPreDeskThread (void) { while(true) { g_hEvent = NULL; g_hEvent = CreateEvent(NULL, FALSE, FALSE, _T("GoPreviousDeskTop_2012")); WaitForSingleObject(g_hEvent, INFINITE); SwitchDesktop(g_hPreDesk); } } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASS wndClass; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WindowProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = g_szWindowClass; return RegisterClass(&wndClass); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { g_hInst = hInstance; // 将实例句柄存储在全局变量中 g_hWnd = CreateWindow(g_szWindowClass, g_szTitle, WS_POPUP, -1, -1, 0, 0, NULL, NULL, hInstance, NULL); if (!g_hWnd) { return FALSE; } ShowWindow(g_hWnd, nCmdShow); UpdateWindow(g_hWnd); return TRUE; }