C language implementation of the Virtual Desktop program code

Source: Internet
Author: User
Tags command line

Virtual desktop, and then start the Explorer process on the virtual desktops, users can use "alt+q" to switch between the existing desktop and the virtual desktop, users need to run what software, they open the virtual desktop. The principle is simple, the implementation is also very simple, the following is the code:

#include <afxwin.h>
HINSTANCE hinst; Current instance
TCHAR szappname[] = TEXT ("Virtualdesk"); Program Name
HWND hwnd; Main form Handle
Hdesk Hvirtualdesk; Virtual Desktop handle
Hdesk hcurrent; Current Desktop handle
Process_information Piexplor; Process_information structure of the explorer process
Lresult CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
First detect registry dependencies every time you run and set up Autorun if not found
void Setstartup (HInstance hinst)
{
HKEY hkey;
DWORD DataType = REG_SZ;
Pcstr Data_run = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
LONG ret = RegOpenKeyEx (Hkey_local_machine,data_run,0,key_all_access,&hkey);
if (ret!= error_success)
{
MessageBox (NULL, "Unable to open registry key", "Error", 0);
Return
}
CString ProcessName;
int len = GetModuleFileName (Hinst,processname.getbuffer (256), 256);
if (len = = 0)
{
MessageBox (NULL, "Unable to get current directory of Process", "Error", 0);
Return
}
Processname.releasebuffer (len);
DWORD Direclen = processname.getlength () + 1;
Lpbyte Direc = new Byte[direclen];
ZeroMemory (Direc,direclen);
ret = RegQueryValueEx (hkey, "Virtualdesk", 0,0,direc,&direclen);
Add a new key if the key does not exist or the key length does not match the current value length
if (ret!= error_success) | | (Direclen!= processname.getlength () +1))
{
SetValue:
DWORD keylength = processname.getlength () + 1;
Lpbyte keyvalue = new Byte[keylength];
ZeroMemory (keyvalue,keylength);
for (int i=0;i<processname.getlength (); i++)
Keyvalue[i] = Processname[i];
Keyvalue[processname.getlength ()] = 0;
ret = RegSetValueEx (hkey, "Virtualdesk", 0,reg_sz,keyvalue,keylength);
delete []keyvalue;
}
Else
{
If the contents of the key do not match the current value, the same changes are made
for (int i=0;i<processname.getlength (); i++)
{
if (Direc[i]!= processname[i])
Goto SetValue;
}
}
delete []direc;
Return
}
To create a function of the explorer process, if you create a virtual desktop, it is best to call the function

void Startexplorer ()
{
Startupinfo si;
ZeroMemory (&si, sizeof (SI));
SI.CB = sizeof (SI);
Si.lpdesktop = "Virtual";
ZeroMemory (&piexplor, sizeof (Piexplor));
if (! CreateProcess (NULL,//No module name (use command line).

"Explorer",//Command line.
NULL,//Process handle not inheritable.
NULL,//Thread handle not inheritable.
FALSE,//Set handle inheritance to FALSE.
0,//No creation flags.
NULL,//Use the parent ' s environment block.
NULL,//Use parent ' s starting directory.
&si,//pointer to STARTUPINFO structure.
&piexplor)//pointer to process_information structure.
)
{
MessageBox (NULL, "Unable to initialize explorer", "Error", 0);
ExitProcess (1);
}
}
ATOM MyRegisterClass (hinstance hinstance)
{
Wndclassex Wcex;
wcex.cbsize = sizeof (wndclassex);
Wcex.style = Cs_hredraw | Cs_vredraw;
Wcex.lpfnwndproc = (WNDPROC) WNDPROC;
Wcex.cbclsextra = 0;
Wcex.cbwndextra = 0;
Wcex.hinstance = hinstance;
Wcex.hicon = LoadIcon (hinstance, (LPCTSTR) idi_application);
Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
Wcex.hbrbackground = (hbrush) (color_window+1);
Wcex.lpszmenuname = NULL;
Wcex.lpszclassname = Szappname;
WCEX.HICONSM = LoadIcon (Wcex.hinstance, (LPCTSTR) idi_application);
return RegisterClassEx (&AMP;WCEX);
}
BOOL InitInstance (hinstance hinstance, int ncmdshow)
{
Hinst = hinstance; To store an instance handle in a global variable
hwnd = CreateWindow (Szappname, TEXT ("Sunbear"), Ws_overlappedwindow,
Cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, hinst, NULL);
if (!hwnd)
{
return FALSE;
}
return TRUE;
}
int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance,pstr szcmdline, int icmdshow)
{
Static TCHAR szappname[] = TEXT ("Sunbear");
MSG msg;
Setstartup (HINSTANCE);
Hvirtualdesk = Createdesktop (
"Virtual",
Null
Null
Df_allowotheraccounthook,
Generic_all,
NULL);
Hcurrent = Getthreaddesktop (GetCurrentThreadID ());
MyRegisterClass (HINSTANCE);
if (! InitInstance (HINSTANCE, Icmdshow))
{
return FALSE;
}
Startexplorer (); Start the explorer process
if (! RegisterHotKey (hwnd, 0x0001,mod_alt, ' Q '))
{
Process switching Virtual Desktops
return TRUE;
}
if (! RegisterHotKey (hwnd, 0x0002, 0,vk_f8))
{
Processing the exit process
return TRUE;
}
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
return 0;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
HDC HDC;
Paintstruct PS;
static Hdesk Hnow = Hcurrent;
Switch (message)
{
Case WM_PAINT:
HDC = BeginPaint (hwnd, &AMP;PS);
EndPaint (hwnd, &AMP;PS);
return 0;
Case Wm_destroy:
To switch back to the current desktop before closing the virtual table
Switchdesktop (hcurrent);
PostQuitMessage (0);
return 0;
Case Wm_hotkey:
if (0x0001 = = WParam)
{
if (Hnow = = hcurrent)
{
Switchdesktop (Hvirtualdesk);
Hnow = Hvirtualdesk;
}
Else
{
Switchdesktop (hcurrent);
Hnow = hcurrent;
}
}
if (0x0002 = = WParam)
{
When you terminate the Explorer process with TerminateProcess, if you pass the second argument to 1
Then the operating system will not automatically wake the explorer after termination, if 0, will restart
Explorer
TerminateProcess (piexplor.hprocess,1);
Turn off Virtual desktop
Closedesktop (Hvirtualdesk);
SendMessage (hwnd,wm_close,0,0);
}
return 0;
}
Return DefWindowProc (HWND, message, WParam, LParam);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.