home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / perfmon / perfmon.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  13KB  |  494 lines

  1. /*****************************************************************************
  2.  *
  3.  *  Perfmon.c - This is the WinMain module. It creates the main window and
  4.  *              the threads, and contains the main MainWndProc.
  5.  *
  6.  *  Microsoft Confidential
  7.  *  Copyright (c) 1992-1997 Microsoft Corporation
  8.  *
  9.  *  Authors -
  10.  *
  11.  *       Russ Blake
  12.  *       Mike Moskowitz
  13.  *       Hon-Wah Chan
  14.  *       Bob Watson
  15.  *
  16.  ****************************************************************************/
  17.  
  18.  
  19. //==========================================================================//
  20. //                                  Includes                                //
  21. //==========================================================================//
  22. #undef NOSYSCOMMANDS
  23.  
  24. // DEFINE_GLOBALS will define all the globals listed in globals.h
  25. #define DEFINE_GLOBALS
  26.  
  27. #include "perfmon.h"
  28.  
  29. #include "command.h"
  30.  
  31. #include "graph.h"
  32. #include "log.h"
  33. #include "alert.h"
  34. #include "report.h"     // for CreateReportWindow
  35. #include "legend.h"
  36. #include "init.h"
  37. #include "perfmops.h"
  38. #include "toolbar.h"    // for CreateToolbar
  39. #include "status.h"     // for CreatePMStatusWindow
  40. #include "utils.h"
  41.  
  42. #include "fileopen.h"   // for FileOpen
  43.  
  44.  
  45. #define dwToolbarStyle     (WS_CHILD | WS_VISIBLE | TBS_NOCAPTION)
  46.  
  47. extern TCHAR szInternational[] ;
  48.  
  49. //==========================================================================//
  50. //                              Message Handlers                            //
  51. //==========================================================================//
  52.  
  53. void static OnSize (HWND hWnd,
  54.                     WORD xWidth,
  55.                     WORD yHeight)
  56. /*
  57.    Effect:        Perform any actions needed when the main window is
  58.                   resized. In particular, size the four data windows,
  59.                   only one of which is visible right now.
  60. */
  61.    {  // OnSize
  62.    SizePerfmonComponents () ;
  63.    }
  64.  
  65.  
  66. void static OnCreate (HWND hWnd)
  67. /*
  68.    Effect:        Perform all actions needed when the main window is
  69.                   created. In particular, create the three data windows,
  70.                   and show one of them.
  71.  
  72.    To Do:         Check for proper creation. If not possible, we will
  73.                   need to abort creation of the program.
  74.  
  75.    Called By:     MainWndProc only, in response to a WM_CREATE message.
  76. */
  77.    {  // OnCreate
  78.    hWndGraph = CreateGraphWindow (hWnd) ;
  79.  
  80. #ifdef ADVANCED_PERFMON
  81.    hWndLog = CreateLogWindow (hWnd) ;
  82.    hWndAlert = CreateAlertWindow (hWnd) ;
  83.    hWndReport = CreateReportWindow (hWnd) ;
  84. #endif
  85.  
  86.    hWndStatus = CreatePMStatusWindow (hWnd) ;
  87.  
  88.    CreateToolbarWnd (hWnd) ;
  89.    MinimumSize += WindowHeight (hWndToolbar) ;
  90.  
  91.    Options.bMenubar = TRUE ;
  92.    Options.bToolbar = TRUE ;
  93.    Options.bStatusbar = TRUE;
  94.    Options.bAlwaysOnTop = FALSE ;
  95.  
  96.    // initialize to chart view - HWC
  97.    iPerfmonView = IDM_VIEWCHART;
  98.  
  99.  
  100.    ShowWindow (PerfmonViewWindow (), SW_SHOWNORMAL) ;
  101.    }  // OnCreate
  102.  
  103.  
  104.  
  105. //==========================================================================//
  106. //                             Exported Functions                           //
  107. //==========================================================================//
  108.  
  109. void MenuBarHit (DWORD wParam)
  110. {
  111.    if (wParam == MENUCLOSING)
  112.       {
  113.       StatusLineReady (hWndStatus) ;
  114.       dwCurrentMenuID = 0 ;
  115.       }
  116.    else if (HIWORD(wParam) & MF_SYSMENU)
  117.       {
  118.       WORD   SystemMenuItem = 0 ;
  119.       switch (LOWORD (wParam))
  120.          {
  121.          case SC_RESTORE:
  122.             SystemMenuItem = IDM_SYSTEMRESTORE ;
  123.             break ;
  124.  
  125.          case SC_SIZE:
  126.             SystemMenuItem = IDM_SYSTEMSIZE ;
  127.             break ;
  128.  
  129.          case SC_MOVE:
  130.             SystemMenuItem = IDM_SYSTEMMOVE ;
  131.             break ;
  132.  
  133.          case SC_MINIMIZE:
  134.             SystemMenuItem = IDM_SYSTEMMINIMIZE ;
  135.             break ;
  136.  
  137.          case SC_MAXIMIZE:
  138.             SystemMenuItem = IDM_SYSTEMMAXIMIZE ;
  139.             break ;
  140.  
  141.          case SC_CLOSE:
  142.             SystemMenuItem = IDM_SYSTEMCLOSE ;
  143.             break ;
  144.  
  145.          case SC_TASKLIST:
  146.             SystemMenuItem = IDM_SYSTEMSWITCHTO ;
  147.             break ;
  148.          }
  149.  
  150.       if (SystemMenuItem)
  151.          {
  152.          StatusLine (hWndStatus, SystemMenuItem) ;
  153.          dwCurrentMenuID = MenuIDToHelpID (SystemMenuItem) ;
  154.          }
  155.       }
  156.    else
  157.       {
  158.       StatusLine (hWndStatus, LOWORD (wParam)) ;
  159.       }
  160. }
  161.  
  162. void OnDropFile (DWORD wParam)
  163.    {
  164.    TCHAR FileName [FilePathLen + 1] ;
  165.    LPTSTR         pFileNameStart ;
  166.    HANDLE         hFindFile ;
  167.    WIN32_FIND_DATA FindFileInfo ;
  168.    int            NameOffset ;
  169.    int            NumOfFiles = 0 ;
  170.  
  171.    NumOfFiles = DragQueryFile ((HDROP) wParam, 0xffffffff, NULL, 0) ;
  172.    if (NumOfFiles > 0)
  173.       {
  174.       // we only open the first file for now
  175.       DragQueryFile((HDROP) wParam, 0, FileName, FilePathLen) ;
  176.  
  177.       pFileNameStart = ExtractFileName (FileName) ;
  178.       NameOffset = pFileNameStart - FileName ;
  179.  
  180.       // convert short filename to long NTFS filename if necessary
  181.       hFindFile = FindFirstFile (FileName, &FindFileInfo) ;
  182.       if (hFindFile && hFindFile != INVALID_HANDLE_VALUE)
  183.          {
  184.          // append the file name back to the path name
  185.          lstrcpy (&FileName[NameOffset], FindFileInfo.cFileName) ;
  186.          FindClose (hFindFile) ;
  187.          }
  188.  
  189.       FileOpen (hWndMain, (int)0, (LPTSTR)FileName) ;
  190.       PrepareMenu (GetMenu (hWndMain));
  191.       }
  192.  
  193.    DragFinish ((HDROP) wParam) ;
  194.    }
  195.  
  196. LRESULT APIENTRY MainWndProc (HWND hWnd, 
  197.                               UINT message, 
  198.                               DWORD wParam, 
  199.                               LONG lParam)
  200.    {
  201.    LONG     lRetCode = 0L ;
  202.    BOOL     bCallDefWinProc = FALSE ;
  203.  
  204.    switch (LOWORD (message))
  205.       {  // switch
  206.       case WM_LBUTTONDBLCLK:
  207.          ShowPerfmonMenu (!Options.bMenubar) ;
  208.          if (Options.bMenubar)
  209.             {
  210.             PrepareMenu (GetMenu (hWnd)) ;
  211.             }
  212.          break ;
  213.  
  214.        case WM_COMMAND:
  215.           if (PerfmonCommand (hWnd,wParam,lParam))
  216.              return(0);
  217.          else
  218.             bCallDefWinProc = TRUE ;
  219.          break;
  220.  
  221.       case WM_MENUSELECT:
  222.          MenuBarHit (wParam) ;
  223.          break ;
  224.  
  225.       case WM_NCHITTEST:
  226.         /* if we have no title/menu bar, clicking and dragging the client
  227.          * area moves the window. To do this, return HTCAPTION.
  228.          * Note dragging not allowed if window maximized, or if caption
  229.          * bar is present.
  230.          */
  231.         wParam = DefWindowProc(hWnd, message, wParam, lParam);
  232.         if (!Options.bMenubar && 
  233.             (wParam == HTCLIENT) &&
  234.             !IsZoomed (hWndMain))
  235.            return HTCAPTION ;
  236.         else
  237.            return wParam ;
  238.         break ;
  239.  
  240.       
  241.       case WM_SHOWWINDOW:
  242.          PrepareMenu (GetMenu (hWnd)) ;
  243.          break ;
  244.  
  245.       case WM_SIZE:
  246.          OnSize (hWnd, LOWORD (lParam), HIWORD (lParam)) ;
  247.          break ;
  248.  
  249.       case WM_GETMINMAXINFO:
  250.          {
  251.          MINMAXINFO   *pMinMax ;
  252.  
  253.          pMinMax = (MINMAXINFO *) lParam ;
  254.          pMinMax->ptMinTrackSize.x = MinimumSize ;
  255.          pMinMax->ptMinTrackSize.y = MinimumSize ;
  256.          }
  257.          break ;
  258.  
  259.       case WM_F1DOWN:
  260.          if (dwCurrentDlgID)
  261.             {
  262.             CallWinHelp (dwCurrentDlgID) ;
  263.             }
  264.          else if (dwCurrentMenuID)
  265.             {
  266.             CallWinHelp (dwCurrentMenuID) ;
  267.             dwCurrentMenuID = 0 ;
  268.             }
  269.          break ;
  270.  
  271.       case WM_CREATE:
  272.          OnCreate (hWnd) ;
  273.          ViewChart (hWnd) ;
  274.          PrepareMenu (GetMenu (hWnd)) ;
  275.          break ;
  276.  
  277.       case WM_DESTROY:
  278.          WinHelp (hWndMain, pszHelpFile, HELP_QUIT, 0) ;
  279.          PostQuitMessage (0);
  280.          break ;
  281.  
  282.       case WM_QUERYENDSESSION:
  283.          // please shut it down
  284.          return (1) ;
  285.          break ;
  286.  
  287.       case WM_ENDSESSION:
  288.          if (wParam == TRUE)
  289.             {
  290.             // close any log file before closing down
  291.             PerfmonClose (hWnd) ;
  292.             return (1) ;
  293.             }
  294.          else
  295.             bCallDefWinProc = TRUE ;
  296.          break ;
  297.  
  298.       case WM_CLOSE:
  299.          PerfmonClose (hWnd) ;
  300.          break ;
  301.  
  302.       case WM_ACTIVATE:
  303.          {
  304.          int   fActivate = LOWORD (wParam) ;
  305.  
  306.          bPerfmonIconic = (BOOL) HIWORD (wParam) ;
  307.          if (bPerfmonIconic == 0 && fActivate != WA_INACTIVE)
  308.             {
  309.             // set focus on the Legend window
  310.             if (iPerfmonView == IDM_VIEWCHART)
  311.                {
  312.                SetFocus (hWndGraphLegend) ;
  313.                }
  314.             else if (iPerfmonView == IDM_VIEWALERT)
  315.                {
  316.                SetFocus (hWndAlertLegend) ;
  317.                }
  318.             else if (iPerfmonView == IDM_VIEWLOG)
  319.                {
  320.                SetFocus (hWndLogEntries) ;
  321.                }
  322.             else if (iPerfmonView == IDM_VIEWREPORT)
  323.                {
  324.                SetFocus (hWndReport) ;
  325.                }
  326.             }
  327.          }
  328.          break ;
  329.  
  330.       case WM_SYSCOLORCHANGE:
  331.          DeletePerfmonSystemObjects () ;
  332.          CreatePerfmonSystemObjects () ;
  333.          WindowInvalidate (PerfmonViewWindow()) ;
  334.          break ;
  335.  
  336.       case WM_WININICHANGE:
  337.          if (!lParam || strsamei((LPTSTR)lParam, szInternational))
  338.             {
  339.             GetDateTimeFormats () ;
  340.             }
  341.          break ;
  342.  
  343.       case WM_DROPFILES:
  344.          OnDropFile (wParam) ;
  345.          return (0) ;
  346.          break ;
  347.  
  348.       default:
  349.          bCallDefWinProc = TRUE ;
  350.           break;
  351.  
  352.       }  // switch
  353.  
  354.    if (bCallDefWinProc)
  355.       {
  356.       lRetCode = DefWindowProc (hWnd, message, wParam, lParam) ;
  357.       }
  358.    return (lRetCode);
  359.    }  // MainWndProc
  360.  
  361.  
  362. int PASCAL WinMain (HINSTANCE hCurrentInstance,
  363.                     HINSTANCE hPrevInstance,
  364.                     LPSTR lpszCmdLine, 
  365.                     int nCmdShow)
  366.    {  // WinMain
  367.    MSG      msg;
  368.  
  369.    if (!PerfmonInitialize (hCurrentInstance, hPrevInstance, 
  370.                            lpszCmdLine, nCmdShow))
  371.       return (FALSE) ;
  372.  
  373.    DragAcceptFiles (hWndMain, TRUE) ;
  374.  
  375.    while (GetMessage (&msg, NULL, 0, 0))
  376.       {
  377.       if (!TranslateAccelerator(hWndMain, hAccelerators, &msg))
  378.          {
  379.          TranslateMessage (&msg) ;
  380.          DispatchMessage (&msg) ;
  381.          }
  382.       }  // while
  383.  
  384.    return(msg.wParam);
  385.    }
  386.  
  387. DWORD FAR PASCAL MessageFilterProc (int nCode,
  388.                                     WPARAM wParam,
  389.                                     LPARAM lParam)
  390.    {
  391.    LPMSG lpMsg = (LPMSG)lParam ;
  392.    extern HHOOK lpMsgFilterProc ;
  393.  
  394.    if (nCode < 0)
  395.       {
  396.       return FALSE ;
  397.       }
  398.  
  399.    if (nCode == MSGF_DIALOGBOX || nCode == MSGF_MENU)
  400.       {
  401.       if (lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F1)
  402.           {
  403.           PostMessage (hWndMain, WM_F1DOWN, nCode, 0L) ;
  404.           return TRUE ;
  405.           }
  406.       }
  407.  
  408.    return (DefHookProc (nCode, wParam,
  409.               (DWORD)lpMsg,
  410.               &lpMsgFilterProc)) ;
  411.    }
  412.  
  413.  
  414. #if 0
  415. /***************************************/
  416. VOID ErrorExit(LPTSTR pszError,HWND hwnd)
  417. /***************************************/
  418. {
  419.     // NOTE: make sure all lgraph calls set StopQuerying.
  420.  
  421.     if (hwnd)
  422.     {
  423.         MessageBox(NULL, pszError, nm_buf, MB_ICONHAND | MB_OK | MB_SYSTEMMODAL);
  424.         DestroyWindow(hwnd);
  425.     }
  426.     return;
  427. }
  428. #endif
  429.  
  430.  
  431.  
  432. void SizePerfmonComponents (void) 
  433.    {
  434.    RECT           rectClient ;
  435.    int            xWidth, yHeight ;
  436.    int            yToolbarHeight ;
  437.    int            yStatusHeight ;
  438.    int            yViewHeight ;
  439.  
  440.    GetClientRect (hWndMain, &rectClient) ;
  441.    xWidth = rectClient.right - rectClient.left ;
  442.    yHeight = rectClient.bottom - rectClient.top ;
  443.  
  444.    if (Options.bToolbar)
  445.       {
  446.       SendMessage (hWndToolbar, WM_SIZE, 0, 0L) ;
  447.       }
  448.  
  449.    yToolbarHeight = Options.bToolbar ? (WindowHeight (hWndToolbar) - 1) : 0 ;
  450.    yStatusHeight = Options.bStatusbar ? StatusHeight (hWndStatus) : 0 ;
  451.  
  452.    if (Options.bStatusbar)
  453.       {
  454.       if (yToolbarHeight + yStatusHeight > yHeight)
  455.          {
  456.          // too small to display both toolbar and status bar
  457.          // just display part of the status bar
  458.          yStatusHeight = yHeight - yToolbarHeight ;
  459.          }
  460.  
  461.       MoveWindow (hWndStatus, 
  462.                   0, yHeight - yStatusHeight, xWidth, yStatusHeight, TRUE) ;
  463.       //WindowInvalidate (hWndStatus) ;
  464.       }
  465.    //WindowInvalidate (hWndMain) ;
  466.    WindowShow (hWndStatus, Options.bStatusbar) ;
  467.    WindowShow (hWndToolbar, Options.bToolbar) ;
  468.  
  469.    yViewHeight = yHeight - yStatusHeight - yToolbarHeight ;
  470.  
  471.    MoveWindow (hWndGraph, 
  472.                0, yToolbarHeight, 
  473.                xWidth, yViewHeight, 
  474.                TRUE) ;
  475.    MoveWindow (hWndAlert, 
  476.                0, yToolbarHeight, 
  477.                xWidth, yViewHeight, 
  478.                TRUE) ;
  479.    MoveWindow (hWndLog, 
  480.                0, yToolbarHeight, 
  481.                xWidth, yViewHeight, 
  482.                TRUE) ;
  483.    MoveWindow (hWndReport, 
  484.                0, yToolbarHeight, 
  485.                xWidth, yViewHeight, 
  486.                TRUE) ;
  487.  
  488.  
  489.    }  // SizePerfmonComponents
  490.  
  491.  
  492.  
  493.  
  494.