发布网友 发布时间:2022-04-25 04:01
共1个回答
热心网友 时间:2022-05-02 20:18
在 Windows Api 中提供了 CreateDesktop 函数来创建新的虚拟桌面,通过 SwitchDesktop 函数可以转入到指定的桌面,在新创建的桌面中可以打开进程explorer.exe,CreateProcess 有个TStartupInfo结构的参数,该结构中有个叫lpDesktop的成员,它指定了在哪个桌面创建进程;该函数实现的虚拟桌面为Windows视图系统创建多桌面扩展,使用户或程序可以多个桌面上相互切换可视化的应用,与Linux 图形界面中四方格切换相似,但是该技术与现行“桌面虚拟化技术”是两种完全不同的概念,不可以混淆。
引用MSDN:
Creates a new desktop, associates it with the current window station of the calling process, and assigns it to the calling thread. The calling process must have an associated window station, either assigned by the system at process creation time or set by the SetProcessWindowStation function.
To specify the size of the heap for the desktop, use the CreateDesktopEx function.
函数原型
HDESK WINAPI CreateDesktop(
_In_ LPCTSTR lpszDesktop,
_Reserved_ LPCTSTR lpszDevice,
_Reserved_ DEVMODE *pDevmode,
_In_ DWORD dwFlags,
_In_ ACCESS_MASK dwDesiredAccess,
_In_opt_ LPSECURITY_ATTRIBUTES lpsa
);
Parameters
lpszDesktop [in]
The name of the desktop to be created. Desktop names are case-insensitive and may not contain backslash characters (\).
lpszDevice
Reserved; must be NULL.
pDevmode
Reserved; must be NULL.
dwFlags [in]
This parameter can be zero or the following value.
在C语言中的实现
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HDESK hOld = GetThreadDesktop(GetCurrentThreadId());
HDESK hNew = CreateDesktopA(Test, NULL, NULL, NULL, GENERIC_ALL, NULL);
if ((NULL == hOld) || (NULL == hNew))
{
MessageBox(NULL, 失败!, 测试, MB_OK | MB_ICONERROR);
return 1;
}
SetThreadDesktop(hNew);
SwitchDesktop(hNew);
MessageBox(NULL, 成功!, 测试, MB_OK);
SwitchDesktop(hOld);
CloseDesktop(hNew);
return 0;
}
在VB中的实现
Private Sub Form_Load()
g_hDesktopThreadOld = GetThreadDesktop(App.ThreadID) '得到正常的桌面句柄
RegisterHotKey Me.hWnd, 1, MOD_CONTROL, Asc(Q) '注册热键
RegisterHotKey Me.hWnd, 2, MOD_CONTROL, Asc(W) '注册热键
g_hDesktopNameNew = MyNewDesktop
g_hDesktopNew = OpenDesktop(g_hDesktopNameNew, 0, False, DESKTOP_ALL) '如果新桌面已经存在,就打开它
If g_hDesktopNew = 0 Then
g_hDesktopNew = CreateDesktop(g_hDesktopNameNew, vbNullString, ByVal 0&, 0, MAXIMUM_ALLOWED, ByVal 0&) '如果不存在,就新建一个
End If
Call SetThreadDesktop(g_hDesktopNew)
lpOldWinProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf myWindowProc) '子类化主窗口,接收热键消息
End Sub
Private Sub Form_Unload(Cancel As Integer)
If g_hDesktopNew <> 0 Then
CloseDesktop g_hDesktopNew '关闭新建的桌面句柄
End If
SetWindowLong Me.hWnd, GWL_WNDPROC, lpOldWinProc
End Sub