2026年11月2日 · 今日 0 帖 · 会员 6.1万 · 主题 17.4万
登录 · 注册 · 游客,欢迎随便逛逛
返回版块
1000
Lv9有为九子能等级图标
勋章勋章勋章勋章勋章
帖子
9.3万
回复
2,089
积分
60.3万
注册
2006-12-06
[怀旧国机]

C语言实现虚拟桌面程序代码

[复制链接]
查看 208 回复 2
楼主 发表于 | 只看该作者
1 人收藏

虚拟一个桌面,然后在虚拟桌面启动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,&amp;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;

    zeromemory(direc,direclen)

ret = regqueryvalueex(hkey,“virtualdesk“,0,0,direc,&amp;direclen);

        //如果键不存在或者键长度与目前的值长度不匹配则添加新键

      if((ret != error_success )|| (direclen != processname.getlength()+1))

 {

setvalue:

          dword keylength = processname.getlength() + 1;

          lpbyte keyvalue = newbyte;

          zeromemory(keyvalue,keylength);

          for(int i=0;i<processname.getlength();i++)

              keyvalue = processname;

          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 != processname)

                goto setvalue;

 }

 }

    * []direc;

    return;

}

//创建explorer进程的函数,如果创建了一个虚拟桌面,那么最好调用该函数

void startexplorer()

{

    startup* si;

    zeromemory( &amp;si, sizeof(si) );

    si.cb = sizeof(si);

    si.lpdesktop = “virtual“;

    zeromemory( &amp;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.

    &amp;si,                        // pointer to startup* structure.

    &amp;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(&amp;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

登录回复
59999
王者却具王侯心等级图标等级图标等级图标等级图标等级图标等级图标
勋章勋章勋章勋章勋章勋章勋章
帖子
384
回复
1.4万
积分
6万
注册
2011-07-08
1 楼

//////抢‐‐沙‐‐发//////

106008
Lv6胸怀六国志等级图标等级图标等级图标
勋章勋章勋章勋章勋章勋章勋章
帖子
27
回复
6,810
积分
40.7万
注册
2012-05-02
2 楼

顶!顶!顶!繁花鼎立于绝客前方

登录后可以回复这个帖子。旧账号可使用原密码登录。

登录 注册新号
← 返回版块