home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / dlgcbr32 / wndlist.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  9.5 KB  |  332 lines

  1. // WndList.cpp : Implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. /*****************************************************************************
  15.   Purpose:
  16.     Implements CWndListDlg, the main dialog window for the application.
  17.  
  18.   Functions:
  19.     CWndListDlg::CWndListDlg()          -- constructor
  20.     CWndListDlg::Create()               -- create dialog window
  21.     CWndListDlg::DoDataExchange()       -- dialog data exchange/validation
  22.     CWndListDlg::OnClose()              -- WM_CLOSE handler
  23.     CWndListDlg::OnInitDialog()         -- initialize dialog
  24.     CWndListDlg::OnOptionNow()          -- handle "Options!Update Now"
  25.     CWndListDlg::OnOptionRate()         -- handle "Options!Update Interval"
  26.     CWndListDlg::OnSelChangeWndList()   -- update dlg on list selection change
  27.     CWndListDlg::OnTimer()              -- WM_TIMER handler
  28.     CWndListDlg::OnUpdateOptionRate()   -- set "Options!Update Interval" status
  29.     CWndListDlg::OnUpdateTime()         -- update status bar clock
  30.     CWndListDlg::WalkWindowList()       -- enumerate windows
  31.  
  32.   Development Team:
  33.     Mary Kirtland
  34.   Ported to 32-bit by:
  35.     Mike Hedley
  36.   Created by Microsoft Product Support Services, Premier ISV Support
  37.   Copyright (c) 1998 Microsoft Corporation. All rights reserved.
  38. \****************************************************************************/
  39.  
  40. #include "stdafx.h"
  41. #include <afxpriv.h>
  42. #include "resource.h"
  43. #include "wndlist.h"
  44. #include "ratedlg.h"
  45.  
  46. #ifdef _DEBUG
  47. #define new DEBUG_NEW
  48. #undef THIS_FILE
  49. static char THIS_FILE[] = __FILE__;
  50. #endif
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CWndListDlg control bar data
  54.  
  55. static UINT auIDStatusBar[] = {
  56.    ID_SEPARATOR,
  57.    ID_INDICATOR_TIME
  58. };
  59.  
  60. static UINT auIDToolBar[] = {
  61.    ID_APP_EXIT,
  62.    ID_SEPARATOR,
  63.    ID_OPTION_NOW,
  64.    ID_OPTION_RATE,
  65.    ID_SEPARATOR,
  66.    ID_HELP_ABOUT
  67. };
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CWndListDlg dialog
  71.  
  72. CWndListDlg::CWndListDlg()
  73. {
  74.     //{{AFX_DATA_INIT(CWndListDlg)
  75.     //}}AFX_DATA_INIT
  76.     m_nIDTimer = 0;
  77.     m_iRate = m_iTicks = 0;
  78. }
  79.  
  80. BOOL CWndListDlg::Create()
  81. {
  82.     return CModelessMain::Create(CWndListDlg::IDD,
  83.                                  auIDStatusBar,
  84.                                  sizeof(auIDStatusBar)/sizeof(UINT),
  85.                                  auIDToolBar,
  86.                                  sizeof(auIDToolBar)/sizeof(UINT),
  87.                                  IDR_MAIN);
  88. }
  89.  
  90. void CWndListDlg::DoDataExchange(CDataExchange* pDX)
  91. {
  92.     CModelessMain::DoDataExchange(pDX);
  93.     //{{AFX_DATA_MAP(CWndListDlg)
  94.     DDX_Control(pDX, IDC_WNDLIST, m_lbWindows);
  95.     //}}AFX_DATA_MAP
  96. }
  97.  
  98. BEGIN_MESSAGE_MAP(CWndListDlg, CModelessMain)
  99.     //{{AFX_MSG_MAP(CWndListDlg)
  100.     ON_WM_CLOSE()
  101.     ON_WM_TIMER()
  102.     ON_LBN_SELCHANGE(IDC_WNDLIST, OnSelChangeWndList)
  103.     ON_COMMAND(ID_OPTION_NOW, OnOptionNow)
  104.     ON_COMMAND(ID_OPTION_RATE, OnOptionRate)
  105.     ON_UPDATE_COMMAND_UI(ID_OPTION_RATE, OnUpdateOptionRate)
  106.     //}}AFX_MSG_MAP
  107.     ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIME, OnUpdateTime)
  108. END_MESSAGE_MAP()
  109.  
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CWndListDlg::OnInitDialog
  113. //      OnInitDialog fills the listbox with information about the parent
  114. //      windows which exist.  It also sets up a 1sec timer, which is used
  115. //      to automatically update the listbox contents and/or to update the
  116. //      status bar clock.
  117.  
  118. BOOL CWndListDlg::OnInitDialog()
  119. {
  120.     CModelessMain::OnInitDialog();
  121.  
  122.     // Initialize windows listbox contents
  123.     WalkWindowList();
  124.     OnSelChangeWndList();
  125.  
  126.     // Set up a 1 second timer
  127.     m_nIDTimer = SetTimer(1, 1000, NULL);
  128.     if (!m_nIDTimer)
  129.         AfxMessageBox(IDP_NOTIMER);
  130.  
  131.     return TRUE;
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CWndListDlg::OnClose
  136. //      OnClose makes sure the 1sec timer is destroyed when the dialog
  137. //      is closed.
  138.  
  139. void CWndListDlg::OnClose()
  140. {
  141.     if (m_nIDTimer)
  142.     {
  143.         KillTimer(m_nIDTimer);
  144.         m_nIDTimer = NULL;
  145.     }
  146.     CModelessMain::OnClose();
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CWndListDlg::WalkWindowList
  151. //      WalkWindowList enumerates all parent windows on the screen.
  152. //      It adds a string containing the window handle and caption to
  153. //      the listbox for each window, using the helper function
  154. //      EnumWindowsProc().
  155.  
  156. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  157. {
  158.     static char szBuffer[90];
  159.     static char szTemp[80];
  160.  
  161.     static char szFormat[] = _T("%08lX: %s");
  162.     HWND hwndListBox = (HWND)lParam;
  163.     CListBox* plb = (CListBox*)CWnd::FromHandle(hwndListBox);
  164.  
  165.     GetWindowText(hwnd, szTemp, sizeof(szTemp));
  166.     sprintf(szBuffer, szFormat, hwnd, szTemp);
  167.  
  168.     return (plb->AddString(szBuffer) >= 0) ? TRUE : FALSE;
  169. }
  170.  
  171. void CWndListDlg::WalkWindowList()
  172. {
  173.     // don't assume that m_lbWindows has been initialized yet
  174.     CListBox* plb = (CListBox*)GetDlgItem(IDC_WNDLIST);
  175.  
  176.     plb->SetRedraw(FALSE);
  177.     plb->ResetContent();
  178.  
  179.     EnumWindows(EnumWindowsProc, (LPARAM)plb->GetSafeHwnd());
  180.  
  181.     plb->SetRedraw(TRUE);
  182. }
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CWndListDlg::OnSelChangeWndList
  186. //      OnSelChangeWndList updates the static dialog fields when the
  187. //      window listbox selection is changed.
  188.  
  189. void CWndListDlg::OnSelChangeWndList()
  190. {
  191.     static char szBuffer[256];
  192.     static char szTemp[80];
  193.  
  194.     static char szParse[]  = _T("%08lX");
  195.     static char szFormat[] = _T("%08lX: %s");
  196.  
  197.     HWND hwnd;
  198.     BOOL bValid = FALSE;
  199.  
  200.     int iSel = m_lbWindows.GetCurSel();
  201.     if (iSel >= 0)
  202.     {
  203.         m_lbWindows.GetText(iSel, szBuffer);
  204.       sscanf(szBuffer, szParse, &hwnd);
  205.         bValid = ::IsWindow(hwnd);
  206.     }
  207.  
  208.     if (bValid)
  209.     {
  210.         ::GetClassName(hwnd, szBuffer, sizeof(szBuffer));
  211.         GetDlgItem(IDC_CLASS)->SetWindowText(szBuffer);
  212.  
  213.         HINSTANCE hInstance = (HINSTANCE)::GetWindowLong(hwnd, GWL_HINSTANCE);
  214.         ::GetModuleFileName(hInstance, szBuffer, sizeof(szBuffer));
  215.         GetDlgItem(IDC_MODULE)->SetWindowText(szBuffer);
  216.  
  217.         HWND hwndParent = ::GetParent(hwnd);
  218.         ::GetWindowText(hwndParent, szTemp, sizeof(szTemp));
  219.         sprintf(szBuffer, szFormat, hwndParent, szTemp);
  220.         GetDlgItem(IDC_PARENT)->SetWindowText(szBuffer);
  221.  
  222.         RECT rect;
  223.         ::GetWindowRect(hwnd, &rect);
  224.         sprintf(szBuffer, _T("(%d,%d) - (%d,%d)"), rect.left, rect.top,
  225.                 rect.right, rect.bottom);
  226.         GetDlgItem(IDC_RECT)->SetWindowText(szBuffer);
  227.  
  228.         LONG lStyle = ::GetWindowLong(hwnd, GWL_STYLE);
  229.         sprintf(szBuffer, _T("%08lX"), lStyle);
  230.         GetDlgItem(IDC_STYLE)->SetWindowText(szBuffer);
  231.     }
  232.     else
  233.     {
  234.         szBuffer[0] = '\0';
  235.         GetDlgItem(IDC_CLASS)->SetWindowText(szBuffer);
  236.         GetDlgItem(IDC_MODULE)->SetWindowText(szBuffer);
  237.         GetDlgItem(IDC_PARENT)->SetWindowText(szBuffer);
  238.         GetDlgItem(IDC_RECT)->SetWindowText(szBuffer);
  239.         GetDlgItem(IDC_STYLE)->SetWindowText(szBuffer);
  240.     }
  241.  
  242. }
  243.  
  244. /////////////////////////////////////////////////////////////////////////////
  245. // CWndListDlg::OnTimer()
  246. //      OnTimer handles messages from the 1sec interval timer.  The variable
  247. //      m_iRate indicates how many timer messages must be received before
  248. //      the window list is updated.  The variable m_iTicks indicates how
  249. //      many timer messages have been received since the last time the
  250. //      window list was updated.
  251.  
  252. void CWndListDlg::OnTimer(UINT nIDEvent)
  253. {
  254.     if (m_iRate)
  255.     {
  256.         m_iTicks++;
  257.         if (m_iTicks == m_iRate)
  258.             OnOptionNow();
  259.     }
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. // CWndListDlg::OnOptionNow()
  264. //      OnOptionNow resets the update interval counter, refills the window
  265. //      list, and resets the selection, if possible.
  266.  
  267. void CWndListDlg::OnOptionNow()
  268. {
  269.     m_iTicks = 0;
  270.  
  271.     int iTopIndex = m_lbWindows.GetTopIndex();
  272.  
  273.     CString strSelect;
  274.     int iSel = m_lbWindows.GetCurSel();
  275.     if (iSel >= 0)
  276.         m_lbWindows.GetText(iSel, strSelect);
  277.  
  278.     WalkWindowList();
  279.  
  280.     if (m_lbWindows.SelectString(-1, strSelect) <= 0)
  281.         m_lbWindows.SetCurSel(iSel);
  282.  
  283.     m_lbWindows.SetTopIndex(iTopIndex);
  284.  
  285. }
  286.  
  287. /////////////////////////////////////////////////////////////////////////////
  288. // CWndListDlg::OnOptionRate()
  289. //      OnOptionRate allows the user to specify the rate at which the
  290. //      window list should be updated.
  291.  
  292. void CWndListDlg::OnOptionRate()
  293. {
  294.     CRateDlg dlg(m_iRate, this);
  295.     if (dlg.DoModal() == IDOK)
  296.         m_iRate = dlg.GetRate();
  297. }
  298.  
  299. /////////////////////////////////////////////////////////////////////////////
  300. // CWndListDlg::OnUpdateOptionRate()
  301. //      OnUpdateOptionRate enables or disables the Option Update Rate...
  302. //      command, depending on whether or not a timer has been created.
  303.  
  304. void CWndListDlg::OnUpdateOptionRate(CCmdUI* pCmdUI)
  305. {
  306.     pCmdUI->Enable(m_nIDTimer != 0);
  307. }
  308.  
  309. /////////////////////////////////////////////////////////////////////////////
  310. // CWndListDlg::OnUpdateTime()
  311. //      OnUpdateTime updates the status bar clock.
  312.  
  313. void CWndListDlg::OnUpdateTime(CCmdUI* pCmdUI)
  314. {
  315.     CTime t = CTime::GetCurrentTime();
  316.     char  szTime[6];
  317.     int   nHour = t.GetHour();
  318.     int   nMinute = t.GetMinute();
  319.  
  320.     // Base hours on 12 instead of 24
  321.     if (nHour > 12)
  322.         nHour -= 12;
  323.  
  324.     wsprintf(szTime, _T("%02i:%02i"), nHour, nMinute);
  325.  
  326.     // Now set the text of the pane
  327.     CStatusBar* pStatusBar = GetStatusBar();
  328.     pStatusBar->SetPaneText(pStatusBar->CommandToIndex(ID_INDICATOR_TIME),
  329.                             LPCSTR(szTime));
  330.     pCmdUI->Enable();
  331. }
  332.