File:  [OS/2 SDKs] / pmsdk / samples / petzold / chap06 / scribble.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:28:13 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: pmsdk-1988, HEAD
Microsoft OS/2 SDK PM 08-08-1988

/*----------------------------------------
   SCRIBBLE.C -- Mouse Scribbling Program 
  ----------------------------------------*/

#define INCL_WIN
#define INCL_GPI

#include <os2.h>
#include <stddef.h>

MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;

HAB  hab ;

int main (void)
     {
     static CHAR szClientClass [] = "Scribble" ;
     HMQ         hmq ;
     HWND        hwndFrame, hwndClient ;
     QMSG        qmsg ;
     ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
     ULONG       flFrameStyle = WS_VISIBLE ;

     hab = WinInitialize (0) ;
     hmq = WinCreateMsgQueue (hab, 0) ;

     WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;

     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                                     &flFrameFlags, szClientClass,
                                     szClientClass,
				     0L, NULL, 0, &hwndClient) ;

     if (hwndClient == NULL)
          WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
                         "Not enough memory to create the "
                         "bitmap used for storing images.",
                         szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
     else
          {
          while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
               WinDispatchMsg (hab, &qmsg) ;

          WinDestroyWindow (hwndFrame) ;
          }

     WinDestroyMsgQueue (hmq) ;
     WinTerminate (hab) ;
     
     return 0 ;
     }

MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
     {
     static BOOL      fButton1Down, fButton2Down ;
     static HBITMAP   hbm ;
     static HDC       hdcMemory ;
     static HPS       hpsMemory ;
     static POINTL    ptlPointerPos, aptl [3] ;
     BITMAPINFOHEADER bmp ;
     HPS              hpsWindow ;
     LONG             cxFullScrn, cyFullScrn ;
     SIZEL            sizl ;

     switch (msg)
          {
          case WM_CREATE:
               cxFullScrn = WinQuerySysValue (HWND_DESKTOP, SV_CXFULLSCREEN) ;
               cyFullScrn = WinQuerySysValue (HWND_DESKTOP, SV_CYFULLSCREEN) ;

                         /*-------------------------
                            Create Memory DC and PS
                           -------------------------*/

               hdcMemory = DevOpenDC (hab, OD_MEMORY, "*", 0L, NULL, NULL) ;

               sizl.cx = 0 ;
               sizl.cy = 0 ;
               hpsMemory = GpiCreatePS (hab, hdcMemory, &sizl,
					PU_PELS    | GPIF_DEFAULT |
                                        GPIT_MICRO | GPIA_ASSOC) ;

                         /*----------------------------------------------
                            Create monochrome bitmap, return 1 if cannot
                           ----------------------------------------------*/

               bmp.cx = (SHORT) cxFullScrn ;
               bmp.cy = (SHORT) cyFullScrn ;
               bmp.cPlanes = 1 ;
               bmp.cBitCount = 1 ;
               hbm = GpiCreateBitmap (hpsMemory, &bmp, 0L, 0L, NULL) ;

               if (hbm == NULL)
                    {
                    GpiDestroyPS (hpsMemory) ;
                    DevCloseDC (hdcMemory) ;
                    return 1 ;
                    }

                         /*--------------------------------------
                            Set bitmap in memory PS and clear it
                           --------------------------------------*/

               GpiSetBitmap (hpsMemory, hbm) ;

               aptl[1].x = cxFullScrn ;
               aptl[1].y = cyFullScrn ;
               GpiBitBlt (hpsMemory, NULL, 2L, aptl, ROP_ZERO, BBO_OR) ;
               return 0 ;

          case WM_BUTTON1DOWN:
               if (!fButton2Down)
                    WinSetCapture (HWND_DESKTOP, hwnd) ;

               ptlPointerPos.x = MOUSEMSG(&msg)->x ;
               ptlPointerPos.y = MOUSEMSG(&msg)->y ;

               fButton1Down = TRUE ;
               return 1 ;

          case WM_BUTTON1UP:
               if (!fButton2Down)
                    WinSetCapture (HWND_DESKTOP, NULL) ;

               fButton1Down = FALSE ;
               return 1 ;

          case WM_BUTTON2DOWN:
               if (!fButton1Down)
                    WinSetCapture (HWND_DESKTOP, hwnd) ;

               ptlPointerPos.x = MOUSEMSG(&msg)->x ;
               ptlPointerPos.y = MOUSEMSG(&msg)->y ;

               fButton2Down = TRUE ;
               return 1 ;

          case WM_BUTTON2UP:
               if (!fButton1Down)
                    WinSetCapture (HWND_DESKTOP, NULL) ;

               fButton2Down = FALSE ;
               return 1 ;

          case WM_MOUSEMOVE:
               if (!fButton1Down && !fButton2Down)
                    break ;

               hpsWindow = WinGetPS (hwnd) ;

               GpiSetColor (hpsMemory, fButton1Down ? CLR_TRUE : CLR_FALSE) ;
               GpiSetColor (hpsWindow,
                            fButton1Down ? CLR_NEUTRAL : CLR_BACKGROUND) ;

               GpiMove (hpsMemory, &ptlPointerPos) ;
               GpiMove (hpsWindow, &ptlPointerPos) ;

               ptlPointerPos.x = MOUSEMSG(&msg)->x ;
               ptlPointerPos.y = MOUSEMSG(&msg)->y ;

               GpiLine (hpsMemory, &ptlPointerPos) ;
               GpiLine (hpsWindow, &ptlPointerPos) ;

               WinReleasePS (hpsWindow) ;
               return 1 ;

          case WM_PAINT:
               hpsWindow = WinBeginPaint (hwnd, NULL, (PRECTL) aptl) ;

               aptl[2] = aptl[0] ;

               GpiBitBlt (hpsWindow, hpsMemory, 3L, aptl, ROP_SRCCOPY,
                          BBO_OR) ;

               WinEndPaint (hpsWindow) ;
               return 0 ;

          case WM_DESTROY:
               GpiDestroyPS (hpsMemory) ;
               DevCloseDC (hdcMemory) ;
               GpiDeleteBitmap (hbm) ;
               return 0 ;
          }
     return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
     }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.