home *** CD-ROM | disk | FTP | other *** search
/ PC Gamer 3.9 / 1997-12_Disc_3.9_No._2.bin / MSPRING / INSTALL / 16BIT / DISK1.Z / MDMSTAT.EXP < prev    next >
Text File  |  1995-01-09  |  11KB  |  405 lines

  1. #include "pp_api.h" /* #### AutoPinPointed #### */                              
  2. /*
  3.  * TCP/Entry
  4.  * TCP/IP Services for Microsoft Windows 3.1
  5.  *
  6.  * Copyright 1995 by Network TeleSystems, Inc.
  7.  * All rights reserved.
  8.  *
  9.  * mdmstat.exp
  10.  * SLIP/PPP Dialer Modem Status Example Source.
  11.  *   NOT guaranteed to compile.
  12.  */
  13.  
  14. #include <windows.h>
  15. #include <windowsx.h>
  16.  
  17. #include "ntsms.h"
  18.  
  19. /* ------------------------------------------------------------------ *
  20.  *    Globals.                                                          *
  21.  * ------------------------------------------------------------------ */
  22. static    BOOL        fEverConnected = FALSE;
  23.  
  24. static    HBITMAP        hBitMapModemStatus = NULL;
  25. static    HINSTANCE     hInst = NULL;
  26. static    HWND        hWndMain = NULL;
  27. static    HCL            hCl = NULL;
  28.  
  29. #define    WM_USER_MODEMSTATUS            (WM_USER+1)
  30.  
  31. /* ------------------------------------------------------------------ *
  32.  *    Modem Status Lights.                                              *
  33.  * ------------------------------------------------------------------ */
  34. #define    MODEMSTATUS_WANT    (MODEMSTATUS_RTS | MODEMSTATUS_DCD    | \
  35.                              MODEMSTATUS_CTS | MODEMSTATUS_RI     | \
  36.                              MODEMSTATUS_TX  | MODEMSTATUS_RX     | \
  37.                              MODEMSTATUS_DIALERACTIVE)
  38.  
  39. #define MSLIGHT_HEIGHT        2
  40. #define    MSLIGHT_WIDTH        9
  41. #define    MSLIGHT_TOP            6
  42. #define MSLIGHT_INTERVAL    16
  43. #define    MSLIGHT_RI            6
  44. #define    MSLIGHT_CD            (MSLIGHT_RI+MSLIGHT_INTERVAL)
  45. #define    MSLIGHT_RS            (MSLIGHT_CD+MSLIGHT_INTERVAL)
  46. #define    MSLIGHT_CS            (MSLIGHT_RS+MSLIGHT_INTERVAL)
  47. #define    MSLIGHT_RD            (MSLIGHT_CS+MSLIGHT_INTERVAL)
  48. #define    MSLIGHT_SD            (MSLIGHT_RD+MSLIGHT_INTERVAL)
  49.  
  50. static    UMODEMSTATUS        uModemStatus = MODEMSTATUS_NULL;
  51. static    UMODEMSTATUS        uLastModemStatus = MODEMSTATUS_NULL;
  52.  
  53. static    COLORREF            OnColor;
  54. static    COLORREF            OffColor;
  55. static    HBRUSH                hBrushOn = NULL;
  56. static    HBRUSH                hBrushOff = NULL;
  57.  
  58. /* ================================================================== *
  59.  *    Strings.                                                          *
  60.  * ================================================================== */
  61. static    char    szOFFCOLORSTR[]            = {"192,192,192"};
  62. static    char    szONCOLORSTR[]            = {"255,0,0"};
  63.  
  64. /* ------------------------------------------------------------------ *
  65.  * Forwards.                                                          *
  66.  * ------------------------------------------------------------------ */
  67.  
  68. LRESULT 
  69. CALLBACK        ModemStatusWndProc(
  70.                     HWND hWnd, 
  71.                     UINT message, 
  72.                     WPARAM wParam, 
  73.                     LPARAM lParam
  74.                     );
  75.  
  76. /* ================================================================== *
  77.  *    Initialization                                                      *
  78.  * ================================================================== */
  79.  
  80. /* ------------------------------------------------------------------ *
  81.  *    AppFinalize - perform one time finalization.                      *                          *
  82.  * ------------------------------------------------------------------ */
  83. static 
  84. void            AppFinalize(
  85.                     void
  86.                     )
  87.  
  88. {
  89.     if (hWndMain != NULL)
  90.         RegisterModemStatusMessage(hWndMain, 0, MODEMSTATUS_NULL);
  91.     hWndMain = NULL;
  92.     if (hBitMapModemStatus != NULL)
  93.         DeleteObject(hBitMapModemStatus);
  94.     if (hBrushOn != NULL);
  95.         DeleteObject(hBrushOn);
  96.     if (hBrushOff != NULL);
  97.         DeleteObject(hBrushOff);
  98.     UnregisterClass((LPSTR)szMainClassName, hInst);
  99. } /*  AppFinalize */
  100.  
  101. /* ------------------------------------------------------------------ *
  102.  *    AppInitialize - Register window classes and perform other one       *
  103.  *    time initialization.                                              *
  104.  * ------------------------------------------------------------------ */
  105. static 
  106. BOOL            AppInitialize(
  107.                     char *Session,
  108.                     int cmdShow
  109.                     )
  110.  
  111. {
  112.     BITMAP        bm;
  113.     char        *szPtr;
  114.     char        szVersion[32];
  115.     HVERS        hVers;
  116.     RECT        rectMain;
  117.     WNDCLASS    WndClass;
  118.  
  119. #ifndef WIN32
  120.     if ((GetWinFlags()&WF_PMODE) ==    0) 
  121.     {
  122.         return FALSE;
  123.     }
  124. #endif
  125.  
  126.     hBitMapModemStatus = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_MDMSTAT));
  127.     if (hBitMapModemStatus == NULL)
  128.     {
  129.         return FALSE;
  130.     } 
  131.  
  132.     GetObject (hBitMapModemStatus, sizeof (BITMAP), (LPSTR) &bm);
  133.     ColorStrToCOLORREF((COLORREF*)&(OnColor),    szONCOLORSTR);
  134.     ColorStrToCOLORREF((COLORREF*)&(OffColor),    szOFFCOLORSTR);
  135.     hBrushOn = CreateSolidBrush(OnColor);
  136.     hBrushOff = CreateSolidBrush(OffColor);
  137.  
  138.        /* Register window class for main window */
  139.     memset((void*)&WndClass, 0, sizeof(WndClass));
  140.     WndClass.lpszClassName     = (LPSTR)szMainClassName;
  141.     WndClass.hInstance         = hInst;
  142.     WndClass.lpfnWndProc     = (WNDPROC)ModemStatusWndProc;
  143.     if (!RegisterClass((LPWNDCLASS)&WndClass)) 
  144.     {
  145.         return FALSE;
  146.     }
  147.  
  148.     /* Create the main window */
  149.     hWndMain = 
  150.         CreateWindow((LPSTR)szMainClassName,
  151.             (LPSTR)szWinTitle,
  152.             WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION,
  153.             rectMain.left,
  154.             rectMain.top,
  155.             bm.bmWidth + (2 * GetSystemMetrics(SM_CXBORDER)),
  156.             bm.bmHeight + GetSystemMetrics(SM_CYCAPTION) + (GetSystemMetrics(SM_CYBORDER)),
  157.             (HWND)0,
  158.             (HMENU)0,
  159.             (HINSTANCE)hInst,
  160.             (LPVOID)NULL);
  161.     if (hWndMain == NULL)
  162.     {
  163.         AppFinalize();
  164.         return FALSE;
  165.     }
  166.  
  167.     if (!RegisterModemStatusMessage(hWndMain, WM_USER_MODEMSTATUS, MODEMSTATUS_WANT))
  168.     {
  169.         return FALSE;
  170.     }
  171.  
  172.     ShowWindow(hWndMain, SW_SHOWNOACTIVATE);
  173.     UpdateWindow(hWndMain);
  174.  
  175.     return TRUE;            /* Initialization succeeded */
  176. } /* AppInitialize */
  177.  
  178. /* ------------------------------------------------------------------ *
  179.  *    WinMain - The Main Function.                                      *
  180.  * ------------------------------------------------------------------ */
  181. int PASCAL        WinMain(
  182.                     HINSTANCE hInstance, 
  183.                     HANDLE hPrevInstance, 
  184.                     LPSTR lpszCmdLine, 
  185.                     int cmdShow
  186.                     )
  187.  
  188. {
  189.     HWND        hWndFirst;
  190.     MSG            msg;
  191.  
  192.     /* see if app is already running. */
  193.     if ((hWndFirst = FindWindow(szMainClassName, NULL)) != NULL)
  194.     {
  195.         return FALSE;
  196.     }
  197.  
  198.     /* Save Instance Handle */
  199.     hInst =    hInstance;
  200.  
  201.     if (!AppInitialize(lpszCmdLine, cmdShow))    
  202.     {
  203.         /* failed to initialize */
  204.         AppFinalize();
  205.         return FALSE;
  206.     }
  207.  
  208.     while (GetMessage((LPMSG)&msg, (HWND)0,    0, 0)) 
  209.     {
  210.         TranslateMessage(&msg);
  211.         DispatchMessage(&msg);
  212.     }
  213.  
  214.     AppFinalize();
  215.  
  216.     return (int)msg.wParam;
  217. } /* WinMain */
  218.  
  219. /* ------------------------------------------------------------------ *
  220.  * DrawBitmap - from Programming Windows 3.1, page 631.                  *
  221.  * ------------------------------------------------------------------ */
  222. static
  223. void             DrawBitmap(
  224.                     HDC hdc, 
  225.                     HBITMAP hBitmap, 
  226.                     short xStart, 
  227.                     short yStart
  228.                     )
  229.  
  230. {
  231.     BITMAP    bm;
  232.     HDC       hdcMem;
  233.     POINT     ptSize, ptOrg;
  234.  
  235.     hdcMem = CreateCompatibleDC (hdc);
  236.     SelectObject (hdcMem, hBitmap);
  237.     SetMapMode (hdcMem, GetMapMode (hdc));
  238.  
  239.     GetObject (hBitmap, sizeof (BITMAP), (LPSTR) &bm);
  240.     ptSize.x = bm.bmWidth;
  241.     ptSize.y = bm.bmHeight;
  242.     DPtoLP (hdc, &ptSize, 1);
  243.  
  244.     ptOrg.x = 0;
  245.     ptOrg.y = 0;
  246.     DPtoLP (hdcMem, &ptOrg, 1);
  247.  
  248.     BitBlt (hdc, xStart, yStart, ptSize.x, ptSize.y,
  249.             hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);
  250.  
  251.     DeleteDC (hdcMem);
  252. } /* DrawBitmap */
  253.  
  254. /* ------------------------------------------------------------------ *
  255.  * InvalidateModemStatusLight                                             *
  256.  * ------------------------------------------------------------------ */
  257. static
  258. void            InvalidateModemStatusLight(
  259.                     UINT    uLeft
  260.                     )
  261.  
  262. {
  263.     RECT    rect;
  264.  
  265.     rect.top    = MSLIGHT_TOP;
  266.     rect.bottom    = MSLIGHT_TOP +    MSLIGHT_HEIGHT;
  267.     rect.left    = uLeft;
  268.     rect.right    = uLeft + MSLIGHT_WIDTH;
  269.     InvalidateRect(hWndMain, &rect, FALSE /* fErase */);
  270. } /* InvalidateModemStatusLight */
  271.  
  272. /* ------------------------------------------------------------------ *
  273.  * SetModemStatusLight                                                  *
  274.  * ------------------------------------------------------------------ */
  275. static
  276. void            SetModemStatusLight(
  277.                     HDC        hDC,
  278.                     UINT    uLeft,
  279.                     BOOL    fState
  280.                     )
  281.  
  282. {
  283.     RECT    rect;
  284.  
  285.     rect.top    = MSLIGHT_TOP;
  286.     rect.bottom    = MSLIGHT_TOP +    MSLIGHT_HEIGHT;
  287.     rect.left    = uLeft;
  288.     rect.right    = uLeft + MSLIGHT_WIDTH;
  289.     if (fState)
  290.         FillRect(hDC, &rect, hBrushOn);
  291.     else
  292.         FillRect(hDC, &rect, hBrushOff);
  293. } /* SetModemStatusLight */
  294.  
  295. /* ------------------------------------------------------------------ *
  296.  * ModemStatusWndProc - Window function for 'ModemStatus' window      *
  297.  * ------------------------------------------------------------------ */
  298. LRESULT 
  299. CALLBACK        ModemStatusWndProc(
  300.                     HWND hWnd, 
  301.                     UINT message, 
  302.                     WPARAM wParam, 
  303.                      LPARAM lParam
  304.                     )
  305.  
  306. {
  307.     HDC            hDC;
  308.     PAINTSTRUCT ps;
  309.  
  310.     switch (message) 
  311.     {
  312.         case WM_CLOSE:
  313.             WriteAppSectionToINI();
  314.             DestroyWindow(hWnd);
  315.             break;
  316.  
  317.         case WM_DESTROY:
  318.             PostQuitMessage(0);
  319.             return 0;
  320.  
  321.         case WM_PAINT:
  322.             hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  323.             if (ps.fErase) 
  324.             {
  325.                  DrawBitmap(hDC, hBitMapModemStatus, 0, 0);
  326.                 uLastModemStatus = MODEMSTATUS_NULL;
  327.             }
  328.  
  329.             if ((uLastModemStatus & MODEMSTATUS_RI) !=
  330.                 (uModemStatus & MODEMSTATUS_RI))
  331.                 SetModemStatusLight(hDC, MSLIGHT_RI, ((uModemStatus & MODEMSTATUS_RI) != 0));
  332.  
  333.             if ((uLastModemStatus & MODEMSTATUS_DCD) !=
  334.                 (uModemStatus & MODEMSTATUS_DCD))
  335.                 SetModemStatusLight(hDC, MSLIGHT_CD, ((uModemStatus & MODEMSTATUS_DCD) != 0));
  336.  
  337.             if ((uLastModemStatus & MODEMSTATUS_RTS) !=
  338.                 (uModemStatus & MODEMSTATUS_RTS))
  339.                 SetModemStatusLight(hDC, MSLIGHT_RS, ((uModemStatus & MODEMSTATUS_RTS) != 0));
  340.  
  341.             if ((uLastModemStatus & MODEMSTATUS_CTS) !=
  342.                 (uModemStatus & MODEMSTATUS_CTS))
  343.                 SetModemStatusLight(hDC, MSLIGHT_CS, ((uModemStatus & MODEMSTATUS_CTS) != 0));
  344.  
  345.             if ((uLastModemStatus & MODEMSTATUS_RX) !=
  346.                 (uModemStatus & MODEMSTATUS_RX))
  347.                 SetModemStatusLight(hDC, MSLIGHT_RD, ((uModemStatus & MODEMSTATUS_RX) != 0));
  348.  
  349.             if ((uLastModemStatus & MODEMSTATUS_TX) !=
  350.                 (uModemStatus & MODEMSTATUS_TX))
  351.                 SetModemStatusLight(hDC, MSLIGHT_SD, ((uModemStatus & MODEMSTATUS_TX) != 0));
  352.  
  353.             uLastModemStatus = uModemStatus;
  354.             EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  355.  
  356.             break;
  357.  
  358.         case WM_USER_MODEMSTATUS:
  359.             uModemStatus = wParam;            
  360.  
  361.             if ((uLastModemStatus & MODEMSTATUS_RI) !=
  362.                 (uModemStatus & MODEMSTATUS_RI))
  363.                 InvalidateModemStatusLight(MSLIGHT_RI);
  364.  
  365.             if ((uLastModemStatus & MODEMSTATUS_DCD) !=
  366.                 (uModemStatus & MODEMSTATUS_DCD))
  367.                 InvalidateModemStatusLight(MSLIGHT_CD);
  368.  
  369.             if ((uLastModemStatus & MODEMSTATUS_RTS) !=
  370.                 (uModemStatus & MODEMSTATUS_RTS))
  371.                 InvalidateModemStatusLight(MSLIGHT_RS);
  372.  
  373.             if ((uLastModemStatus & MODEMSTATUS_CTS) !=
  374.                 (uModemStatus & MODEMSTATUS_CTS))
  375.                 InvalidateModemStatusLight(MSLIGHT_CS);
  376.  
  377.             if ((uLastModemStatus & MODEMSTATUS_RX) !=
  378.                 (uModemStatus & MODEMSTATUS_RX))
  379.                 InvalidateModemStatusLight(MSLIGHT_RD);
  380.  
  381.             if ((uLastModemStatus & MODEMSTATUS_TX) !=
  382.                 (uModemStatus & MODEMSTATUS_TX))
  383.                 InvalidateModemStatusLight(MSLIGHT_SD);
  384.             UpdateWindow(hWndMain);
  385.  
  386.             if (uModemStatus & MODEMSTATUS_DCD)
  387.                fEverConnected = TRUE;
  388.  
  389.             if (fEverConnected)
  390.                 if ((uModemStatus & (MODEMSTATUS_DCD | MODEMSTATUS_DIALERACTIVE)) == 0)
  391.                 {
  392.                     /* cd gone and dialer inactive.  Stop. */
  393.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  394.                 }
  395.             break;
  396.  
  397.         default:
  398.             return DefWindowProc(hWnd, message, wParam, lParam);
  399.     }
  400.  
  401.     return 0L;
  402. } /* ModemStatusWndProc */
  403.  
  404.  
  405.