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

📄 frame.cpp

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

// Shuts down the virtual desktop management code -- writes the colors and desktop
// names to the registry then makes all hidden apps under ZDDesk's control visible
// again
void CZDDeskFrame::UninitializeDesks()
{
   // Save the desktop count
   m_pRegistry->SetValue(m_hKey,"DeskCount",REG_DWORD,&m_nDeskCount,sizeof(m_nDeskCount));

   char szDeskNameKey[50];
   char szDeskColorKey[50];

   // Save the colors and names
   for(int i = 0; i < CZDDeskFrame::MAX_DESKS; i++)
   {
      sprintf(szDeskNameKey,"DeskName%d",i+1);
      sprintf(szDeskColorKey,"DeskColor%d",i+1);

      // Write the value to the registry
      m_pRegistry->SetValue(m_hKey,szDeskNameKey,REG_SZ,
                        (LPVOID)(LPCTSTR)m_arrStrDeskName[i],
                        strlen(m_arrStrDeskName[i]));

      // If we are still doing color changes then write the current ones out to the
      // registry
      if(!m_bFrozen)
         m_pRegistry->SetValue(m_hKey,szDeskColorKey,REG_DWORD,&m_arrClrDesk[i],
                                          sizeof(m_arrClrDesk[i]));
   }

   // Close the registry and delete the object
   m_pRegistry->Close(m_hKey);
   delete m_pRegistry;

   // Restore all of the virtual desktops by showing all hidden apps on them
   for(m_nCurrDesk = 0; m_nCurrDesk < m_nDeskCount; m_nCurrDesk++)
      LoadDesk(FALSE);

   // Reset the color to the main desktop
   int nColor = COLOR_DESKTOP;
   SafeSetSysColors(1,&nColor,&m_arrClrDesk[0]);

   // Remove the system tray icon and remove the interface object
   m_pTray->Delete(IDI_ZDDESK1);
   delete m_pTray;
}

///////////////////////////////////////////
/// General desktop management routines ///
///////////////////////////////////////////

// Builds the tooltip text name
CString CZDDeskFrame::GetToolTipText()
{
   // Construct tooltip text pre-pended with the application name
   CString strTitle = AfxGetAppName()+CString(" - ");
   strTitle += m_arrStrDeskName[m_nCurrDesk];

   return(strTitle);
}

// 通过 EnumWindows() 回调函数,得到所有窗口的句柄
BOOL CALLBACK CZDDeskFrame::EnumCallback(HWND hWnd, LPARAM lParam)
{
   // Get the this pointer
   CZDDeskFrame *pThis = (CZDDeskFrame *) lParam;  

   // This saves window in list (if criteria is met)
   pThis->SaveWnd(hWnd);      

   // Tell Windows to continue the enumeration
   return(TRUE);         
}

// 装载某一桌面
void CZDDeskFrame::LoadDesk(BOOL bSetColors)
{
   int nSafety;
   int nCount = GetWindowCount(m_nCurrDesk);
   HWND hWnd;

   // Iterate through the list
   for(int i = 0; i < nCount; i++)
   {
      // Show the window
      hWnd = m_arrHwndDesktop[m_nCurrDesk][i];
      SafeShowWindow(hWnd,SW_SHOW);

      // Wait for the window to draw itself -- give up after a while
      nSafety = 5;
      while(nSafety-- && !::IsWindowVisible(hWnd))
         Sleep(100L);
   }

   // This list is no longer up to date so clean it out now, it will 
   // get refreshed when we switch to another desktop
   m_arrHwndDesktop[m_nCurrDesk].RemoveAll();

   // If we should set colors -- note that setting the color after painting
   // the windows will cause a flash, but we have to do this last in case
   // any windows are "frozen" - i.e the m_bFrozen flag is set because this
   // will cause the WM_SYSCOLORCHANGE message to hang up ZDDesk
   if(bSetColors)
   {
      // Set the background colors
      int nColor = COLOR_DESKTOP;
      SafeSetSysColors(1,&nColor,&m_arrClrDesk[m_nCurrDesk]);
   }
}

//保存桌面 
void CZDDeskFrame::SaveDesk()
{
   // Clear the app list for the current desktop
   m_arrHwndDesktop[m_nCurrDesk].RemoveAll();

   // Enumerate the top level windows
   ::EnumWindows(EnumCallback,(LPARAM)this);

   // Save the system color
   m_arrClrDesk[m_nCurrDesk] = ::GetSysColor(COLOR_DESKTOP);
}

// Handles switching to a new virtual desktop -- called by either a left-mouse
// click message or from a context menu item
void CZDDeskFrame::SwitchDesks(int nNewDesk)
{   
   // Switch desks if necessary
   if(nNewDesk != m_nCurrDesk)
   {
      // Save the current one
      SaveDesk();
      m_nCurrDesk = nNewDesk;

      // Show the new desk's icon and title
      m_pTray->Modify(IDI_ZDDESK1,IDI_ZDDESK1+m_nCurrDesk,GetToolTipText());

      // Load the new desk
      LoadDesk();
   }
}

// Sets the tooltip text for the current active desktop.  This text is based
// on the desktop's name
void CZDDeskFrame::SetCurrentToolTip()
{
   // Tell the tray to change it's title
   m_pTray->Modify(IDI_ZDDESK1,NULL,GetToolTipText());
}

/////////////////////////////////////////////////////////////////////////////
// CZDDeskFrame message handlers

// 在创建框架窗口的过程中创建托盘
int CZDDeskFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
   // Call the base class
   if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
      return -1;

   // Setup the tray information -- notify window, msg and add the try icon
   m_pTray->SetNotifyWindow(GetSafeHwnd(),UM_TRAYNOTIFY);
   m_pTray->Add(IDI_ZDDESK1,GetToolTipText());

   // The following line was added by the Splash Screen component.  // CSplashWnd::ShowSplashScreen(this);

   return 0;
}

// Handles the WM_DESTROY message -- removes the tray notification
void CZDDeskFrame::OnDestroy() 
{
   // Call the base class
   CFrameWnd::OnDestroy();

   // Remove the tray notification
   m_pTray->Delete(IDI_ZDDESK1);
}

// Process the tray notification message -- in our case the UM_TRAYNOTIFY
// message (we specified this in the CZDTray::Add() call in OnCreate() above
LRESULT CZDDeskFrame::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
   // Determine the tray message
   switch(lParam)
   {
      // Left mouse button release -- send it to the frame's normal message
      // handler
      case WM_LBUTTONUP:
         SendMessage(WM_LBUTTONUP,wParam);
         break;

      // Right mouse button release -- send it to the frame's normal message
      // handler
      case WM_RBUTTONUP:
         SendMessage(WM_RBUTTONUP,wParam);
         break;
   }

   return(0);
}

// Display's the context menu
void CZDDeskFrame::ShowContextMenu()
{
   // Load the context menu
   CMenu menuMain;
   menuMain.LoadMenu(IDR_MENU);
   CMenu *pmenuContext = menuMain.GetSubMenu(0);

   // Make sure our window is activated
   SetForegroundWindow();

   // Add the menu items for the number of desks
   for(int i = 0; i < m_nDeskCount; i++)
      pmenuContext->InsertMenu(i,MF_BYPOSITION,IDM_DESK1+i,
                  m_arrStrDeskName[i]);

   // Get rid of the placeholder
   pmenuContext->DeleteMenu(IDM_DUMMY,MF_BYCOMMAND);


   // Check the current desk...
   pmenuContext->CheckMenuItem(IDM_DESK1+m_nCurrDesk,MF_BYCOMMAND|MF_CHECKED);

   // Display the context menu near the mouse cursor
   POINT pt;
   ::GetCursorPos(&pt);
   pmenuContext->TrackPopupMenu(
               TPM_BOTTOMALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
               pt.x,pt.y,this,NULL);

   // We no longer need the menu so destroy it
   menuMain.DestroyMenu();

   // Works around Windows menu handling bug that occurs when no
   // selection was made on the context menu -- the basic bug is that
   // it takes 2 mouse clicks to re-activate the menu after this
   // situation.  Sending a dummy message gets around this
   PostMessage(WM_NULL);
}

// Processes the right mouse button release message -- displays the context menu
void CZDDeskFrame::OnRButtonUp(UINT nFlags, CPoint point) 
{
   if(!m_bInMenu)
   {
      // Display the context menu
      m_bInMenu = TRUE;
      ShowContextMenu();
      m_bInMenu = FALSE;
   }
}

// Handles the "exit" menu option
void CZDDeskFrame::OnExit() 
{
   SendMessage(WM_CLOSE,0,0L);
}

// Processes the left mouse button release message -- switches to a new desktop
void CZDDeskFrame::OnLButtonUp(UINT nFlags, CPoint point) 
{
   // Displaying a menu, or already switching desks???
   if(m_bInMenu || m_bSwitching) 
   {
      // Yep...yell at the user
      MessageBeep(0);
      return;
   }

   // Avoid re-entrancy problems
   m_bSwitching = TRUE;

   // Calculate the new desktop
   int nNewDesk = m_nCurrDesk;
   if(nNewDesk+1 < m_nDeskCount)
      nNewDesk++;
   else nNewDesk = 0;

   // Switch desks
   SwitchDesks(nNewDesk);

   // Clear the re-entrancy flag
   m_bSwitching = FALSE;
}

// Handles the direct desktop selection from the context menu
void CZDDeskFrame::OnSelectDesk(UINT nID)
{
   // Go to the desk selected by the menu item
   SwitchDesks(nID-IDM_DESK1);
}

// Handles the "arrange" menu item
void CZDDeskFrame::OnArrange() 
{
   m_bNoHide = TRUE;
   SaveDesk();
   m_bNoHide = FALSE;

   m_bInMenu = TRUE;

   // Display the arrange dialog
   CZDDeskArrange cArrange(this);
   cArrange.DoModal();

   m_bInMenu = FALSE;
}

// Handles the "properties" menu item
void CZDDeskFrame::OnProperties() 
{
   m_bInMenu = TRUE;

   // Display the properties dialog
   CZDDeskProperties cProperties(this);
   cProperties.DoModal();

   m_bInMenu = FALSE;
}

// Handles the "about" menu item
void CZDDeskFrame::OnAbout() 
{
   m_bInMenu = TRUE;

   // Display the about dialog
   CAbout dlgAbout;
   dlgAbout.DoModal();

   m_bInMenu = FALSE;
}



⌨️ 快捷键说明

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