Annotation of pmsdk/samples/pmcap/pmcap.c, revision 1.1.1.1

1.1       root        1: /*--------------------------------------------
                      2:    PMCAP.C -- Captures PM Screen to .BMP File
                      3:   --------------------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: #include <os2.h>
                      8: #include <stdlib.h>
                      9: #include <stdio.h>
                     10: #include "pmcap.h"
                     11: 
                     12: #define ID_TIMER    1
                     13: 
                     14: typedef struct
                     15:      {
                     16:      BOOL  fMonochrome ;
                     17:      BOOL  fHideWindow ;
                     18:      BOOL  fAutoDialog ;
                     19:      BOOL  fIncludePtr ;
                     20:      SHORT sDelay ;
                     21:      SHORT sDisplay ;
                     22:      }
                     23:      SETTINGS ;
                     24: 
                     25: MRESULT CALLBACK ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     26: MRESULT CALLBACK AboutDlgProc  (HWND, USHORT, MPARAM, MPARAM) ;
                     27: MRESULT CALLBACK SaveDlgProc   (HWND, USHORT, MPARAM, MPARAM) ;
                     28: 
                     29: CHAR szClientClass [] = "PMCAP" ;
                     30: HAB  hab ;
                     31: 
                     32: int main (void)
                     33:      {
                     34:      static SWCNTRL swctl ;
                     35:      static ULONG   flFrameFlags = FCF_TITLEBAR   | FCF_SYSMENU |
                     36:                                    FCF_SIZEBORDER | FCF_MINMAX  |
                     37:                                    FCF_MENU       | FCF_ACCELTABLE |
                     38:                                    FCF_SHELLPOSITION;
                     39:      HMQ            hmq ;
                     40:      HSWITCH        hsw ;
                     41:      HWND           hwndFrame, hwndClient ;
                     42:      QMSG           qmsg ;
                     43: 
                     44:      hab = WinInitialize (0) ;
                     45:      hmq = WinCreateMsgQueue (hab, 0) ;
                     46: 
                     47:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     48: 
                     49:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
                     50:                                      &flFrameFlags, szClientClass,
                     51:                                      "PMCAP - Captures PM Screen",
                     52:                                      0L, NULL, ID_RESOURCE, &hwndClient) ;
                     53: 
                     54:      WinSendMsg (hwndFrame, WM_SETICON,
                     55:                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
                     56:                  NULL) ;
                     57: 
                     58:      swctl.hwnd = hwndFrame ;
                     59:      hsw = WinAddSwitchEntry (&swctl) ;
                     60: 
                     61:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     62:           WinDispatchMsg (hab, &qmsg) ;
                     63: 
                     64:      WinRemoveSwitchEntry (hsw) ;
                     65:      WinDestroyWindow (hwndFrame) ;
                     66:      WinDestroyMsgQueue (hmq) ;
                     67:      WinTerminate (hab) ;
                     68:      return 0 ;
                     69:      }
                     70: 
                     71: MRESULT CALLBACK ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                     72:      {
                     73:      static CHAR     szFilename [81],
                     74:                      szFilePaint [81],
                     75:                      szAppName [] = "PMCAP",
                     76:                      szKeySet  [] = "SETTINGS",
                     77:                      szKeyFile [] = "FILENAME" ,
                     78:                      szKeyPaint[] = "FILEPAINT" ;
                     79:      static HBITMAP  hbm ;
                     80:      static HWND     hwndFrame, hwndMenu ;
                     81:      static SETTINGS set = { FALSE, FALSE, TRUE, FALSE,
                     82:                              IDM_DELAY15, IDM_ACTUAL } ;
                     83:      static SHORT    cxScreen, cyScreen, sCountDown ;
                     84:      HBITMAP         hbmClip ;
                     85:      HPS             hps ;
                     86:      RECTL           rcl ;
                     87:      SHORT           sSaveResult, sLen ;
                     88:      USHORT          usfInfo ;
                     89: 
                     90:      switch (msg)
                     91:           {
                     92:           case WM_CREATE:
                     93:                cxScreen = (SHORT) WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
                     94:                cyScreen = (SHORT) WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
                     95: 
                     96:                WinQueryProfileData   (hab, szAppName, szKeySet, &set, &sLen);
                     97:                WinQueryProfileString (hab, szAppName, szKeyFile,
                     98:                                       "BITMAP00.BMP", szFilename,
                     99:                                       sizeof szFilename - 1) ;
                    100:                WinQueryProfileString (hab, szAppName, szKeyPaint,
                    101:                                       "PAINT00.MSP", szFilePaint,
                    102:                                       sizeof szFilePaint - 1) ;
                    103: 
                    104:                SaveColorSettings (TRUE, set.fMonochrome) ;
                    105: 
                    106:                hwndFrame = WinQueryWindow (hwnd, QW_PARENT, FALSE) ;
                    107:                hwndMenu  = WinWindowFromID (hwndFrame, FID_MENU) ;
                    108: 
                    109:                CheckMenuItem (hwndMenu, IDM_MONO,     set.fMonochrome) ;
                    110:                CheckMenuItem (hwndMenu, IDM_HIDE,     set.fHideWindow) ;
                    111:                CheckMenuItem (hwndMenu, IDM_AUTODLG,  set.fAutoDialog) ;
                    112:                CheckMenuItem (hwndMenu, IDM_POINTER,  set.fIncludePtr) ;
                    113:                CheckMenuItem (hwndMenu, set.sDelay,   TRUE) ;
                    114:                CheckMenuItem (hwndMenu, set.sDisplay, TRUE) ;
                    115: 
                    116:                AddItemToSysMenu (hwndFrame) ;
                    117:                return 0 ;
                    118: 
                    119:           case WM_INITMENU:
                    120:                switch (SHORT1FROMMP (mp1))
                    121:                     {
                    122:                     case IDM_FILE:
                    123:                          EnableMenuItem (hwndMenu, IDM_SAVE, hbm != NULL) ;
                    124:                          EnableMenuItem (hwndMenu, IDM_SAVEPAINT,
                    125:                                          IsBitmapMonoEGA (hbm)) ;
                    126:                          return 0 ;
                    127: 
                    128:                     case IDM_EDIT:
                    129:                          EnableMenuItem (hwndMenu, IDM_COPY, hbm != NULL) ;
                    130:                          EnableMenuItem (hwndMenu, IDM_PASTE,
                    131:                               WinQueryClipbrdFmtInfo (hab, CF_BITMAP, &usfInfo)
                    132:                                    && usfInfo == CFI_HANDLE) ;
                    133:                          return 0 ;
                    134:                     }
                    135:                break ;
                    136: 
                    137:           case WM_COMMAND:
                    138:                switch (COMMANDMSG(&msg)->cmd)
                    139:                     {
                    140:                     case IDM_ABOUT:
                    141:                          WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc,
                    142:                                     NULL, IDD_ABOUT, NULL) ;
                    143:                          return 0 ;
                    144: 
                    145:                     case IDM_SAVE:
                    146:                          if (!WinDlgBox (HWND_DESKTOP, hwnd, SaveDlgProc,
                    147:                                          NULL, IDD_SAVE, szFilename))
                    148:                               return 0 ;
                    149: 
                    150:                          if (hbm != NULL)
                    151:                               {
                    152:                               WinSetPointer (HWND_DESKTOP,
                    153:                                    WinQuerySysPointer (HWND_DESKTOP,
                    154:                                                        SPTR_WAIT, FALSE)) ;
                    155: 
                    156:                               sSaveResult = SaveBitmap (hbm, szFilename) ;
                    157: 
                    158:                               WinSetPointer (HWND_DESKTOP,
                    159:                                    WinQuerySysPointer (HWND_DESKTOP,
                    160:                                                        SPTR_ARROW, FALSE)) ;
                    161: 
                    162:                               if (sSaveResult)
                    163:                                    ErrorMessage (hwnd, sSaveResult) ;
                    164:                               else
                    165:                                    IncrementFilename (szFilename) ;
                    166:                               }
                    167:                          return 0 ;
                    168: 
                    169:                     case IDM_SAVEPAINT:
                    170:                          if (!WinDlgBox (HWND_DESKTOP, hwnd, SaveDlgProc,
                    171:                                          NULL, IDD_SAVEPAINT, szFilePaint))
                    172:                               return 0 ;
                    173: 
                    174:                          if (IsBitmapMonoEGA (hbm))
                    175:                               {
                    176:                               WinSetPointer (HWND_DESKTOP,
                    177:                                    WinQuerySysPointer (HWND_DESKTOP,
                    178:                                                        SPTR_WAIT, FALSE)) ;
                    179: 
                    180:                               sSaveResult = SavePaintFormat (hbm, szFilePaint);
                    181: 
                    182:                               WinSetPointer (HWND_DESKTOP,
                    183:                                    WinQuerySysPointer (HWND_DESKTOP,
                    184:                                                        SPTR_ARROW, FALSE)) ;
                    185: 
                    186:                               if (sSaveResult)
                    187:                                    ErrorMessage (hwnd, sSaveResult) ;
                    188:                               else
                    189:                                    IncrementFilename (szFilePaint) ;
                    190:                               }
                    191:                          return 0 ;
                    192: 
                    193:                     case IDM_COPY:
                    194:                                              // Make copy of stored bitmap
                    195: 
                    196:                          hbmClip = CopyBitmap (hbm) ;
                    197: 
                    198:                                              // Set clipboard data to copy
                    199: 
                    200:                          if (hbmClip != NULL)
                    201:                               {
                    202:                               WinOpenClipbrd (hab) ;
                    203:                               WinEmptyClipbrd (hab) ;
                    204:                               WinSetClipbrdData (hab, (ULONG) hbmClip,
                    205:                                                  CF_BITMAP, CFI_HANDLE) ;
                    206:                               WinCloseClipbrd (hab) ;
                    207:                               }
                    208:                          else
                    209:                               ErrorMessage (hwnd, IDS_BMPCREATE) ;
                    210:                          return 0 ;
                    211: 
                    212:                     case IDM_PASTE:
                    213:                                               // Get bitmap from clipboard
                    214: 
                    215:                          WinOpenClipbrd (hab) ;
                    216:                          hbmClip = WinQueryClipbrdData (hab, CF_BITMAP) ;
                    217: 
                    218:                          if (hbmClip != NULL)
                    219:                               {
                    220:                               if (hbm != NULL)
                    221:                                    GpiDeleteBitmap (hbm) ;
                    222: 
                    223:                                              // Make copy of it
                    224: 
                    225:                               hbm = CopyBitmap (hbmClip) ;
                    226: 
                    227:                               if (hbm == NULL)
                    228:                                    ErrorMessage (hwnd, IDS_BMPCREATE) ;
                    229:                               }
                    230:                          WinCloseClipbrd (hab) ;
                    231:                          WinInvalidateRect (hwnd, NULL, FALSE) ;
                    232:                          return 0 ;
                    233: 
                    234:                     case IDM_BEGIN:
                    235:                          if (WinStartTimer (hab, hwnd, ID_TIMER, 1000))
                    236:                               {
                    237:                               EnableMenuItem (hwndMenu, IDM_BEGIN, FALSE) ;
                    238:                               EnableMenuItem (hwndMenu, IDM_ABORT, TRUE) ;
                    239:                               sCountDown = set.sDelay - IDM_DELAY ;
                    240: 
                    241:                               if (set.fHideWindow)
                    242:                                    WinShowWindow (hwndFrame, FALSE) ;
                    243:                               }
                    244:                          else
                    245:                               ErrorMessage (hwnd, IDS_TIMER) ;
                    246: 
                    247:                          return 0 ;
                    248: 
                    249:                     case IDM_ABORT:
                    250:                          EnableMenuItem (hwndMenu, IDM_BEGIN, TRUE) ;
                    251:                          EnableMenuItem (hwndMenu, IDM_ABORT, FALSE) ;
                    252: 
                    253:                          WinStopTimer (hab, hwnd, ID_TIMER) ;
                    254:                          return 0 ;
                    255: 
                    256:                     case IDM_MONO:
                    257:                          CheckMenuItem (hwndMenu, IDM_MONO,
                    258:                                         set.fMonochrome = !set.fMonochrome) ;
                    259:                          
                    260:                          SaveColorSettings (FALSE, set.fMonochrome) ;
                    261:                          return 0 ;
                    262: 
                    263:                     case IDM_HIDE:
                    264:                          CheckMenuItem (hwndMenu, IDM_HIDE,
                    265:                                         set.fHideWindow = !set.fHideWindow) ;
                    266:                          return 0 ;
                    267: 
                    268:                     case IDM_AUTODLG:
                    269:                          CheckMenuItem (hwndMenu, IDM_AUTODLG,
                    270:                                         set.fAutoDialog = !set.fAutoDialog) ;
                    271:                          return 0 ;
                    272: 
                    273:                     case IDM_POINTER:
                    274:                          CheckMenuItem (hwndMenu, IDM_POINTER,
                    275:                                         set.fIncludePtr = !set.fIncludePtr) ;
                    276:                          return 0 ;
                    277: 
                    278:                     case IDM_DELAY5:
                    279:                     case IDM_DELAY10:
                    280:                     case IDM_DELAY15:
                    281:                     case IDM_DELAY30:
                    282:                     case IDM_DELAY60:
                    283:                          CheckMenuItem (hwndMenu, set.sDelay, FALSE) ;
                    284:                          set.sDelay = COMMANDMSG(&msg)->cmd ;
                    285:                          CheckMenuItem (hwndMenu, set.sDelay, TRUE) ;
                    286:                          return 0 ;
                    287: 
                    288:                     case IDM_ACTUAL:
                    289:                     case IDM_STRETCH:
                    290:                          CheckMenuItem (hwndMenu, set.sDisplay, FALSE) ;
                    291:                          set.sDisplay = COMMANDMSG(&msg)->cmd ;
                    292:                          CheckMenuItem (hwndMenu, set.sDisplay, TRUE) ;
                    293: 
                    294:                          WinInvalidateRect (hwnd, NULL, FALSE) ;
                    295:                          return 0 ;
                    296:                     }
                    297: 
                    298:           case WM_TIMER:
                    299:                switch (SHORT1FROMMP (mp1))
                    300:                     {
                    301:                     case ID_TIMER:
                    302:                          if (--sCountDown)
                    303:                               {
                    304:                               DosBeep (1024, 100) ;
                    305:                               return 0 ;
                    306:                               }
                    307: 
                    308:                          DosBeep (1024, 1000) ;
                    309:                          WinStopTimer (hab, hwnd, ID_TIMER) ;
                    310:                          EnableMenuItem (hwndMenu, IDM_BEGIN, TRUE) ;
                    311:                          EnableMenuItem (hwndMenu, IDM_ABORT, FALSE) ;
                    312: 
                    313:                                              // Delete old bitmap
                    314:                          if (hbm != NULL)
                    315:                               GpiDeleteBitmap (hbm) ;
                    316: 
                    317:                                              // Copy screen to bitmap
                    318: 
                    319:                          hbm = ScreenToBitmap (cxScreen, cyScreen,
                    320:                                                set.fIncludePtr,
                    321:                                                set.fMonochrome) ;
                    322: 
                    323:                          if (set.fHideWindow)
                    324:                               WinShowWindow (hwndFrame, TRUE) ;
                    325: 
                    326:                          if (hbm == NULL)
                    327:                               ErrorMessage (hwnd, IDS_BMPCREATE) ;
                    328: 
                    329:                          else if (set.fAutoDialog)
                    330:                               WinPostMsg (hwnd, WM_COMMAND,
                    331:                                    MPFROMSHORT (
                    332:                                         set.fMonochrome ? IDM_SAVEPAINT
                    333:                                                         : IDM_SAVE),
                    334:                                         NULL) ;
                    335: 
                    336:                          WinInvalidateRect (hwnd, NULL, FALSE) ;
                    337:                          WinUpdateWindow (hwnd) ;
                    338:                          return 0 ;
                    339:                     }
                    340:                break ;
                    341: 
                    342:           case WM_PAINT:
                    343:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    344:                GpiErase (hps) ;
                    345: 
                    346:                if (hbm != NULL)
                    347:                     {
                    348:                     WinQueryWindowRect (hwnd, &rcl) ;
                    349: 
                    350:                     WinDrawBitmap (hps, hbm, NULL, (PPOINTL) &rcl,
                    351:                                    CLR_NEUTRAL, CLR_BACKGROUND,
                    352:                                    set.sDisplay == IDM_STRETCH ?
                    353:                                         DBM_STRETCH : DBM_NORMAL) ;
                    354:                     }
                    355:                WinEndPaint (hps) ;
                    356:                return 0 ;
                    357: 
                    358:           case WM_DESTROY:
                    359:                if (hbm != NULL)
                    360:                     GpiDeleteBitmap (hbm) ;
                    361: 
                    362:                WinWriteProfileString (hab, szAppName, szKeyFile, szFilename) ;
                    363:                WinWriteProfileString (hab, szAppName, szKeyPaint,szFilePaint);
                    364:                WinWriteProfileData   (hab, szAppName, szKeySet,
                    365:                                       &set, sizeof set) ;
                    366: 
                    367:                if (set.fMonochrome)
                    368:                     SaveColorSettings (FALSE, FALSE) ;
                    369: 
                    370:                return 0 ;
                    371:           }
                    372:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    373:      }
                    374: 
                    375: MRESULT CALLBACK AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    376:      {
                    377:      switch (msg)
                    378:           {
                    379:           case WM_COMMAND:
                    380:                switch (COMMANDMSG(&msg)->cmd)
                    381:                     {
                    382:                     case DID_OK:
                    383:                     case DID_CANCEL:
                    384:                          WinDismissDlg (hwnd, TRUE) ;
                    385:                          return 0 ;
                    386:                     }
                    387:                break ;
                    388:           }
                    389:      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
                    390:      }
                    391: 
                    392: MRESULT CALLBACK SaveDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    393:      {
                    394:      static PCHAR szFilename ;
                    395:      FILE         *file ;
                    396: 
                    397:      switch (msg)
                    398:           {
                    399:           case WM_INITDLG:
                    400:                szFilename = PVOIDFROMMP (mp2) ;
                    401: 
                    402:                WinSendDlgItemMsg (hwnd, IDD_FILENAME, EM_SETTEXTLIMIT,
                    403:                                   MPFROMSHORT (80), NULL) ;
                    404: 
                    405:                WinSetDlgItemText (hwnd, IDD_FILENAME, szFilename) ;
                    406:                break ;
                    407: 
                    408:           case WM_COMMAND:
                    409:                switch (COMMANDMSG(&msg)->cmd)
                    410:                     {
                    411:                     case DID_OK:
                    412:                          WinQueryDlgItemText (hwnd, IDD_FILENAME,
                    413:                                               80, szFilename) ;
                    414: 
                    415:                                    // Test if file exists
                    416: 
                    417:                          if (file = fopen (szFilename, "r"))
                    418:                               {
                    419:                               fclose (file) ;
                    420: 
                    421:                               if (MBID_NO == WinMessageBox (HWND_DESKTOP, hwnd,
                    422:                                                   "File exists.  Replace it?",
                    423:                                                   szClientClass, 0,
                    424:                                                   MB_YESNO | MB_ICONQUESTION |
                    425:                                                   MB_DEFBUTTON2 | MB_MOVEABLE))
                    426:                                    return 0 ;
                    427:                               }
                    428: 
                    429:                          WinDismissDlg (hwnd, TRUE) ;
                    430:                          return 0 ;
                    431: 
                    432:                     case DID_CANCEL:
                    433:                          WinDismissDlg (hwnd, FALSE) ;
                    434:                          return 0 ;
                    435:                     }
                    436:                break ;
                    437:           }
                    438:      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
                    439:      }

unix.superglobalmegacorp.com

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