Please help Ukrainian armed forces! Official Fundraising

How to create a window on a separate desktop in Windows

1 1 1 1 1 How to create a window on a separate desktop in Windows5.00Rating 5.00 (1 Vote)

It's not a secret that Windows 10 has several desktops. The previous Windows versions had separate desktops as well, but only the default was accessible by applications by default. When you need to create a window on a separate desktop, you can use the sample code from this article.

The code below creates a separate thread for the desktop and runs a separate message loop in it. This is necessary because the default application message loop is not intended to rum on multiple desktops. 

 


void CreateDesktop2()
{
	CString sDeskName;
	sDeskName.Format("DeskTop%x",GetTickCount());
		
	HDESK hDesktop2 = CreateDesktop(sDeskName,
	NULL,NULL,0,STANDARD_RIGHTS_ALL|DESKTOP_CREATEMENU|DESKTOP_CREATEWINDOW|
	DESKTOP_ENUMERATE|DESKTOP_HOOKCONTROL|DESKTOP_READOBJECTS|
	DESKTOP_SWITCHDESKTOP|DESKTOP_WRITEOBJECTS,NULL);
	if(hDesktop2 == NULL)
		AfxMessageBox("Error in CreateDesktop",MB_OK,0);
	HDESK hUserDesktop = GetThreadDesktop(GetCurrentThreadId());
	HTHREAD hThread = (HANDLE)_beginthreadex(NULL,0,DeskThread,this,0,(unsigned*)&m_dwThreadId);
}
UINT DeskThread(LPVOID lpParam)
{
	MSG msg;
	
	SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
	SetThreadDesktop(hBlockDesktop);
	if (p->m_wnd_desk.Create())
	{
                /// CREATE YOUR WINDOW HERE
		///p->m_waker_dlg.Create(CWakerDlg::IDD,&p->m_wnd_desk);
		while(::GetMessage(&msg,0,0,0)!=0)
		{
			if (!IsDialogMessage(p->m_waker_dlg.m_hWnd,&msg))// to process keyboard messages
			{
				::TranslateMessage(&msg);
				::DispatchMessage(&msg);
			}
		}
	}
	else
	{
		::MessageBox(0,"DeskThread failed",NULL,MB_OK);
	}
	p->m_waker_dlg.DestroyWindow();
	p->m_wnd_desk.DestroyWindow();
	SetThreadDesktop(p->m_hUserDesktop);
	SwitchDesktop(p->m_hUserDesktop);
	
	_endthreadex(0);
	return 0;
}

 

You have no rights to post comments

Thursday the 25th.