home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l360 / 3.ddi / WINDOWS.@EM / WINHELLO.CBL < prev    next >
Encoding:
Text File  |  1991-06-10  |  21.4 KB  |  408 lines

  1.       $    set ans85 noosvs mf defaultbyte"00"
  2.        IDENTIFICATION DIVISION.
  3.        PROGRAM-ID. WinHello.
  4.  
  5.       ****************************************************************
  6.       *                                                              *
  7.       *                                                              *
  8.       *                (C) Micro Focus Ltd. 1991                     *
  9.       *                                                              *
  10.       *                      WINHELLO.CBL                            *
  11.       *                                                              *
  12.       * Example program: Windows 3.0 'Hello World'                   *
  13.       *                                                              *
  14.       ****************************************************************
  15.       * The following DEF file should be used when this program is   *
  16.       * linked:                                                      *
  17.       *                                                              *
  18.       *    NAME            WinCbl                                    *
  19.       *    DESCRIPTION     'Welcome to COBOL and Windows.'           *
  20.       *    EXETYPE         WINDOWS 3.0                               *
  21.       *    STUB            'WINSTUB.EXE'                             *
  22.       *    CODE            PRELOAD FIXED                             *
  23.       *    DATA            PRELOAD FIXED MULTIPLE                    *
  24.       *    STACKSIZE       16384                                     *
  25.       *    HEAPSIZE        1024                                      *
  26.       *    EXPORTS         MYWNDPROC                                 *
  27.       *                                                              *
  28.       ****************************************************************
  29.       *                                                              *
  30.       * Windows Programming                                          *
  31.       *                                                              *
  32.       * Please refer to printed and on-line documentation for more   *
  33.       * information on Windows programming with COBOL.               *
  34.       *                                                              *
  35.       ****************************************************************
  36.       *                                                              *
  37.       * About WINHELLO                                               *
  38.       *                                                              *
  39.       * A number of extensions to the COBOL language are used in     *
  40.       * this program, and are noted in comments where they occur.    *
  41.       * See the documentation and release notes for full description *
  42.       * etc.                                                         *
  43.       *                                                              *
  44.       * To write your own Windows programs in COBOL, we recommend    *
  45.       * that you use this program as a base.                         *
  46.       *                                                              *
  47.       ****************************************************************
  48.  
  49.       ****************************************************************
  50.       *                                                              *
  51.       * COBOL Extension: Special-names.                              *
  52.       *                                                              *
  53.       *     call-conventions are supported as below.                 *
  54.       *                                                              *
  55.       *     The meaning of the numbers is derived from decomposing   *
  56.       *     the number into binary components, with bits having      *
  57.       *     the following meanings:                                  *
  58.       *                                                              *
  59.       *     0   -   no bits specified means that the standard        *
  60.       *             COBOL Calling conventions are                    *
  61.       *             employed.  This means parameters are passed      *
  62.       *             on a stack, last named is first pushed on the    *
  63.       *             stack.  The parameters are removed from the      *
  64.       *             stack by the CALLer.                             *
  65.       *             Use this for compatibility with existing COBOL   *
  66.       *             programs.                                        *
  67.       *     1   -   parameters are passed on a stack, first named    *
  68.       *             is first pushed.  So you could call this         *
  69.       *             convention 'REVERSED'                            *
  70.       *     2   -   The parameters are removed from the stack        *
  71.       *             by the called routine                            *
  72.       *                                                              *
  73.       *                                                              *
  74.       *     So, we get the 'WINAPI' convention used by Windows as    *
  75.       *     convention 3. This convention is alternatively known as  *
  76.       *     the PASCAL calling convention.                           *
  77.       *                                                              *
  78.       ****************************************************************
  79.     special-names.
  80.                 call-convention 3 is WINAPI.
  81.     data division.
  82.         working-storage section.
  83.  
  84.       ****************************************************************
  85.       *                                                              *
  86.       * Microsoft Windows SDK supplies an include file WINDOWS.H     *
  87.       * containing data types and constants for Windows programming. *
  88.       * In COBOL we have to scan the C header files and create our   *
  89.       * own constants with the appropriate values.                   *
  90.       * This can be done automatically if required using the program *
  91.       * H2CPY.EXE provided with this COBOL system.                   *
  92.       *                                                              *
  93.       * In this program, we are using the WM-PAINT and WM-DESTROY    *
  94.       * messages.                                                    *
  95.       * To translate values from C constants to COBOL constants,     *
  96.       * use the following rules:                                     *
  97.       *                                                              *
  98.       *                     C           COBOL                        *
  99.       *     Hexadecimal   0xnn          h"nn"                        *
  100.       *     Decimal         nn            nn                         *
  101.       *                                                              *
  102.       ****************************************************************
  103.  
  104.         78  WM-PAINT            value h"000F".
  105.         78  WM-DESTROY          value h"0002".
  106.  
  107.       ****************************************************************
  108.       *                                                              *
  109.       * The supplied C header file defines data types for all the    *
  110.       * Windows data items.  In COBOL, we have to use the COBOL data *
  111.       * types.                                                       *
  112.       *                                                              *
  113.       * As a general conversion rule:                                *
  114.       *                                                              *
  115.       *     'C'         COBOL                                        *
  116.       *     SHORT       PIC S9(4) COMP-5                             *
  117.       *     USHORT      PIC 9(4)  COMP-5                             *
  118.       *     LONG        PIC S9(9) COMP-5                             *
  119.       *     ULONG       PIC 9(9)  COMP-5                             *
  120.       *     PVOID       POINTER             (similarly for other     *
  121.       *                                     pointer types)           *
  122.       *     LHANDLE     PIC 9(9)  COMP-5 )  (These are equivalent    *
  123.       *     LHANDLE     PPOINTER         )  for Windows working      *
  124.       *                         LHANDLE is used for any 32bit        *
  125.       *                         handle, eg HAB, HMQ, HPS etc.        *
  126.       *                                                              *
  127.       *     NB  PIC 9(4) COMP-5 is identical to PIC X(2) COMP-5      *
  128.       *     NB  PIC 9(9) COMP-5 is identical to PIC X(4) COMP-5      *
  129.       *                                                              *
  130.       ****************************************************************
  131.  
  132.       ****************************************************************
  133.       *                                                              *
  134.       * COBOL Extension: Procedure-pointers                          *
  135.       *                                                              *
  136.       *     Data pointers are now complemented by procedure pointers *
  137.       *                                                              *
  138.       ****************************************************************
  139.  
  140.         01  MyWndProc        procedure-pointer.
  141.  
  142.       ****************************************************************
  143.       *                                                              *
  144.       * An ASCIIZ string is a zero-terminated string. That is, the   *
  145.       * last character in the string must have the ASCII value of 0  *
  146.       * (ASCII null). Many of the calls to the Windows API require   *
  147.       * ASCIIZ strings.                                              *
  148.       *                                                              *
  149.       * However they are not natural with COBOL, and in particular   *
  150.       * are not suitable for use as literals.                        *
  151.       *                                                              *
  152.       * Where ASCIIZ strings are used, they must be declared in      *
  153.       * Working-Storage and followed by a x"00" NULL terminator.     *
  154.       *                                                              *
  155.       ****************************************************************
  156.  
  157.     01  MyClassName        pic x(20) value "Welcome1" & x"00".
  158.  
  159.     01  MyData.
  160.         03    loop-flag        pic x value 'C'.
  161.         88  loop-end        value 'E'.
  162.         03    bool            pic 9(4) comp-5.
  163.         88  boolTRUE        value 1.
  164.                 88  boolFALSE           value 0.
  165.  
  166.     01  WndClass.
  167.         03  style            pic 9(4) comp-5.
  168.         03  lpfnWndProc        procedure-pointer.
  169.         03  cbClsExtra        pic s9(4) comp-5.
  170.         03  cbWndExtra        pic s9(4) comp-5.
  171.         03  hInstance        pic 9(4) comp-5.
  172.         03  hIcon            pic 9(4) comp-5.
  173.         03  hCursor            pic 9(4) comp-5.
  174.         03  hbrBackground        pic 9(4) comp-5.
  175.         03  lpszMenuName        pointer.
  176.         03  lpszClassName        pointer.
  177.  
  178.       ****************************************************************
  179.       *                                                              *
  180.       * Structures are supplied in C header files, and must be       *
  181.       * converted to COBOL format to be used.                        *
  182.       * Below is a MSG structure, and in LOCAL-STORAGE section       *
  183.       * is an example of an LPPAINTSTRUCT structure (ppaint)         *
  184.       *                                                              *
  185.       ****************************************************************
  186.     01  msg.
  187.         03  msg-hwnd        pic 9(4) comp-5.
  188.         03  msg-message        pic 9(4) comp-5.
  189.         03  msg-wParam        pic 9(4) comp-5.
  190.         03  msg-lParam        pic s9(9) comp-5.
  191.         03  msg-time        pic 9(9) comp-5.
  192.         03  msg-pt.
  193.         05  msg-pt-x        pic 9(4) comp-5.
  194.         05  msg-pt-y        pic 9(4) comp-5.
  195.  
  196.       ****************************************************************
  197.       *                                                              *
  198.       * COBOL Extension: Local-Storage Section.                      *
  199.       * COBOL Extension: Recursion                                   *
  200.       *                                                              *
  201.       *     Any data declared in the LOCAL-STORAGE SECTION is        *
  202.       *     created freshly for each instance of the program.        *
  203.       *     This data cannot currently be initialised.               *
  204.       *                                                              *
  205.       ****************************************************************
  206.         local-storage SECTION.
  207.     01  MyData.
  208.             03  mResult                 pic 9(9) comp-5.
  209.         03  tmpFlag            pic 9(4) comp-5.
  210.         03  hWindow         pic 9(4) comp-5.
  211.  
  212.         01  hps                         pic x(4) comp-5.
  213.         01  ppaint.
  214.             03  hdc                     pic x(4) comp-5.
  215.             03 fErase                   pic s9(4) comp-5.
  216.             03  rcl.
  217.                 05  xLeft               pic s9(4) comp-5.
  218.                 05  yTop                pic s9(4) comp-5.
  219.                 05  xRight              pic s9(4) comp-5.
  220.                 05  yBottom             pic s9(4) comp-5.
  221.             03 fRestore                 pic s9(4) comp-5.
  222.             03 fUpdate                  pic s9(4) comp-5.
  223.             03 rgbdata                  pic x occurs 16.
  224.  
  225.         linkage section.
  226.         01  hWnd               pic x(2) comp-5.
  227.         01  iMessage                pic 9(4) comp-5.
  228.         01  wParam                  pic 9(4) comp-5.
  229.         01  lParam                  pic s9(9) comp-5.
  230.         01  hInst                   pic xx   comp-5.
  231.         01  hPrevInstance           pic xx   comp-5.
  232.         01  lpszCmdLine             pic x(120).
  233.         01  nCmdShow                pic xx   comp-5.
  234.  
  235.       ****************************************************************
  236.       *                                                              *
  237.       * COBOL Extension: Call-conventions                            *
  238.       *                                                              *
  239.       *     This use of the call-convention WINAPI (declared above   *
  240.       *     in special-names) means that all the entry points in     *
  241.       *     this program follow the OS2API calling convention.       *
  242.       *                                                              *
  243.       ****************************************************************
  244.         procedure division WINAPI using  by value hInst
  245.                                   by value hPrevInstance
  246.                                   by reference lpszCmdLine
  247.                                   by value nCmdShow.
  248.         MyWinMain section.
  249.         if hPrevInstance = 0
  250.         move 3 to style
  251.         set lpfnWndProc to entry "MyWndProc"
  252.         move 0 to cbClsExtra
  253.         move 0 to cbWndExtra
  254.         move hInst to hInstance
  255.                 call WINAPI "__LoadIcon" using by value 0 size 2
  256.                                 by value h"00007f00" size 4
  257.             returning hIcon
  258.                 call WINAPI "__LoadCursor" using by value 0 size 2
  259.                 by value h"00007f00" size 4
  260.             returning hCursor
  261.                 call WINAPI "__GetStockObject" using by value 0 size 2
  262.             returning hbrBackground
  263.         set lpszMenuName to NULL
  264.         set lpszClassName to address of MyClassName
  265.                 call WINAPI '__RegisterClass' using WndClass
  266.             returning tmpFlag
  267.         if tmpFlag = 0
  268.             exit program returning 0
  269.         end-if
  270.         end-if
  271.             call WINAPI "__CreateWindow" using by reference MyClassName
  272.             by reference "COBOL & Windows" & x"00"
  273.             by value h"00CF0000" size 4
  274.             by value h"8000" size 2
  275.                         by value 0 size 2
  276.             by value h"8000" size 2
  277.                         by value 0 size 2
  278.             by value 0 size 2
  279.             by value 0 size 2
  280.             by value hInst
  281.             by value 0 size 4
  282.             returning hWindow
  283.             call WINAPI "__ShowWindow" using by value hWindow
  284.                         by value nCmdShow
  285.             call WINAPI "__UpdateWindow" using by value hWindow
  286.  
  287.       ****************************************************************
  288.       *                                                              *
  289.       * This in-line PERFORM implements the message loop.            *
  290.       *                                                              *
  291.       ****************************************************************
  292.         perform until loop-end
  293.                 call WINAPI '__GetMessage' using
  294.             by reference msg
  295.             by value 0 size 2
  296.             by value 0 size 2
  297.             by value 0 size 2
  298.               returning bool
  299.         if boolFALSE
  300.             set loop-end to true
  301.         else
  302.                         call WINAPI '__TranslateMessage'
  303.                     using by reference msg
  304.                         call WINAPI '__DispatchMessage'
  305.                     using by reference msg
  306.         end-if
  307.         end-perform
  308.  
  309.             exit program returning msg-wParam
  310.             stop run.
  311.  
  312.       ****************************************************************
  313.       *                                                              *
  314.       * The first ever Windows COBOL window procedure!               *
  315.       *                                                              *
  316.       ****************************************************************
  317.  
  318.       MyWindowProcedure SECTION.
  319.       ****************************************************************
  320.       *                                                              *
  321.       * COBOL Extension: ENTRY USING BY VALUE                        *
  322.       * COBOL Extension: Recursion                                   *
  323.       *                                                              *
  324.       *     To complement the CALL USING BY VALUE, we now allow      *
  325.       *     ENTRY USING BY VALUE.                                    *
  326.       *                                                              *
  327.       *     COBOL being recursive means that the call to             *
  328.       *     CreateWindow (above) can lead to control being           *
  329.       *     passed to this entry point.                              *
  330.       *     In fact, any of the calls in this section could lead     *
  331.       *     to control being passed to a new instance of this        *
  332.       *     entry point (hence the need for LOCAL-STORAGE SECTION.)  *
  333.       *                                                              *
  334.       ****************************************************************
  335.         entry "MyWndProc" using by value hWnd
  336.                                 by value iMessage
  337.                                 by value wParam
  338.                                 by value lParam.
  339.             move 0 to mResult
  340.         evaluate iMessage
  341.  
  342.       ****************************************************************
  343.       *                                                              *
  344.       * The only message we are interested in is the PAINT message   *
  345.       * The sequence of actions is:                                  *
  346.       *                                                              *
  347.       *     Get Handle-To-Presentation-Space (HPS) for painting      *
  348.       *                         in the client window                 *
  349.       *     Fill the window with the System Background colour        *
  350.       *     Write the words 'Hello COBOL World' at position (70,70)  *
  351.       *     Release the HPS.                                         *
  352.       *                                                              *
  353.       ****************************************************************
  354.  
  355.               when WM-PAINT
  356.                 call WINAPI '__BeginPaint'
  357.                             using by value hwnd
  358.                                   by reference ppaint
  359.                             returning hps
  360.  
  361.                 call WINAPI '__FillRect'
  362.                             using by value hps
  363.                                   by reference rcl
  364.                                   by value hbrBackground
  365.  
  366.                 call WINAPI '__GetClientRect'
  367.                             using by value hwnd
  368.                                   by reference rcl
  369.                 call WINAPI '__DrawText'
  370.                             using by value hps
  371.                                   by reference 'Hello COBOL World'
  372.                                   by value 17 size 2
  373.                                   by reference rcl
  374.                                   by value h"25" size 2
  375.  
  376.                 call WINAPI '__EndPaint'
  377.                             using by value hwnd
  378.                                   by reference ppaint
  379.  
  380.           when WM-DESTROY
  381.                 call WINAPI '__PostQuitMessage' using by value 0 size 2
  382.  
  383.       ****************************************************************
  384.       *                                                              *
  385.       *     All other messages are despatched to the default         *
  386.       *     window procedure according to the Windows rules          *
  387.       *                                                              *
  388.       ****************************************************************
  389.  
  390.           when other
  391.                 call WINAPI "__DefWindowProc"
  392.               using by value hWnd
  393.                       by value iMessage
  394.                   by value wParam
  395.                    by value lParam
  396.                         returning mResult
  397.         end-evaluate
  398.  
  399.       ****************************************************************
  400.       *                                                              *
  401.       * COBOL Extension: RETURNING phrase                            *
  402.       *                                                              *
  403.       *     To complement the RETURNING phrase on the CALL, you      *
  404.       *     can also use the RETURNING phrase on the EXIT.           *
  405.       *                                                              *
  406.       ****************************************************************
  407.             exit program returning mResult.
  408.