home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / vrac / dlgcbr.zip / WNDLIST.CPP < prev    next >
C/C++ Source or Header  |  1994-07-05  |  10KB  |  340 lines

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