C++ 虚拟桌面
阅读原文时间:2023年07月10日阅读:1

C++ 打开一个虚拟桌面的代码

看不明白的地方 请查看demo: http://download.csdn.net/detail/allh45601/7224205

QQ群:103197177 C++进阶讨论;欢迎喜欢编程朋友加入进来一起讨论学习;

// Run.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "Run.h"
#include

#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;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章