home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / os2apipm.zip / PMEXAM / HELLO / HELLO.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  14KB  |  345 lines

  1. /*************** Hello C Sample Program Source Code File (.C) **************
  2.  *
  3.  * PROGRAM NAME: HELLO
  4.  * -------------
  5.  *  Presentation Manager Standard Window C Sample Program
  6.  *
  7.  * REVISION LEVEL: 2.0
  8.  * ---------------
  9.  *
  10.  * WHAT THIS PROGRAM DOES:
  11.  * -----------------------
  12.  *  This program displays a standard window containing the text "Hello".
  13.  *  The action bar contains a single choice, Options.
  14.  *  The Options pull-down contains three choices that each
  15.  *  paint a different string in the window.
  16.  *
  17.  * WHAT THIS PROGRAM DEMONSTRATES:
  18.  * -------------------------------
  19.  *  This program demonstrates how to create and display a standard window,
  20.  *  and how to use resources defined in a resource script file.
  21.  *
  22.  * WHAT YOU NEED TO COMPILE THIS PROGRAM:
  23.  * --------------------------------------
  24.  *
  25.  *  REQUIRED FILES:
  26.  *  ---------------
  27.  *
  28.  *    HELLO.C       - Source code
  29.  *    HELLO.MAK     - Make file for this program
  30.  *    HELLO.DEF     - Module definition file
  31.  *    HELLO.H       - Application header file
  32.  *    HELLO.ICO     - Icon file
  33.  *    HELLO.L       - Linker automatic response file
  34.  *    HELLO.RC      - Resource file
  35.  *
  36.  *    OS2.H          - Presentation Manager include file
  37.  *    STRING.H       - String handling function declarations
  38.  *
  39.  *  REQUIRED LIBRARIES:
  40.  *  -------------------
  41.  *
  42.  *    OS2386.LIB     - Presentation Manager/OS2 library
  43.  *    LIBC.LIB       - Protect mode/standard combined small model C library
  44.  *
  45.  *  REQUIRED PROGRAMS:
  46.  *  ------------------
  47.  *    IBM C Compiler
  48.  *    IBM Linker
  49.  *    Resource Compiler
  50.  *
  51.  *  Copyright (C) 1991 IBM Corporation
  52.  *
  53.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  54.  *      sample code created by IBM Corporation. This sample code is not
  55.  *      part of any standard or IBM product and is provided to you solely
  56.  *      for  the purpose of assisting you in the development of your
  57.  *      applications.  The code is provided "AS IS", without
  58.  *      warranty of any kind.  IBM shall not be liable for any damages
  59.  *      arising out of your use of the sample code, even if they have been
  60.  *      advised of the possibility of such damages.                                                    *
  61.  ******************************************************************************/
  62. #define INCL_WIN
  63. #define INCL_GPI
  64.  
  65. #include <os2.h>                        /* PM header file               */
  66. #include "hello.h"                      /* Resource symbolic identifiers*/
  67.  
  68. #define STRINGLENGTH 20                 /* Length of string             */
  69.  
  70. /*
  71.  * Function prototypes
  72.  */
  73. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  74.  
  75.                                         /* Define parameters by type    */
  76. HAB  hab;                               /* PM anchor block handle       */
  77. CHAR szHello[STRINGLENGTH];             /* String parameters, set in    */
  78. CHAR sz1[STRINGLENGTH];                 /* the processing of WM_CREATE, */
  79. CHAR sz2[STRINGLENGTH];                 /* and used in the processing   */
  80. CHAR sz3[STRINGLENGTH];                 /* of WM_COMMAND, in window     */
  81. CHAR szString[STRINGLENGTH];            /* procedure.                   */
  82. PSZ  pszErrMsg;
  83.  
  84. /**************************************************************************
  85.  *
  86.  *  Name       : main()
  87.  *
  88.  *  Description: Initializes the process for OS/2 PM services and
  89.  *               process the application message queue until a
  90.  *               WM_QUIT message is received.  It then destroys all
  91.  *               OS/2 PM resources and terminates.
  92.  *
  93.  *  Concepts   : - obtains anchor block handle and creates message
  94.  *                   queue
  95.  *               - creates the main frame window which creates the
  96.  *                   main client window
  97.  *               - polls the message queue via Get/Dispatch Msg loop
  98.  *               - upon exiting the loop, exits
  99.  *
  100.  *  API's      :   WinInitialize
  101.  *                 WinCreateMsgQueue
  102.  *                 WinTerminate
  103.  *                 WinSetWindowPos
  104.  *                 WinSetWindowText
  105.  *                 WinRegisterClass
  106.  *                 WinCreateStdWindow
  107.  *                 WinGetMsg
  108.  *                 WinDispatchMsg
  109.  *                 WinDestroyWindow
  110.  *                 WinDestroyMsgQueue
  111.  *
  112.  *  Parameters :  [none]
  113.  *
  114.  *  Return     :  1 - if successful execution completed
  115.  *                0 - if error
  116.  *
  117.  *************************************************************************/
  118. INT main (VOID)
  119. {
  120.   HMQ  hmq;                             /* Message queue handle         */
  121.   HWND hwndClient = NULLHANDLE;         /* Client area window handle    */
  122.   HWND hwndFrame = NULLHANDLE;          /* Frame window handle          */
  123.   QMSG qmsg;                            /* Message from message queue   */
  124.   ULONG flCreate;                       /* Window creation control flags*/
  125.  
  126.   if ((hab = WinInitialize(0)) == 0L) /* Initialize PM     */
  127.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  128.  
  129.   if ((hmq = WinCreateMsgQueue( hab, 0 )) == 0L)/* Create a msg queue */
  130.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  131.  
  132.   if (!WinRegisterClass(                /* Register window class        */
  133.      hab,                               /* Anchor block handle          */
  134.      (PSZ)"MyWindow",                   /* Window class name            */
  135.      (PFNWP)MyWindowProc,               /* Address of window procedure  */
  136.      CS_SIZEREDRAW,                     /* Class style                  */
  137.      0                                  /* No extra window words        */
  138.      ))
  139.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  140.  
  141.    flCreate = FCF_STANDARD &            /* Set frame control flags to   */
  142.              ~FCF_SHELLPOSITION;        /* standard except for shell    */
  143.                                         /* positioning.                 */
  144.  
  145.   if ((hwndFrame = WinCreateStdWindow(
  146.                HWND_DESKTOP,            /* Desktop window is parent     */
  147.                0,                       /* STD. window styles           */
  148.                &flCreate,               /* Frame control flag           */
  149.                "MyWindow",              /* Client window class name     */
  150.                "",                      /* No window text               */
  151.                0,                       /* No special class style       */
  152.                (HMODULE)0L,           /* Resource is in .EXE file     */
  153.                ID_WINDOW,               /* Frame window identifier      */
  154.                &hwndClient              /* Client window handle         */
  155.                )) == 0L)
  156.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  157.  
  158.     WinSetWindowText(hwndFrame, "HELLO SAMPLE");
  159.  
  160.   if (!WinSetWindowPos( hwndFrame,      /* Shows and activates frame    */
  161.                    HWND_TOP,            /* window at position 100, 100, */
  162.                    100, 100, 200, 200,  /* and size 200, 200.           */
  163.                    SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW
  164.                  ))
  165.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  166.  
  167. /*
  168.  * Get and dispatch messages from the application message queue
  169.  * until WinGetMsg returns FALSE, indicating a WM_QUIT message.
  170.  */
  171.  
  172.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  173.     WinDispatchMsg( hab, &qmsg );
  174.   WinDestroyWindow(hwndFrame);           /* Tidy up...                   */
  175.   WinDestroyMsgQueue( hmq );             /* Tidy up...                   */
  176.   WinTerminate( hab );                   /* Terminate the application    */
  177. } /* End of main */
  178.  
  179. /**************************************************************************
  180.  *
  181.  *  Name       : MyWindowProc
  182.  *
  183.  *  Description: The window procedure associated with the client area in
  184.  *               the standard frame window. It processes all messages
  185.  *               either sent or posted to the client area, depending on
  186.  *               the message command and parameters.
  187.  *
  188.  *  Concepts   :
  189.  *
  190.  *  API's      :   WinLoadString
  191.  *                 WinInvalidateRegion
  192.  *                 WinPostMsg
  193.  *                 WinDefWindowProc
  194.  *                 WinBeginPaint
  195.  *                 GpiSetColor
  196.  *                 GpiSetBackColor
  197.  *                 GpiSetBackMix
  198.  *                 GpiCharStringAt
  199.  *                 WinEndPaint
  200.  *
  201.  *  Parameters :  hwnd = window handle
  202.  *                msg = message code
  203.  *                mp1 = first message parameter
  204.  *                mp2 = second message parameter
  205.  *
  206.  *  Return     :  depends on message sent
  207.  *
  208.  *************************************************************************/
  209. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  210. {
  211.   switch( msg )
  212.   {
  213.     case WM_CREATE:
  214.       /*
  215.        * Window initialization is performed here in WM_CREATE processing
  216.        * WinLoadString loads strings from the resource file.
  217.        */
  218.       WinLoadString( hab, (HMODULE)0L, IDS_HELLO, STRINGLENGTH, szHello );
  219.       WinLoadString( hab, (HMODULE)0L, IDS_1,     STRINGLENGTH, sz1     );
  220.       WinLoadString( hab, (HMODULE)0L, IDS_2,     STRINGLENGTH, sz2     );
  221.       WinLoadString( hab, (HMODULE)0L, IDS_3,     STRINGLENGTH, sz3     );
  222.       strcpy( szString, szHello );      /* Copy text Hello into szString*/
  223.       break;
  224.  
  225.     case WM_COMMAND:
  226.       /*
  227.        * When the user chooses option 1, 2, or 3 from the Options pull-
  228.        * down, the text string is set to 1, 2, or 3, and
  229.        * WinInvalidateRegion sends a WM_PAINT message.
  230.        * When Exit is chosen, the application posts itself a WM_CLOSE
  231.        * message.
  232.        */
  233.       {
  234.       USHORT command;                   /* WM_COMMAND command value     */
  235.       command = SHORT1FROMMP(mp1);      /* Extract the command value    */
  236.       switch (command)
  237.       {
  238.         case ID_OPTION1:
  239.           strcpy( szString, sz1 );
  240.           WinInvalidateRegion( hwnd, 0L, FALSE );
  241.           break;
  242.         case ID_OPTION2:
  243.           strcpy( szString, sz2 );
  244.           WinInvalidateRegion( hwnd, 0L, FALSE );
  245.           break;
  246.         case ID_OPTION3:
  247.           strcpy( szString, sz3 );
  248.           WinInvalidateRegion( hwnd, 0L, FALSE );
  249.           break;
  250.         case ID_EXITPROG:
  251.           WinPostMsg( hwnd, WM_CLOSE, (MPARAM)0, (MPARAM)0 );
  252.           break;
  253.         default:
  254.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  255.       }
  256.  
  257.       break;
  258.       }
  259.     case WM_ERASEBACKGROUND:
  260.       /*
  261.        * Return TRUE to request PM to paint the window background
  262.        * in SYSCLR_WINDOW.
  263.        */
  264.       return (MRESULT)( TRUE );
  265.     case WM_PAINT:
  266.       /*
  267.        * Window contents are drawn here in WM_PAINT processing.
  268.        */
  269.       {
  270.       HPS    hps;                       /* Presentation Space handle    */
  271.       RECTL  rc;                        /* Rectangle coordinates        */
  272.       POINTL pt;                        /* String screen coordinates    */
  273.                                         /* Create a presentation space  */
  274.       hps = WinBeginPaint( hwnd, 0L, &rc );
  275.       pt.x = 50; pt.y = 50;             /* Set the text coordinates,    */
  276.       GpiSetColor( hps, CLR_NEUTRAL );         /* colour of the text,   */
  277.       GpiSetBackColor( hps, CLR_BACKGROUND );  /* its background and    */
  278.       GpiSetBackMix( hps, BM_OVERPAINT );      /* how it mixes,         */
  279.                                                /* and draw the string...*/
  280.       GpiCharStringAt( hps, &pt, (LONG)strlen( szString ), szString );
  281.       WinEndPaint( hps );                      /* Drawing is complete   */
  282.       break;
  283.       }
  284.     case WM_CLOSE:
  285.       /*
  286.        * This is the place to put your termination routines
  287.        */
  288.       WinPostMsg( hwnd, WM_QUIT, (MPARAM)0,(MPARAM)0 );/* Cause termination*/
  289.       break;
  290.     default:
  291.       /*
  292.        * Everything else comes here.  This call MUST exist
  293.        * in your window procedure.
  294.        */
  295.  
  296.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  297.   }
  298.   return (MRESULT)FALSE;
  299. } /* End of MyWindowProc */
  300.  
  301. /**************************************************************************
  302.  *
  303.  *  Name       : AbortHello
  304.  *
  305.  *  Description: Report an error returned from an API service
  306.  *
  307.  *  Concepts   :  use of message box to display information
  308.  *
  309.  *  API's      :  DosBeep
  310.  *                WinGetErrorInfo
  311.  *                WinMessageBox
  312.  *                WinFreeErrorInfo
  313.  *                WinPostMsg
  314.  *
  315.  *  Parameters :  hwndFrame = frame window handle
  316.  *                hwndClient = client window handle
  317.  *
  318.  *  Return     :  [none]
  319.  *
  320.  *************************************************************************/
  321. VOID AbortHello(HWND hwndFrame, HWND hwndClient)
  322. {
  323.    PERRINFO  pErrInfoBlk;
  324.    PSZ       pszOffSet;
  325.    void      stdprint(void);
  326.  
  327.    DosBeep(100,10);
  328.    if ((pErrInfoBlk = WinGetErrorInfo(hab)) != (PERRINFO)NULL)
  329.    {
  330.       pszOffSet = ((PSZ)pErrInfoBlk) + pErrInfoBlk->offaoffszMsg;
  331.       pszErrMsg = ((PSZ)pErrInfoBlk) + *((PSHORT)pszOffSet);
  332.       if((INT)hwndFrame && (INT)hwndClient)
  333.          WinMessageBox(HWND_DESKTOP,         /* Parent window is desk top */
  334.                        hwndFrame,            /* Owner window is our frame */
  335.                        (PSZ)pszErrMsg,       /* PMWIN Error message       */
  336.                        "Error Msg",          /* Title bar message         */
  337.                        MSGBOXID,             /* Message identifier        */
  338.                        MB_MOVEABLE | MB_CUACRITICAL | MB_CANCEL ); /* Flags */
  339.       WinFreeErrorInfo(pErrInfoBlk);
  340.    }
  341.    WinPostMsg(hwndClient, WM_QUIT, (MPARAM)NULL, (MPARAM)NULL);
  342. } /* End of AbortHello */
  343.  
  344. /*********************** End of the hello.c *******************************/
  345.