当线程更改为不同的桌面时,使用 AllocConsole() 创建的新控制台会出现在原始桌面中。如预期的那样,创建的消息框和其他窗口会出现在新桌面中,但控制台窗口不会。以下代码是一个 windows 应用程序示例,但是无论它是否是控制台应用程序,结果仍然是相同的。
#include <Windows.h>
HDESK hDesk;
DWORD WINAPI Testing(void *)
{
SetThreadDesktop(hDesk);
AllocConsole();
MessageBox(NULL, TEXT("Test"), NULL, MB_OK); //This will show on the new desktop
FreeConsole();
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hDesk=CreateDesktop(TEXT("Testing"),NULL,NULL,NULL,GENERIC_ALL,NULL);
SwitchDesktop(hDesk);
DWORD thr;
HANDLE thread = CreateThread (0, 0, Testing, 0, 0, &thr);
WaitForSingleObject (thread, 10000); //Wait 10 seconds before automatically exiting.
SwitchDesktop(GetThreadDesktop(GetCurrentThreadId())); //Return to previous desktop
CloseDesktop(hDesk);
return 0;
}
如何在第二个桌面中创建控制台窗口?