home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / pm / stdwnd / hello.c next >
C/C++ Source or Header  |  1999-05-11  |  14KB  |  349 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 <string.h>
  67. #include "hello.h"                      /* Resource symbolic identifiers*/
  68.  
  69. #define STRINGLENGTH 20                 /* Length of string             */
  70.  
  71. /*
  72.  * Function prototypes
  73.  */
  74. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  75.  
  76.                                         /* Define parameters by type    */
  77. HAB  hab;                               /* PM anchor block handle       */
  78. CHAR szHello[STRINGLENGTH];             /* String parameters, set in    */
  79. CHAR sz1[STRINGLENGTH];                 /* the processing of WM_CREATE, */
  80. CHAR sz2[STRINGLENGTH];                 /* and used in the processing   */
  81. CHAR sz3[STRINGLENGTH];                 /* of WM_COMMAND, in window     */
  82. CHAR szString[STRINGLENGTH];            /* procedure.                   */
  83. PSZ  pszErrMsg;
  84.  
  85. /**************************************************************************
  86.  *
  87.  *  Name       : main()
  88.  *
  89.  *  Description: Initializes the process for OS/2 PM services and
  90.  *               process the application message queue until a
  91.  *               WM_QUIT message is received.  It then destroys all
  92.  *               OS/2 PM resources and terminates.
  93.  *
  94.  *  Concepts   : - obtains anchor block handle and creates message
  95.  *                   queue
  96.  *               - creates the main frame window which creates the
  97.  *                   main client window
  98.  *               - polls the message queue via Get/Dispatch Msg loop
  99.  *               - upon exiting the loop, exits
  100.  *
  101.  *  API's      :   WinInitialize
  102.  *                 WinCreateMsgQueue
  103.  *                 WinTerminate
  104.  *                 WinSetWindowPos
  105.  *                 WinSetWindowText
  106.  *                 WinRegisterClass
  107.  *                 WinCreateStdWindow
  108.  *                 WinGetMsg
  109.  *                 WinDispatchMsg
  110.  *                 WinDestroyWindow
  111.  *                 WinDestroyMsgQueue
  112.  *
  113.  *  Parameters :  [none]
  114.  *
  115.  *  Return     :  1 - if successful execution completed
  116.  *                0 - if error
  117.  *
  118.  *************************************************************************/
  119. INT main (VOID)
  120. {
  121.   HMQ  hmq;                             /* Message queue handle         */
  122.   HWND hwndClient = NULLHANDLE;         /* Client area window handle    */
  123.   HWND hwndFrame = NULLHANDLE;          /* Frame window handle          */
  124.   QMSG qmsg;                            /* Message from message queue   */
  125.   ULONG flCreate;                       /* Window creation control flags*/
  126.  
  127.   if ((hab = WinInitialize(0)) == 0L) /* Initialize PM     */
  128.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  129.  
  130.   if ((hmq = WinCreateMsgQueue( hab, 0 )) == 0L)/* Create a msg queue */
  131.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  132.  
  133.   if (!WinRegisterClass(                /* Register window class        */
  134.      hab,                               /* Anchor block handle          */
  135.      (PSZ)"MyWindow",                   /* Window class name            */
  136.      (PFNWP)MyWindowProc,               /* Address of window procedure  */
  137.      CS_SIZEREDRAW,                     /* Class style                  */
  138.      0                                  /* No extra window words        */
  139.      ))
  140.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  141.  
  142.    flCreate = FCF_STANDARD &            /* Set frame control flags to   */
  143.              ~FCF_SHELLPOSITION;        /* standard except for shell    */
  144.                                         /* positioning.                 */
  145.  
  146.   if ((hwndFrame = WinCreateStdWindow(
  147.                HWND_DESKTOP,            /* Desktop window is parent     */
  148.                0,                       /* STD. window styles           */
  149.                &flCreate,               /* Frame control flag           */
  150.                "MyWindow",              /* Client window class name     */
  151.                "",                      /* No window text               */
  152.                0,                       /* No special class style       */
  153.                (HMODULE)0L,           /* Resource is in .EXE file     */
  154.                ID_WINDOW,               /* Frame window identifier      */
  155.                &hwndClient              /* Client window handle         */
  156.                )) == 0L)
  157.      AbortHello(HWND_DESKTOP, HWND_DESKTOP); /* Terminate the application    */
  158.  
  159.     WinSetWindowText(hwndFrame, "HELLO SAMPLE");
  160.  
  161.   if (!WinSetWindowPos( hwndFrame,      /* Shows and activates frame    */
  162.                    HWND_TOP,            /* window at position 100, 100, */
  163.                    100, 100, 200, 200,  /* and size 200, 200.           */
  164.                    SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW
  165.                  ))
  166.      AbortHello(hwndFrame, hwndClient); /* Terminate the application    */
  167.  
  168. /*
  169.  * Get and dispatch messages from the application message queue
  170.  * until WinGetMsg returns FALSE, indicating a WM_QUIT message.
  171.  */
  172.  
  173.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  174.     WinDispatchMsg( hab, &qmsg );
  175.   WinDestroyWindow(hwndFrame);           /* Tidy up...                   */
  176.   WinDestroyMsgQueue( hmq );             /* Tidy up...                   */
  177.   WinTerminate( hab );                   /* Terminate the application    */
  178.  
  179.   return(0);
  180. } /* End of main */
  181.  
  182. /**************************************************************************
  183.  *
  184.  *  Name       : MyWindowProc
  185.  *
  186.  *  Description: The window procedure associated with the client area in
  187.  *               the standard frame window. It processes all messages
  188.  *               either sent or posted to the client area, depending on
  189.  *               the message command and parameters.
  190.  *
  191.  *  Concepts   :
  192.  *
  193.  *  API's      :   WinLoadString
  194.  *                 WinInvalidateRegion
  195.  *                 WinPostMsg
  196.  *                 WinDefWindowProc
  197.  *                 WinBeginPaint
  198.  *                 GpiSetColor
  199.  *                 GpiSetBackColor
  200.  *                 GpiSetBackMix
  201.  *                 GpiCharStringAt
  202.  *                 WinEndPaint
  203.  *
  204.  *  Parameters :  hwnd = window handle
  205.  *                msg = message code
  206.  *                mp1 = first message parameter
  207.  *                mp2 = second message parameter
  208.  *
  209.  *  Return     :  depends on message sent
  210.  *
  211.  *************************************************************************/
  212. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  213. {
  214.   switch( msg )
  215.   {
  216.     case WM_CREATE:
  217.       /*
  218.        * Window initialization is performed here in WM_CREATE processing
  219.        * WinLoadString loads strings from the resource file.
  220.        */
  221.       WinLoadString( hab, (HMODULE)0L, IDS_HELLO, STRINGLENGTH, szHello );
  222.       WinLoadString( hab, (HMODULE)0L, IDS_1,     STRINGLENGTH, sz1     );
  223.       WinLoadString( hab, (HMODULE)0L, IDS_2,     STRINGLENGTH, sz2     );
  224.       WinLoadString( hab, (HMODULE)0L, IDS_3,     STRINGLENGTH, sz3     );
  225.       strcpy( szString, szHello );      /* Copy text Hello into szString*/
  226.       break;
  227.  
  228.     case WM_COMMAND:
  229.       /*
  230.        * When the user chooses option 1, 2, or 3 from the Options pull-
  231.        * down, the text string is set to 1, 2, or 3, and
  232.        * WinInvalidateRegion sends a WM_PAINT message.
  233.        * When Exit is chosen, the application posts itself a WM_CLOSE
  234.        * message.
  235.        */
  236.       {
  237.       USHORT command;                   /* WM_COMMAND command value     */
  238.       command = SHORT1FROMMP(mp1);      /* Extract the command value    */
  239.       switch (command)
  240.       {
  241.         case ID_OPTION1:
  242.           strcpy( szString, sz1 );
  243.           WinInvalidateRegion( hwnd, 0L, FALSE );
  244.           break;
  245.         case ID_OPTION2:
  246.           strcpy( szString, sz2 );
  247.           WinInvalidateRegion( hwnd, 0L, FALSE );
  248.           break;
  249.         case ID_OPTION3:
  250.           strcpy( szString, sz3 );
  251.           WinInvalidateRegion( hwnd, 0L, FALSE );
  252.           break;
  253.         case ID_EXITPROG:
  254.           WinPostMsg( hwnd, WM_CLOSE, (MPARAM)0, (MPARAM)0 );
  255.           break;
  256.         default:
  257.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  258.       }
  259.  
  260.       break;
  261.       }
  262.     case WM_ERASEBACKGROUND:
  263.       /*
  264.        * Return TRUE to request PM to paint the window background
  265.        * in SYSCLR_WINDOW.
  266.        */
  267.       return (MRESULT)( TRUE );
  268.     case WM_PAINT:
  269.       /*
  270.        * Window contents are drawn here in WM_PAINT processing.
  271.        */
  272.       {
  273.       HPS    hps;                       /* Presentation Space handle    */
  274.       RECTL  rc;                        /* Rectangle coordinates        */
  275.       POINTL pt;                        /* String screen coordinates    */
  276.                                         /* Create a presentation space  */
  277.       hps = WinBeginPaint( hwnd, 0L, &rc );
  278.       WinFillRect( hps, &rc, SYSCLR_WINDOW);
  279.       pt.x = 50; pt.y = 50;             /* Set the text coordinates,    */
  280.       GpiSetColor( hps, CLR_NEUTRAL );         /* colour of the text,   */
  281.       GpiSetBackColor( hps, CLR_BACKGROUND );  /* its background and    */
  282.       GpiSetBackMix( hps, BM_OVERPAINT );      /* how it mixes,         */
  283.                                                /* and draw the string...*/
  284.       GpiCharStringAt( hps, &pt, (LONG)strlen( szString ), szString );
  285.       WinEndPaint( hps );                      /* Drawing is complete   */
  286.       break;
  287.       }
  288.     case WM_CLOSE:
  289.       /*
  290.        * This is the place to put your termination routines
  291.        */
  292.       WinPostMsg( hwnd, WM_QUIT, (MPARAM)0,(MPARAM)0 );/* Cause termination*/
  293.       break;
  294.     default:
  295.       /*
  296.        * Everything else comes here.  This call MUST exist
  297.        * in your window procedure.
  298.        */
  299.  
  300.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  301.   }
  302.   return (MRESULT)FALSE;
  303. } /* End of MyWindowProc */
  304.  
  305. /**************************************************************************
  306.  *
  307.  *  Name       : AbortHello
  308.  *
  309.  *  Description: Report an error returned from an API service
  310.  *
  311.  *  Concepts   :  use of message box to display information
  312.  *
  313.  *  API's      :  DosBeep
  314.  *                WinGetErrorInfo
  315.  *                WinMessageBox
  316.  *                WinFreeErrorInfo
  317.  *                WinPostMsg
  318.  *
  319.  *  Parameters :  hwndFrame = frame window handle
  320.  *                hwndClient = client window handle
  321.  *
  322.  *  Return     :  [none]
  323.  *
  324.  *************************************************************************/
  325. VOID AbortHello(HWND hwndFrame, HWND hwndClient)
  326. {
  327.    PERRINFO  pErrInfoBlk;
  328.    PSZ       pszOffSet;
  329.    void      stdprint(void);
  330.  
  331.    DosBeep(100,10);
  332.    if ((pErrInfoBlk = WinGetErrorInfo(hab)) != (PERRINFO)NULL)
  333.    {
  334.       pszOffSet = ((PSZ)pErrInfoBlk) + pErrInfoBlk->offaoffszMsg;
  335.       pszErrMsg = ((PSZ)pErrInfoBlk) + *((PSHORT)pszOffSet);
  336.       if((INT)hwndFrame && (INT)hwndClient)
  337.          WinMessageBox(HWND_DESKTOP,         /* Parent window is desk top */
  338.                        hwndFrame,            /* Owner window is our frame */
  339.                        (PSZ)pszErrMsg,       /* PMWIN Error message       */
  340.                        "Error Msg",          /* Title bar message         */
  341.                        MSGBOXID,             /* Message identifier        */
  342.                        MB_MOVEABLE | MB_CUACRITICAL | MB_CANCEL ); /* Flags */
  343.       WinFreeErrorInfo(pErrInfoBlk);
  344.    }
  345.    WinPostMsg(hwndClient, WM_QUIT, (MPARAM)NULL, (MPARAM)NULL);
  346. } /* End of AbortHello */
  347.  
  348. /*********************** End of the hello.c *******************************/
  349.