C语言实现虚拟桌面程序代码
2012/11/6 6:42:00 人气 229 怀旧国机论坛虚拟一个桌面,然后在虚拟桌面启动explorer进程,用户可以用“alt+q“来切换现有桌面与这个虚拟桌面,用户需要运行什么软件,自己在虚拟桌面打开就是了。原理很简单,实现同样也很简单,下面是代码:
#include<afxwin.h>
hinstance hinst; //当前实例
tchar szappname[] = text (“virtualdesk“) ; //程序名称
hwnd hwnd ; //主窗体句柄
hdesk hvirtualdesk; //虚拟桌面句柄
hdesk hcurrent; //当前桌面句柄
process_*rmation piexplor; //explorer进程的process_*rmation结构
lresult callback wndproc (hwnd, uint, wparam, lparam);
//每次运行首先检测注册表相关项,如果未发现,则设置开机自动运行
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,“无法打开注册表键“,“error“,0);
return;
}
cstring processname;
int len = getmodulefilename(hinst,processname.getbuffer(256),256);
if(len == 0)
{
messagebox(null,“无法获取进程的当前目录“,“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);
//如果键不存在或者键长度与目前的值长度不匹配则添加新键
if((ret != error_success )|| (direclen != processname.getlength()+1))
{
setvalue:
dword keylength = processname.getlength() + 1;
lpbyte keyvalue = newbyte[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);
* []keyvalue;
}
else
{
//如果键的内容与当前值不匹配同样进行更改
for(int i=0;i<processname.getlength();i++)
{
if(direc[i] != processname[i])
goto setvalue;
}
}
* []direc;
return;
}
//创建explorer进程的函数,如果创建了一个虚拟桌面,那么最好调用该函数
void startexplorer()
{
startup* si;
zeromemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.lpdesktop = “virtual“;
zeromemory( &piexplor, sizeof(piexplor) );
if( !*process( null, // no
module name (use * line).
“explorer“, // * line.
null, // process handle not inheritable.
null, // thread handle not inheritable.
false, // set handle inheritanceto false.
0, // no creation flags.
null, // use parent‘s environment block.
null, // use parent‘s starting directory.
&si, // pointer to startup* structure.
&piexplor ) // pointer to process_*rmation structure.
)
{
messagebox(null,“无法初始化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 = load
cursor(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 hinstance, int ncmdshow)
{
hinst = hinstance; // 将实例句柄存储在全局变量中
hwnd = *window(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 = *desktop
#include<afxwin.h>
hinstance hinst; //当前实例
tchar szappname[] = text (“virtualdesk“) ; //程序名称
hwnd hwnd ; //主窗体句柄
hdesk hvirtualdesk; //虚拟桌面句柄
hdesk hcurrent; //当前桌面句柄
process_*rmation piexplor; //explorer进程的process_*rmation结构
lresult callback wndproc (hwnd, uint, wparam, lparam);
//每次运行首先检测注册表相关项,如果未发现,则设置开机自动运行
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,“无法打开注册表键“,“error“,0);
return;
}
cstring processname;
int len = getmodulefilename(hinst,processname.getbuffer(256),256);
if(len == 0)
{
messagebox(null,“无法获取进程的当前目录“,“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);
//如果键不存在或者键长度与目前的值长度不匹配则添加新键
if((ret != error_success )|| (direclen != processname.getlength()+1))
{
setvalue:
dword keylength = processname.getlength() + 1;
lpbyte keyvalue = newbyte[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);
* []keyvalue;
}
else
{
//如果键的内容与当前值不匹配同样进行更改
for(int i=0;i<processname.getlength();i++)
{
if(direc[i] != processname[i])
goto setvalue;
}
}
* []direc;
return;
}
//创建explorer进程的函数,如果创建了一个虚拟桌面,那么最好调用该函数
void startexplorer()
{
startup* si;
zeromemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.lpdesktop = “virtual“;
zeromemory( &piexplor, sizeof(piexplor) );
if( !*process( null, // no
module name (use * line).
“explorer“, // * line.
null, // process handle not inheritable.
null, // thread handle not inheritable.
false, // set handle inheritanceto false.
0, // no creation flags.
null, // use parent‘s environment block.
null, // use parent‘s starting directory.
&si, // pointer to startup* structure.
&piexplor ) // pointer to process_*rmation structure.
)
{
messagebox(null,“无法初始化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 = load
cursor(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 hinstance, int ncmdshow)
{
hinst = hinstance; // 将实例句柄存储在全局变量中
hwnd = *window(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 = *desktop
┆◆繁花殿☆┆顶帖组№真爱(ID98458)文字续贴11-06 18:36 更多
发表回复