⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:


#include "stdafx.h"
#include "zddesk.h"

#include "frame.h"

#include "tray.h"
#include "registry.h"
#include "arrange.h"
#include "property.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static const char *ZDDESKKEY = "Software\\PC Magazine\\ZDDesk";
static const int UM_TRAYNOTIFY = WM_USER+2000;

/////////////////////////////////////////////////////////////////////////////
// CZDDeskFrame

BEGIN_MESSAGE_MAP(CZDDeskFrame, CFrameWnd)
   //{{AFX_MSG_MAP(CZDDeskFrame)
   ON_WM_CREATE()
   ON_WM_DESTROY()
   ON_WM_RBUTTONUP()
   ON_COMMAND(IDM_EXIT, OnExit)
   ON_WM_LBUTTONUP()
   ON_COMMAND(IDM_ARRANGE, OnArrange)
   ON_COMMAND(IDM_PROPERTIES, OnProperties)
   ON_COMMAND(IDM_ABOUT, OnAbout)  
   //}}AFX_MSG_MAP
   ON_MESSAGE(UM_TRAYNOTIFY,OnTrayNotify)
   ON_COMMAND_RANGE(IDM_DESK1,IDM_DESK9,OnSelectDesk)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZDDeskFrame construction/destruction

// Constructor
CZDDeskFrame::CZDDeskFrame()
{
   // 初始化虚拟桌面
   InitializeDesks();
}

// Destructor
CZDDeskFrame::~CZDDeskFrame()
{
   // 关闭虚拟桌面
   UninitializeDesks();
}

///////////////////////////////
/// 安全显示窗口例程 

// 在应用程序未冻结时,执行窗口显示
BOOL CZDDeskFrame::SafeShowWindow(HWND hWnd, int nShowCmd)
{
   //判断应用程序是否存活或是已经挂起
   DWORD dwResult;
   if(::SendMessageTimeout(hWnd,WM_NULL,0,0,SMTO_ABORTIFHUNG,1000,&dwResult))
   {
      // 正常情况下,窗口显示
      if(dwResult == 0)
         ::ShowWindow(hWnd,nShowCmd);
      return(TRUE);
   }
   else 
   {
      // 程序已经冻结,给出警告消息
      if(!m_bFrozen)
      {
         m_bFrozen = TRUE;
         AfxMessageBox(IDS_FROZEN,MB_ICONWARNING|MB_OK);
      }
      return(FALSE);
   }
}

// 程序未冻结时,可以设置背景颜色
void CZDDeskFrame::SafeSetSysColors(int cElements, CONST INT *lpaElements,
                              CONST COLORREF *lpaRgbValues)
{
   // Okay to send???
   if(!m_bFrozen)
      ::SetSysColors(cElements,lpaElements,lpaRgbValues);
}


//////////////////////////////////////////
/// 窗口处理例程 ///
//////////////////////////////////////////

// 将有效的窗口句柄保存进行句柄集合中
void CZDDeskFrame::SaveWnd(HWND hWnd)
{
   // 判断句柄是否合法
   if(IsCandidate(hWnd))
   {
      // If we shouldn't skip the hide (m_bNoHide is set if we are just building
      // a list of apps for the arrange dialog) and the window is not glued....
      // then hide it
      if(!m_bNoHide && !IsGlued(hWnd))
      {
         // 测试窗口是否能够正常显示
         // collective...
         if(!SafeShowWindow(hWnd,SW_HIDE))
            return;
      }

      // 加入集合
      m_arrHwndDesktop[m_nCurrDesk].Add(hWnd);
   }
}

// 判断窗口是否是可以假如集合的
BOOL CZDDeskFrame::IsCandidate(HWND hWnd)
{
   
   if(hWnd == m_hWnd || hWnd == m_hWndShell ||   hWnd == m_hWndDesktop ||
                           hWnd == m_hWndPopup)
      return(FALSE);

    return(::IsWindowVisible(hWnd));
}

// 判断集合中的窗口数
int CZDDeskFrame::FindNormalWindow(int nDesk, HWND hWnd)
{
   // How many windows we got, anyway??
   int nCount = GetWindowCount(nDesk);

   // Iterate through them all
   for(int i = 0; i < nCount; i++)
   {
      // Aha, we found it...return its index
      if(m_arrHwndDesktop[nDesk][i] == hWnd)
         return(i);
   }

   // Didn't find it, return and error
   return(-1);
}

// Searches for the specifed window in the list of glued windows
int CZDDeskFrame::FindGluedWindow(HWND hWndGlue)
{
   // Get the number of glued windows
   int nCount = GetGluedCount();

   // Iterate through the list
   for(int i = 0; i < nCount; i++)
   {
      // Aha, we got it, tell somebody about it
      if(m_arrHwndGlued[i] == hWndGlue)
         return(i);
   }

   // No dice, try again
   return(-1);
}

// Adds the specified window to the collection objects of each
// desktop
void CZDDeskFrame::AddToAllDesks(HWND hWnd)
{
   int nIndex;

   // Add this window to all desks (even non-configured ones)
   for(int i = 0; i < CZDDeskFrame::MAX_DESKS; i++)
   {
      // Is it already there???
      nIndex = FindNormalWindow(i,hWnd);

      // Not found...add it
      if(nIndex < 0)
         m_arrHwndDesktop[i].Add(hWnd);
   }
}

// Removes the specified window from all desktops except for the one
// specifed by nTab (this is the active tab on the Arrange dialog)
void CZDDeskFrame::RemoveFromAllDesks(HWND hWnd, int nTab)
{
   int nCount;

   // Iterate through all of the desks
   for(int i = 0; i < CZDDeskFrame::MAX_DESKS; i++)
   {
      // On the desk for the current tab??? get out then
      if(i == nTab) continue;
      
      // Get the number of entries in the window collection
      nCount = GetWindowCount(i);

      // Iterate through the window collection until we
      // find the specified window
      for(int j = 0; j < nCount; j++)
      {
         // Found a match??
         if(m_arrHwndDesktop[i][j] == hWnd)
         {
            // Yep, remove it and get out
            m_arrHwndDesktop[i].RemoveAt(j);
            break;
         }
      }
   }
}

// Moves the specifed entry from one desk (nOldDesk) to another desk (nNewDesk)
void CZDDeskFrame::MoveToDesk(int nOldDesk, int nOldEntry, int nNewDesk)
{
   // Get the window handle
   HWND hWnd = m_arrHwndDesktop[nOldDesk][nOldEntry];

   // Remove the window from the current desk...
   m_arrHwndDesktop[nOldDesk].RemoveAt(nOldEntry);

   // And add it to the new desk
   m_arrHwndDesktop[nNewDesk].Add(hWnd);

   // Hide or show the window based on whether we are moving it to or from
   // the currently active desk
   if(nOldDesk == m_nCurrDesk)
      SafeShowWindow(hWnd,SW_HIDE);
   else if(nNewDesk == m_nCurrDesk)
      SafeShowWindow(hWnd,SW_SHOW);
}

// Sets the glue state for a windwo -- if bGlue is TRUE it is glued, if bGlue
// is FALSE it is un-glued
void CZDDeskFrame::SetGlue(HWND hWndGlue, BOOL bGlue, int nTab)
{
   // Try to locate the window
   int nIndex = FindGluedWindow(hWndGlue);

   // Didn't find it and we are glueing....
   if(nIndex < 0 && bGlue)
   {
      // Add it to the glue list and to the window collections
      m_arrHwndGlued.Add(hWndGlue);
      AddToAllDesks(hWndGlue);
   }   // Found and match and not un-glueing
   else if(nIndex >= 0 && !bGlue)  
   {
      // Remove the window from the collection objects
      RemoveFromAllDesks(m_arrHwndGlued[nIndex],nTab);

      // Hide the window on the current desk if it doesn't match
      // the selected tab
      if(nTab != m_nCurrDesk)
         SafeShowWindow(m_arrHwndGlued[nIndex],SW_HIDE);

      // Remove the window from glue collection
      m_arrHwndGlued.RemoveAt(nIndex);
   }
}

// Returns the number of entries in the window collection object for the
// specified desk
int CZDDeskFrame::GetWindowCount(int nDesk)
{
   return(m_arrHwndDesktop[nDesk].GetSize());
}

// Returns the number of entries in the glued window collection object
int CZDDeskFrame::GetGluedCount()
{
   return(m_arrHwndGlued.GetSize());
}

////////////////////////////////////////////////////////////
/// Virtual desktop initialization and shutdown routines ///
////////////////////////////////////////////////////////////

// Initializes the virtual desktop management code: sets up work variables then
// loads default information from the system registry such as desktop colors and
// desktop names
void CZDDeskFrame::InitializeDesks()
{
   // Initialize some work variables
   m_nCurrDesk = 0;
   m_bSwitching = FALSE;
   m_bInMenu = FALSE;
   m_bFrozen = FALSE;
   m_hWndPopup = NULL;
   m_bNoHide = FALSE;
   m_nDeskCount = 5;

   // Construct the system tray interface
   m_pTray = new CZDTray();

   // Get the current background color
   COLORREF clrDesk = ::GetSysColor(COLOR_DESKTOP);

   // Load some default data for colors and desktop names just in case
   // we can't find a registry entry
   CString strTitle;
   for(int i = 0; i < CZDDeskFrame::MAX_DESKS; i++)
   {
      m_arrClrDesk.Add(clrDesk);
      strTitle.Format("Desk %d",i+1);
      m_arrStrDeskName.Add(strTitle);
   }

   // Get the window handles the for taskbar and the desktop (progman)
   m_hWndShell = ::FindWindow("Shell_TrayWnd",NULL);
   m_hWndDesktop = ::FindWindow("Progman",NULL);


   // Initialize the registry stuff
   m_pRegistry = new CZDRegistry();   
   m_pRegistry->Open(HKEY_CURRENT_USER,ZDDESKKEY,KEY_ALL_ACCESS,m_hKey);

   // Get the desktop count
   m_pRegistry->GetValue(m_hKey,"DeskCount",REG_DWORD,&m_nDeskCount,sizeof(m_nDeskCount));

   // Get the desktop names and colors
   char szDeskNameKey[50];
   char szDeskColorKey[50];
   for(i = 0; i < CZDDeskFrame::MAX_DESKS; i++)
   {
      // Key names DeskNamex DeskColorx -- when x is the desktop 1-9
      sprintf(szDeskNameKey,"DeskName%d",i+1);
      sprintf(szDeskColorKey,"DeskColor%d",i+1);

      // Get the name of the desk
      m_pRegistry->GetValue(m_hKey,szDeskNameKey,REG_SZ,
                     m_arrStrDeskName[i].GetBuffer(CZDDeskFrame::MAX_NAME+1),
                     CZDDeskFrame::MAX_NAME);
      m_arrStrDeskName[i].ReleaseBuffer();

      // Get the color of the desk
      m_pRegistry->GetValue(m_hKey,szDeskColorKey,REG_DWORD,&m_arrClrDesk[i],
                                          sizeof(m_arrClrDesk[i]));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -