Implementation of a virtual desktop program

Source: Internet
Author: User

I wrote a small tool for my friend a few days ago to hide the big smart stock market software with one click. The first version was to use window search and then send the wm_show message to the window. Although the implementation principle is simple, it is not universal. As I initially imagined, one-click hiding of software should be defined by the user, rather than being customized every time. So the second version of this tool was born, that is, Virtualize a desktop for the user, and then start the explorer process on the virtual desktop, you can use "Alt + q" to switch between the existing desktop and the virtual desktop. You can open the software on the virtual desktop. The principle is very simple, and the implementation is also very simple. The following code:

/*************************************** **************************************** Module: sunbear. cppdate: 2008/06/13 notice: Copyright (c) 2008 changhailong ************************************* **************************************** **/# include <afxwin. h> hinstance hinst; // the current instance tchar szappname [] = text ("virtualdesk"); // program name hwnd; // The main form handle hdesk hvirtualdesk; // hdesk hcurrent; // The current desktop handle process_information piexp Lor; // The process_information structure of the explorer process lresult callback wndproc (hwnd, uint, wparam, lparam); // check the registry-related metrics for each running. If not, set void setstartup (hinstance hinst) {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, "cannot open registry key", "error", 0); return;} cstring processname; int Len = getmodulefilename (hinst, processname. getbuffer (256), 256); If (LEN = 0) {MessageBox (null, "unable to get the current directory of the 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, & D Ireclen); // if the key does not exist or the key length does not match the current value length, add the new key 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 key content does not match the current value, change for (INT I = 0; I <processname. Getlength (); I ++) {If (direc [I]! = Processname [I]) goto setvalue ;}} Delete [] direc; return;} // create a function of the explorer process. If a virtual desktop is created, it is better to call this 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 parent's Environment block. null, // use parent's starting directory. & Si, // pointer to startupinfo structure. & piexplor) // pointer to process_information s Tructure .) {MessageBox (null, "Cannot initialize Explorer", "error", 0); exitprocess (1) ;}} atom myregisterclass (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 (& wcex);} bool initinstance (hinstance, int ncmdshow) {hinst = hinstance; // store the instance handle in the global variable hwnd = createwindow (szappname, text ("sunbear"), ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, NUL L, hinst, null); If (! Hwnd) {return false;} return true;} int winapi winmain (hinstance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {static tchar szappname [] = text ("sunbear"); 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 between virtual desktops return true;} If (! Registerhotkey (hwnd, 0x0002, 0, vk_f8) {// return true;} while (getmessage (& MSG, null, 0, 0 )) {translatemessage (& MSG); dispatchmessage (& MSG);} return 0;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {HDC; paintstruct pS; static hdesk Hnow = hcurrent; Switch (Message) {Case wm_paint: HDC = beginpaint (hwnd, & PS); endpaint (hwnd, & PS); Return 0; case wm_destroy: // switch back to the current desktop switchdesktop (hcurrent); postquitmessage (0); Return 0; Case wm_hotkey: If (0x0001 = wparam) before closing the virtual desktop) {If (Hnow = hcurrent) {switchdesktop (hvirtualdesk); Hnow = hvirtualdesk;} else {switchdesktop (hcurrent); Hnow = hcurrent ;}} if (0x0002 = wparam) {// when terminateprocess is used to terminate the explorer process, if the second parameter is 1 //, the operating system will not automatically wake up explorer after the termination, if the value is 0, restart // assumerterminateprocess (piexplor. hprocess, 1); // close the closedesktop (hvirtualdesk); sendmessage (hwnd, wm_close,);} return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

 

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.