C++ 建立虛擬桌面
阿新 • • 發佈:2019-01-02
在C*DeskDlg.h檔案下private下建立變數:
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo;
HDESK hDesktop;
HDESK hOriginalThread;
HDESK hOriginalInput;
在C*DeskDlg.cpp下新增函式
LRESULT CVirtualDeskDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your message handler code here and/or call default
if (wParam == 521) //訊息為 Alt + X時
{
hDesktop = CreateDesktop(L"Virtual", NULL, NULL, 0, GENERIC_ALL , NULL);
//初始化建立程序必須的結構體
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.lpDesktop = L"Virtual";
//記錄原始桌面的控制代碼
hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); //此句非常重要,否則無法退出
//建立桌面外殼程序
TCHAR tcCmd[MAX_PATH] = L"explorer";
CreateProcess(NULL, tcCmd, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo);
//完成初始化,以後不會再次被呼叫,防止開啟虛擬桌面出現資源管理器
SetThreadDesktop(hDesktop); //設定桌面活動焦點是虛擬桌面
SwitchDesktop(hDesktop); //切換到虛擬桌面
//GetDlgItem(IDOK)->EnableWindow(FALSE); //設定初始化按鈕失效,只允許熱鍵操作
RegisterHotKey(m_hWnd, 1314, MOD_WIN, 'Z'); //設定熱鍵為 Alt+Z
}
if (wParam == 1314)
{
SetThreadDesktop(hOriginalThread);
SwitchDesktop(hOriginalInput);
CloseDesktop(hDesktop); //關閉虛擬桌面
}
return TRUE;
}
void CVirtualDeskDlg::OnBnClickedCancel()
{//關閉程式
// TODO: Add your control notification handler code here
UnregisterHotKey(m_hWnd, 1314);
UnregisterHotKey(m_hWnd, 521);
//關閉建立的explorer
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.dwProcessId); //獲取虛擬桌面的explorer控制代碼
if(hProcess)
{
TerminateProcess(hProcess, 1); //結束虛擬桌面的explorer
}
CloseHandle(hProcess);
CDialogEx::OnCancel();
}
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo;
HDESK hDesktop;
HDESK hOriginalThread;
HDESK hOriginalInput;
在C*DeskDlg.cpp下新增函式
LRESULT CVirtualDeskDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your message handler code here and/or call default
if (wParam == 521) //訊息為 Alt + X時
{
hDesktop = CreateDesktop(L"Virtual", NULL, NULL, 0, GENERIC_ALL , NULL);
//初始化建立程序必須的結構體
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.lpDesktop = L"Virtual";
//記錄原始桌面的控制代碼
hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); //此句非常重要,否則無法退出
//建立桌面外殼程序
TCHAR tcCmd[MAX_PATH] = L"explorer";
CreateProcess(NULL, tcCmd, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo);
//完成初始化,以後不會再次被呼叫,防止開啟虛擬桌面出現資源管理器
SetThreadDesktop(hDesktop); //設定桌面活動焦點是虛擬桌面
SwitchDesktop(hDesktop); //切換到虛擬桌面
//GetDlgItem(IDOK)->EnableWindow(FALSE); //設定初始化按鈕失效,只允許熱鍵操作
RegisterHotKey(m_hWnd, 1314, MOD_WIN, 'Z'); //設定熱鍵為 Alt+Z
}
if (wParam == 1314)
{
SetThreadDesktop(hOriginalThread);
SwitchDesktop(hOriginalInput);
CloseDesktop(hDesktop); //關閉虛擬桌面
}
return TRUE;
}
void CVirtualDeskDlg::OnBnClickedCancel()
{//關閉程式
// TODO: Add your control notification handler code here
UnregisterHotKey(m_hWnd, 1314);
UnregisterHotKey(m_hWnd, 521);
//關閉建立的explorer
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.dwProcessId); //獲取虛擬桌面的explorer控制代碼
if(hProcess)
{
TerminateProcess(hProcess, 1); //結束虛擬桌面的explorer
}
CloseHandle(hProcess);
CDialogEx::OnCancel();
}