程式控制案頭切換

來源:互聯網
上載者:User
程式控制案頭切換   [ 日期:2006-01-03 ]   [ 來自:轉貼 ]


魔法表情

用嚮導建立一個Win32程式“hello, world!”,然後用下面的內容替換整個CPP檔案內容就可以了

#include "stdafx.h"
#include "resource.h"

#include "ShellApi.h"

HINSTANCE hInst = NULL;

HDESK hDesktopCurrent;
HDESK hDesktopLlx;

LONG APIENTRY WndProc(
    HWND hWnd,
    UINT message,      // type of message
    WPARAM wParam,     // additional information
    LPARAM lParam)     // additional information
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;

 switch (message) 
 {
  case WM_COMMAND:
   wmId    = LOWORD(wParam); 
   wmEvent = HIWORD(wParam); 
   // Parse the menu selections:
   switch (wmId)
   {
    case IDM_ABOUT:
       //DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...
   RECT rt;
   GetClientRect(hWnd, &rt);
   //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
   EndPaint(hWnd, &ps);
   break;
  /*
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  //*/
  case WM_LBUTTONDOWN:
   break;
  case WM_HOTKEY:
   if(7777 == wParam)
   {
    PostQuitMessage(0);
   }
   else if(7778 == wParam)
   {
    SwitchDesktop(hDesktopCurrent);
   }
   else if(7779 == wParam)
   {
    SwitchDesktop(hDesktopLlx);
   }
   break;
  case WM_QUIT:
  case WM_DESTROY:
   SwitchDesktop(hDesktopCurrent);
   return DefWindowProc(hWnd, message, wParam, lParam);
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

void StartMyExplore(void)
{
 STARTUPINFO sui;         // Process startup info
 PROCESS_INFORMATION pi;  // info returned from CreateProcess
 //
 // Most sui members will be 0
 //
 ZeroMemory ((PVOID)&sui, sizeof(sui));

 sui.cb = sizeof (sui);
 //
 // Need the lpDesktop member so the new process runs on this desktop
 // The lpDesktop member was reserved in previous versions of NT
 //
 sui.lpDesktop = _T("llx");
 CreateProcess (NULL,   // image name
      "explorer", // command line
      NULL,   // process security attributes
      NULL,   // thread security attributes
      TRUE,   // inherit handles
      CREATE_DEFAULT_ERROR_MODE|CREATE_SEPARATE_WOW_VDM,
      NULL,   // environment block
      NULL,   // current directory
      &sui,   // STARTUPINFO
      &pi);   // PROCESS_INFORMATION
}

int CALLBACK WinMain( HINSTANCE hInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nCmdShow)
{
 WNDCLASS wc;
 wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc = WndProc;
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;
 wc.hInstance = hInstance;
 wc.hIcon = LoadIcon (NULL, "IDI_SETTHREADDESKTOP");
 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
 wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
 wc.lpszMenuName = NULL;
 wc.lpszClassName = "lilinxiang";
 if(!RegisterClass(&wc))
 {
  return TRUE;
 }

 hDesktopCurrent = NULL;
 hDesktopCurrent = GetThreadDesktop(GetCurrentThreadId());

 hDesktopLlx = NULL;
 hDesktopLlx = OpenDesktop("llx", 0, FALSE, NULL);
    if (hDesktopLlx != NULL)
 {
  CloseDesktop(hDesktopLlx);
 }

 SECURITY_ATTRIBUTES sa;
 sa.bInheritHandle = TRUE;
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.lpSecurityDescriptor = NULL;

 hDesktopLlx = CreateDesktop("llx", NULL,
                                    NULL,0,MAXIMUM_ALLOWED,
                                    NULL);
 if(hDesktopLlx == NULL)
 {
  return 0;
 }

 if(!SetThreadDesktop(hDesktopLlx))
 {
  char szError[256] = {0};
  ltoa( (long)(GetLastError()) , szError, 10);
 }
 SwitchDesktop(hDesktopLlx);

 HWND hWnd = NULL;
 hWnd = CreateWindow ("lilinxiang",
        "hello, world!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        0, 
        CW_USEDEFAULT, 
        0, 
        NULL, 
        NULL, 
        hInstance, 
        NULL
        );
 if(NULL == hWnd)
 {
  return TRUE;
 }

 //register hotkey for exit this desktop or switch to another desktop
 //ShowWindow(hWnd, SW_SHOW);
 //UpdateWindow(hWnd);
 if(!RegisterHotKey(hWnd, 7777, MOD_CONTROL, 'Q'))
 {//exit process
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7778, MOD_CONTROL | MOD_SHIFT, 'Q'))
 {//switch to new desktop
  return TRUE;
 }
 if(!RegisterHotKey(hWnd, 7779, MOD_CONTROL | MOD_SHIFT, 'W'))
 {//switch to original desktop
  return TRUE;
 }
 
 StartMyExplore();

 MSG msg;
 while (GetMessage(&msg, NULL,  0, 0))
    {
                 TranslateMessage(&msg);// Translates virtual key codes
                 DispatchMessage(&msg); // Dispatches message to window
    }

 SwitchDesktop(hDesktopCurrent);
 return TRUE;
}

//   : )  好東西噢

用CreateDesktop建立一個案頭,什麼都沒有的案頭,然後在新案頭環境中運行了explorer所以就有了和windows一樣功能的案頭,這個時候你就有了兩個案頭了,下面要做的就是用SwitchDesktop切換不同的案頭了

在不同案頭中開啟的程式在其他案頭的工作列包括系統托盤上不可見,不過工作管理員中還是可見的這樣你就非常方便的做很多事了,很多...  而且還可以非常快的轉到正常狀態上可以讓會責罵你的人比如老闆什麼的無話可說,因為他們什麼也看不到  : )

或者你也可以給你使用者定製一個個人化的案頭。改改上面的代碼可以得到你要的效果的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.