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

📄 arrange.cpp

📁 这是书上的代码
💻 CPP
字号:
#include "stdafx.h"

#include "zddesk.h"
#include "arrange.h"
#include "frame.h"

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

/////////////////////////////////////////////////////////////////////////////
// CZDDeskArrange dialog


// Constructor 
CZDDeskArrange::CZDDeskArrange(CZDDeskFrame *pFrame, CWnd* pParent /*=NULL*/)
   : CDialog(CZDDeskArrange::IDD, pParent)
{
   //{{AFX_DATA_INIT(CZDDeskArrange)
      // NOTE: the ClassWizard will add member initialization here
   //}}AFX_DATA_INIT

   // Save the parent frame
   m_pFrame = pFrame;

   // Initialize drag info
   m_bDragging = FALSE;
   m_hcurDrop = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_DROP));
   m_hcurNoDrop = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_NODROP));

   m_nSelectedEntry = -1;
}

// Destructor
CZDDeskArrange::~CZDDeskArrange()
{
}

// Handles MFC field data exchange
void CZDDeskArrange::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   //{{AFX_DATA_MAP(CZDDeskArrange)
   DDX_Control(pDX, IDC_TAB, m_Tab);
   DDX_Control(pDX, IDC_LIST, m_List);
   //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CZDDeskArrange, CDialog)
   //{{AFX_MSG_MAP(CZDDeskArrange)
   ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTab)
   ON_NOTIFY(LVN_BEGINDRAG, IDC_LIST, OnBegindragList)
   ON_BN_CLICKED(IDC_ACTIVATE, OnActivate)
   ON_WM_CLOSE()
   ON_BN_CLICKED(IDC_GLUE, OnGlue)
   ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnItemChangedList)
   ON_NOTIFY_RANGE(TTN_NEEDTEXT, IDC_TAB, IDC_TAB, OnTabTooltip)
   ON_NOTIFY_RANGE(TTN_NEEDTEXT, 0, 9, OnTabTooltip)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////
// Private operations

// Loads the list of running applications for the specified desktop
void CZDDeskArrange::FillDeskList(int nDesk)
{
   // Clear the old list
   m_List.DeleteAllItems();
   int nImageIndex;

   // Get the number of apps on this desk
   int nCount = m_pFrame->GetWindowCount(nDesk);

   // Iterate through the app list
   for(int i = 0; i < nCount; i++)
   {
      HWND hWnd = m_pFrame->m_arrHwndDesktop[nDesk][i];

      // Valid window handle???
      if(hWnd)
      {
         CString strTitle;

         // Get the window's title 
         ::GetWindowText(hWnd,strTitle.GetBuffer(500),500);
         strTitle.ReleaseBuffer();

         // No title...display a default "untitled" string because we 
         // need a placeholder for this app because it is visible and
         // untitled, ooohhh, gross
         if(strTitle.IsEmpty())
            strTitle.LoadString(IDS_UNTITLED);

         // Set the appropriate list icon -- glued windows have a 
         // checkmark so they use index 1, whereas all other windows
         // use image index 0
         nImageIndex = m_pFrame->IsGlued(hWnd)?1:0;
         m_List.InsertItem(LVIF_TEXT|LVIF_IMAGE,i,strTitle,0,0,
                           nImageIndex,0);
      }
   }
}

// Initializes the tab control
void CZDDeskArrange::Init()
{
   TC_ITEM tcItem;
   char szTemp[20];

   tcItem.mask = TCIF_TEXT;

   // Add tabs for each desktop
   for(int i = 0; i < m_pFrame->m_nDeskCount; i++)
   {
      sprintf(szTemp,"Desk %d",i+1);
      tcItem.pszText = szTemp;
      m_Tab.InsertItem(i,&tcItem);
   }

   // Select the current desktop's tab by default
   m_Tab.SetCurSel(m_pFrame->m_nCurrDesk);

   // Initialize the image list
   m_imgList.Create(IDB_LIST,16,1,RGB(0,128,128));
   m_List.SetImageList(&m_imgList,LVSIL_NORMAL);
   m_List.SetImageList(&m_imgList,LVSIL_SMALL);

   // Add a column for the app name
   m_List.InsertColumn(0,"Name",LVCFMT_LEFT,300);

   // Load the app names
   FillDeskList(m_pFrame->m_nCurrDesk);

   // Disable the glue button until we select something valid
   GetDlgItem(IDC_GLUE)->EnableWindow(FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// CZDDeskArrange message handlers


// Handles the WM_INITDIALOG message
BOOL CZDDeskArrange::OnInitDialog() 
{
   CDialog::OnInitDialog();

   // Save the arrange window handle so we don't hide it if we switch desks using
   // the "Set Active" button
   m_pFrame->m_hWndPopup = m_hWnd;

   // Initilize the tab control
   Init();

   // Set the list control to always show the selection
   DWORD dwStyle = ::GetWindowLong(m_List.GetSafeHwnd(),GWL_STYLE);
   ::SetWindowLong(m_List.GetSafeHwnd(),GWL_STYLE,dwStyle|LVS_SHOWSELALWAYS);

   return TRUE;  // return TRUE unless you set the focus to a control
                 // EXCEPTION: OCX Property Pages should return FALSE
}

// Handles a tab change message
void CZDDeskArrange::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
   // Load the app list for the new tab
   int nNewTab = m_Tab.GetCurSel();
   FillDeskList(nNewTab);

   // Initialize the glue button
   CheckDlgButton(IDC_GLUE,FALSE);
   GetDlgItem(IDC_GLUE)->EnableWindow(FALSE);
   m_nSelectedEntry = -1;

   *pResult = 0;
}

// Handles the TVN_BEGINDRAG message indicating that a drag operation is
// starting
void CZDDeskArrange::OnBegindragList(NMHDR* pNMHDR, LRESULT* pResult) 
{
   NM_LISTVIEW* pList = (NM_LISTVIEW*)pNMHDR;
  
   // Determine the selected item
   int nDesk = m_Tab.GetCurSel();
   int nIndex = m_nSelectedEntry;

   // No valid selection, just get out...
   if(nIndex < 0 || nIndex >= m_pFrame->GetWindowCount(nDesk)) 
   {
      // Abort the whole thing
      *pResult = 0;
      return;
   }

   // Get the window handle
   HWND hWnd = m_pFrame->m_arrHwndDesktop[nDesk][nIndex];

   // Make sure it is isn't glued -- can't drag glued items
   if(!m_pFrame->IsGlued(hWnd))
   {
      // Grab the mouse and set the dragging flags
      m_bDragging = TRUE;
      m_nDragDesk = m_Tab.GetCurSel();
      SetCapture();

      // Okay to start the drag operation
      *pResult = 1;
   }
   else *pResult = 0;
}

// Handles the messages used during drag/drop operations
// by getting the messages here we don't have to worry about them
// being sent to any of the child controls on the dialog
BOOL CZDDeskArrange::PreTranslateMessage(MSG* pMsg) 
{
   switch(pMsg->message)
   {
      // Process the left button release message
      case WM_LBUTTONUP:
         // Were we dragging???
         if(m_bDragging)
         {
            // Let go of the mouse
            m_bDragging = FALSE;
            ReleaseCapture();

            TC_HITTESTINFO tcHit;      // Tab hittest structure

            // Tell HitTest() to look within the entire area of the tab
            tcHit.flags = TCHT_ONITEM;   

            // Get the mouse location (from WM_MOUSEMOVE)
            tcHit.pt = pMsg->pt;      

            // Convert the location the the tab's client area
            m_Tab.ScreenToClient(&tcHit.pt);

            // Get the tab number
            int nDropDesk = m_Tab.HitTest(&tcHit);   
         
            // Verify that we have a valid drop tab
            if(m_nSelectedEntry >= 0 && nDropDesk >= 0 
                        && nDropDesk != m_nDragDesk)
            {
               // Move the app then make sure the list reflects this
               m_pFrame->MoveToDesk(m_nDragDesk,m_nSelectedEntry,nDropDesk); 
               FillDeskList(m_nDragDesk);
               ::BringWindowToTop(m_pFrame->m_hWnd);
            }

            // All finished, set the cursor back to normal
            ::SetCursor(::LoadCursor(NULL,IDC_ARROW));
         }
         break;

      case WM_MOUSEMOVE:
         // Are we dragging??
         if(m_bDragging)
         {
            // See if we are over a valid tab
            TC_HITTESTINFO tcHit;
            tcHit.flags = TCHT_ONITEM;
            tcHit.pt = pMsg->pt;
            m_Tab.ScreenToClient(&tcHit.pt);
            int nSelTab = m_Tab.HitTest(&tcHit);
            int nCurrTab = m_Tab.GetCurSel();

            // If we are over a valid tab then set the drop cursor, else
            // set the nodrop cursor
            if(nSelTab >= 0 && nSelTab != nCurrTab)
               ::SetCursor(m_hcurDrop);
            else ::SetCursor(m_hcurNoDrop);
         }
         break;
   }

   // Pass this stuff on to the base class
   return CDialog::PreTranslateMessage(pMsg);
}

// Handles the Set Active button
void CZDDeskArrange::OnActivate() 
{
   int nNewDesk = m_Tab.GetCurSel();

   // Active the current desktop based on the selected tab
   if(nNewDesk != m_pFrame->m_nCurrDesk)
   {
      // Perform the seitch
      m_pFrame->SwitchDesks(nNewDesk);
      m_pFrame->m_bNoHide = TRUE;

      // Update the new desk's app list so we can display it
      m_pFrame->SaveDesk();
      m_pFrame->m_bNoHide = FALSE;

      // Display the new desk's app list
      FillDeskList(nNewDesk);
   }
}

// Handles the WM_CLOSE message
void CZDDeskArrange::OnClose() 
{
   // Unregister the popup handle
   m_pFrame->m_hWndPopup = NULL;

   CDialog::OnClose();
}

// Handles the Keep on All Desks (glue) button
void CZDDeskArrange::OnGlue() 
{
   // Do we have a valid selection???
   if(m_nSelectedEntry >= 0)
   {
      int nDesk = m_Tab.GetCurSel();
      int nIndex = m_nSelectedEntry;
      HWND hWnd = m_pFrame->m_arrHwndDesktop[nDesk][nIndex];
      BOOL bGlued = IsDlgButtonChecked(IDC_GLUE);

      // Toggle the glue state
      m_pFrame->SetGlue(hWnd,bGlued,nDesk);

      // If it is now glued then show the checkmark icon
      if(bGlued)
      {
         m_List.SetItem(m_nSelectedEntry,0,LVIF_IMAGE,NULL,1,0,0,0);
         if(!::IsWindowVisible(hWnd))
            ::ShowWindow(hWnd,SW_SHOW);
      }
      else
      {
         // Redisplay the desk list -- the unglued application will
         // change
         FillDeskList(nDesk);
         CheckDlgButton(IDC_GLUE,FALSE);
         GetDlgItem(IDC_GLUE)->EnableWindow(FALSE);
      }

      // Move focus back to the list
      m_List.SetFocus();
   }
}

// Enables or disables the Keep on All Desks checkbox based on the 
// currently selected list item
void CZDDeskArrange::OnItemChangedList(NMHDR* pNMHDR, LRESULT* pResult) 
{
   NM_LISTVIEW* pList = (NM_LISTVIEW*)pNMHDR;

   // Is this item being selected???
   if(pList && pList->uNewState & LVIS_SELECTED)
   {
      // Get the list entry that was clicked
      m_nSelectedEntry = pList->iItem;

      int nDesk = m_Tab.GetCurSel();
      HWND hWnd = m_pFrame->m_arrHwndDesktop[nDesk][m_nSelectedEntry];

      // Check button based on the glue state
      CheckDlgButton(IDC_GLUE,m_pFrame->IsGlued(hWnd));
      GetDlgItem(IDC_GLUE)->EnableWindow(TRUE);
   }

   *pResult = 0;
}


// Provides tooltip text for a tab (the desk name)
void CZDDeskArrange::OnTabTooltip(UINT nID, NMHDR *pNMHDR, LRESULT *pResult)
{
   TOOLTIPTEXT *pTT = (TOOLTIPTEXT *) pNMHDR;

   int nDesk = pTT->hdr.idFrom;

   // Is the tab number valid???
   if(nDesk >= 0 && nDesk < CZDDeskFrame::MAX_DESKS)
   {
      // Yes, set the tooltip text to the desktop name
      pTT->hinst = 0;
      strcpy(pTT->szText,m_pFrame->m_arrStrDeskName[nDesk]);
   }

   // Return good result (always)
   *pResult = 1;
}


⌨️ 快捷键说明

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