home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / comm / builddcb.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.2 KB  |  197 lines

  1. /*
  2.  *
  3.  * Function(s) demonstrated in this program: BuildCommDCB
  4.  * 
  5.  * Windows version:  2.03
  6.  * 
  7.  * Windows SDK version:  2.00
  8.  * 
  9.  * Compiler version:  C 5.10
  10.  * 
  11.  * Description:  This function translates the definition string specified
  12.  *    by the parameter into appropriate device-control block codes and places
  13.  *     these codes in the device control block specified by the other parmeter.
  14.  * 
  15.  * Additional Comments:
  16.  * 
  17.  */
  18.  
  19. #define NOMINMAX
  20. #include <windows.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "BuildDCB.h"
  24.  
  25.  
  26. HWND     hWndParent1;
  27. HANDLE   hInstMain;
  28.  
  29. char    szOutputBuffer1 [70];
  30. char    szOutputBuffer2 [500];
  31. char    szMode          [40] = "COM1:9600,N,8,1";
  32.  
  33.  
  34. /****************************************************************************/
  35. /************************    Message Structure      *************************/
  36. /****************************************************************************/
  37.  
  38. struct { 
  39.   char    *szMessage; 
  40. } Messages [] = {
  41.   "About\0",
  42.   "     This is a sample application to demonstrate the\n\
  43. use of the BuildCommDCB Windows function.",
  44.  
  45.   "Help Message",
  46.   "     This program uses the BuildCommDCB Windows\n\
  47. function to build a device control block from the\n\
  48. given string.  Use the menu to invoke this function.",
  49.  
  50. };
  51.  
  52.  
  53. /****************************************************************************/
  54.  
  55. void ProcessMessage (HWND, int);
  56.  
  57. void ProcessMessage (hWnd, MessageNumber)
  58. HWND     hWnd;
  59. int    MessageNumber;
  60. {
  61.   sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  62.   sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  63.   MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  64. }
  65.  
  66.  
  67. /****************************************************************************/
  68.  
  69. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  70. HANDLE      hInstance, hPrevInstance ;
  71. LPSTR       lpszCmdLine ;
  72. int    nCmdShow ;
  73. {
  74.   static char    szAppName [] = "BuildDCB" ;
  75.   HWND        hWnd ;
  76.   WNDCLASS    wndclass ;
  77.   MSG msg;
  78.   short    xScreen, yScreen ;
  79.  
  80.   if (!hPrevInstance)
  81.   {
  82.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  83.     wndclass.lpfnWndProc   = WndProc ;
  84.     wndclass.cbClsExtra    = 0 ;
  85.     wndclass.cbWndExtra    = 0 ;
  86.     wndclass.hInstance     = hInstance ;
  87.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  88.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  89.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  90.     wndclass.lpszMenuName  = szAppName ;
  91.     wndclass.lpszClassName = szAppName ;
  92.  
  93.     if (!RegisterClass (&wndclass))
  94.       return FALSE ;
  95.   }
  96.  
  97.   xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  98.   yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  99.  
  100. hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  101. "BuildCommDCB",             /* window caption          */
  102. WS_OVERLAPPEDWINDOW,        /* window style            */
  103. CW_USEDEFAULT,              /* initial x position      */
  104. 0,                          /* initial y position      */
  105. CW_USEDEFAULT,              /* initial x size          */
  106. 0,                          /* initial y size          */
  107. NULL,                       /* parent window handle    */
  108. NULL,                       /* window menu handle      */
  109. hInstance,                  /* program instance handle */
  110.   NULL) ;                     /* create parameters       */
  111.  
  112.   ShowWindow (hWndParent1, nCmdShow) ;
  113.   UpdateWindow (hWndParent1) ;
  114.  
  115.   hInstMain = hInstance;
  116.  
  117.   while (GetMessage(&msg, NULL, 0, 0))
  118.   {
  119.     TranslateMessage(&msg);
  120.     DispatchMessage(&msg);
  121.   }
  122.   return (msg.wParam) ;
  123. }
  124.  
  125.  
  126. /****************************************************************************/
  127.  
  128. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  129. HWND     hWnd ;
  130. unsigned    iMessage ;
  131. WORD     wParam ;
  132. LONG     lParam ;
  133. {
  134.   HMENU       hMenu;
  135.   HDC         hDC;
  136.   DCB         CommDCB;
  137.   int    ComVal;
  138.   PAINTSTRUCT ps;
  139.  
  140.   switch (iMessage)
  141.   {
  142.   case WM_CREATE:
  143.     hMenu = GetSystemMenu (hWnd, FALSE);
  144.  
  145.     ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  146.         MF_APPEND | MF_STRING);
  147.     break;
  148.  
  149.   case WM_SYSCOMMAND:
  150.     switch (wParam) 
  151.     {
  152.     case IDM_ABOUT:
  153.       ProcessMessage (hWnd, 0);
  154.       break;
  155.     default:
  156.       return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  157.     }
  158.     break;
  159.  
  160.   case WM_COMMAND:
  161.     switch (wParam) 
  162.     {
  163.     case IDM_BUILDDCB:
  164.       sprintf(szOutputBuffer1, "Definition string = COM1:9600,N,8,1");
  165.       MessageBox (hWnd, szOutputBuffer1, "BuildCommDCB", MB_OK);
  166.       ComVal = BuildCommDCB ((LPSTR)szMode, (DCB FAR * ) & CommDCB);
  167.       sprintf (szOutputBuffer2, "%s%s%s%i", 
  168.           "     If the return value is 0 then the BuildCommDCB ",
  169.           "function was performed correctly.\n\n",
  170.           "               The return value = ", ComVal);
  171.       MessageBox (hWnd, szOutputBuffer2, "BuildCommDCB", MB_OK);
  172.       break;
  173.  
  174.     case IDM_HELP:
  175.       ProcessMessage (hWnd, 2);
  176.       break;
  177.     }
  178.     break;
  179.  
  180.   case WM_PAINT:
  181.     BeginPaint(hWnd, (LPPAINTSTRUCT) & ps);
  182.     EndPaint(hWnd, (LPPAINTSTRUCT) & ps);
  183.     break;
  184.  
  185.   case WM_DESTROY:
  186.     PostQuitMessage(0);
  187.     break;
  188.  
  189.   default:
  190.     return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  191.   }
  192.   return (0L);
  193. }
  194.  
  195.  
  196.  
  197.