home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / BACKDR.ZIP / BACKDROP.C < prev    next >
C/C++ Source or Header  |  1989-06-13  |  13KB  |  352 lines

  1. #define INCL_WIN
  2. #define INCL_GPI
  3. #define INCL_DOS
  4. #define INCL_DOSERRORS
  5. #include <os2.h>
  6.  
  7. #include <process.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "backdrop.h"
  12.  
  13. HAB     hAB;
  14. CHAR    szAppName[] = "Desktop Background";
  15. ULONG   ScreenWidth;
  16. ULONG   ScreenHeight;
  17. ULONG   BitmapWidth;
  18. ULONG   BitmapHeight;
  19. PFNWP   pfnwpOldFrameProc;
  20.  
  21. #define STACK_SIZE 10000
  22.  
  23. void cdecl main (void)
  24.     {
  25.     SEL     Sel;
  26.     PCH     StackBottom;
  27.     int     main2_ThreadID;
  28.     HSYSSEM hssm;
  29.     USHORT  usError;
  30.  
  31.     /*----------------------------------------------------------------------*/
  32.     /*  Set up a semaphore to make sure that only one copy of backdrop is   */
  33.     /*  ever loaded and running.                                            */
  34.     /*                                                                      */
  35.     /*  If the semaphore is already set up, the usError will be             */
  36.     /*  ERROR_ALREADYEXISTS                                                 */
  37.     /*----------------------------------------------------------------------*/
  38.     usError = DosCreateSem (CSEM_PUBLIC,
  39.                             &hssm,
  40.                             "\\sem\\backdrop.sem");
  41.     if (usError != 0)
  42.         {
  43.         DosExit (EXIT_PROCESS, 1);
  44.         }
  45.  
  46.     DosSemSet (hssm);
  47.  
  48.     /*----------------------------------------------------------------------*/
  49.     /*  Start up the main application as a secondary thread of the loading  */
  50.     /*  application.                                                        */
  51.     /*                                                                      */
  52.     /*  I use a second thread so that the stack is protected.  In this way  */
  53.     /*  any stack overflows will cause a GP fault, rather that overwriting  */
  54.     /*  the client data segment contents.                                   */
  55.     /*----------------------------------------------------------------------*/
  56.     DosAllocSeg (STACK_SIZE, &Sel, 0);
  57.     StackBottom = MAKEP (Sel, 0);
  58.     main2_ThreadID = _beginthread (main2, StackBottom, STACK_SIZE, 0L);
  59.  
  60.     while (TRUE)
  61.         {
  62.         DosSleep (-1L);
  63.         }
  64.  
  65.     DosSemClear (hssm);
  66.  
  67.     DosExit (EXIT_PROCESS, 1);
  68.     }
  69.  
  70. void cdecl far main2 (void far * ArgList)
  71.     {
  72.     HMQ     hMq;
  73.     HWND    hWndClient;
  74.     HWND    hWndFrame;
  75.     QMSG    qMsg;
  76.     ULONG   ulStyle = NULL;
  77.     SWCNTRL swctl;
  78.     HSWITCH hswitch;
  79.     PID     pid;
  80.  
  81.     /*----------------------------------------------------------------------*/
  82.     /*  Create the message queue and background window.                     */
  83.     /*----------------------------------------------------------------------*/
  84.  
  85.     hAB = WinInitialize (0);
  86.     hMq = WinCreateMsgQueue (hAB, 0);
  87.  
  88.     if (WinRegisterClass (hAB,
  89.                           szAppName,
  90.                           ClientWndProc,
  91.                           CS_SIZEREDRAW,
  92.                           0))
  93.         {
  94.         hWndFrame = WinCreateStdWindow (HWND_DESKTOP,
  95.                                         FS_NOBYTEALIGN,
  96.                                         &ulStyle,
  97.                                         szAppName,
  98.                                         "",
  99.                                         0L,
  100.                                         NULL,
  101.                                         NULL,
  102.                                         &hWndClient);
  103.         if (hWndFrame)
  104.             {
  105.             /*--------------------------------------------------------------*/
  106.             /*  Add an entry to the swicth list for the background app.     */
  107.             /*--------------------------------------------------------------*/
  108.             WinQueryWindowProcess (hWndFrame, &pid, NULL);
  109.  
  110.             swctl.hwnd          = hWndFrame;        /* window handle      */
  111.             swctl.hwndIcon      = NULL;             /* icon handle        */
  112.             swctl.hprog         = NULL;             /* program handle     */
  113.             swctl.idProcess     = pid;              /* process identifier */
  114.             swctl.idSession     = NULL;             /* session identifier */
  115.             swctl.uchVisibility = SWL_GRAYED;       /* visibility         */
  116.             swctl.fbJump        = SWL_NOTJUMPABLE;  /* jump indicator     */
  117.      
  118.             strcpy (swctl.szSwtitle, "Fancy Background");
  119.  
  120.             hswitch = WinAddSwitchEntry (&swctl);
  121.  
  122.             /*--------------------------------------------------------------*/
  123.             /*  Normal windows message loop                                 */
  124.             /*--------------------------------------------------------------*/
  125.             while (WinGetMsg (hAB, &qMsg, (HWND) NULL, 0, 0))
  126.                 {
  127.                 WinDispatchMsg (hAB, &qMsg);
  128.                 }
  129.  
  130.             WinShowWindow (hWndFrame, FALSE);
  131.             WinDestroyWindow (hWndFrame);
  132.             }
  133.         else
  134.             {
  135.             WinMessageBox (HWND_DESKTOP,
  136.                         NULL,
  137.                         "Could not create main window",
  138.                         szAppName,
  139.                         0,
  140.                         MB_OK | MB_ICONEXCLAMATION);
  141.             }
  142.         }
  143.     else
  144.         {
  145.         WinMessageBox (HWND_DESKTOP,
  146.                        NULL,
  147.                        "Could not register window class",
  148.                        szAppName,
  149.                        0,
  150.                        MB_OK | MB_ICONEXCLAMATION);
  151.         }
  152.  
  153.     WinDestroyMsgQueue (hMq);
  154.     WinTerminate (hAB);
  155.     DosExit (EXIT_PROCESS, 0);
  156.     }
  157.  
  158. MRESULT EXPENTRY ClientWndProc (HWND hWnd, USHORT Message,
  159.                                 MPARAM mp1, MPARAM mp2)
  160.     {
  161.     HPS     hPS;
  162.     RECTL   Rect;
  163.     RECTL   rcl;
  164.     BITMAPINFOHEADER BitmapInfo;
  165.     static HBITMAP   Bitmap;
  166.  
  167.     switch  (Message)
  168.         {
  169.         case WM_CREATE:
  170.             {
  171.             /*--------------------------------------------------------------*/
  172.             /*  Load the background bitmap, scaling to the background       */
  173.             /*  window size on loading.                                     */
  174.             /*--------------------------------------------------------------*/
  175.             ScreenWidth  = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
  176.             ScreenHeight = WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
  177.        
  178.             hPS = WinGetPS (HWND_DESKTOP);
  179.             Bitmap = GpiLoadBitmap (hPS,
  180.                                     NULL,
  181.                                     ID_BACKGROUND,
  182.                                     ScreenWidth,
  183.                                     ScreenHeight);
  184.             WinReleasePS (hPS);
  185.          
  186.             GpiQueryBitmapParameters (Bitmap, &BitmapInfo);
  187.  
  188.             BitmapWidth  = BitmapInfo.cx;
  189.             BitmapHeight = BitmapInfo.cy;
  190.  
  191.             /*--------------------------------------------------------------*/
  192.             /*  Push the window to the bottom of the window pile.           */
  193.             /*--------------------------------------------------------------*/
  194.             WinSetWindowPos (WinQueryWindow (hWnd,
  195.                                              QW_PARENT,
  196.                                              FALSE),
  197.                              HWND_BOTTOM,
  198.                              (SHORT) 0,
  199.                              (SHORT) 0,
  200.                              (SHORT) ScreenWidth, 
  201.                              (SHORT) ScreenHeight,
  202.                              SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ZORDER);
  203.  
  204.             /*--------------------------------------------------------------*/
  205.             /*  Sub class the frame window to prevent it coming to the top. */
  206.             /*--------------------------------------------------------------*/
  207.             pfnwpOldFrameProc = WinSubclassWindow (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  208.                                                    FrameSubclassProc);
  209.             break;
  210.             }
  211.  
  212.         case WM_DESTROY:
  213.             {
  214.             /*--------------------------------------------------------------*/
  215.             /*  Clear out the bitmap and un-subclass the frame window.      */
  216.             /*--------------------------------------------------------------*/
  217.             GpiDeleteBitmap (Bitmap);
  218.          
  219.             WinSubclassWindow (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  220.                                (* pfnwpOldFrameProc));
  221.             break;
  222.             }
  223.  
  224.         case WM_SEM2:
  225.             {
  226.             /*--------------------------------------------------------------*/
  227.             /*                                                              */
  228.             /*--------------------------------------------------------------*/
  229.             WinStartTimer (hAB, hWnd, 1, 1500);
  230.             break;
  231.             }
  232.  
  233.         case WM_TIMER:
  234.             {
  235.             /*--------------------------------------------------------------*/
  236.             /*  Push the window to the bottom of the pile.                  */
  237.             /*--------------------------------------------------------------*/
  238.             WinStopTimer (hAB, hWnd, 1);
  239.             WinSetWindowPos (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  240.                             HWND_BOTTOM,
  241.                             0,
  242.                             0,
  243.                             0,
  244.                             0,
  245.                             SWP_ZORDER);
  246.             break;
  247.             }
  248.  
  249.         case WM_QUERYTRACKINFO:
  250.             {
  251.             /*--------------------------------------------------------------*/
  252.             /*  Prevent the user moving the window                          */
  253.             /*--------------------------------------------------------------*/
  254.             return (MRESULT)FALSE;
  255.             }
  256.  
  257.         case WM_BUTTON1DOWN:
  258.             {
  259.             /*--------------------------------------------------------------*/
  260.             /*  Push the window to the bottom of the pile.                  */
  261.             /*--------------------------------------------------------------*/
  262.             WinSetWindowPos (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  263.                              HWND_BOTTOM,
  264.                              0,
  265.                              0,
  266.                              0,
  267.                              0,
  268.                              SWP_ZORDER);
  269.             break;
  270.             }
  271.  
  272.         case WM_BUTTON1UP:
  273.         case WM_ACTIVATE:
  274.             {
  275.             /*--------------------------------------------------------------*/
  276.             /*  Push the window to the bottom of the pile.                  */
  277.             /*--------------------------------------------------------------*/
  278.             WinSetWindowPos (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  279.                              HWND_BOTTOM,
  280.                              0,
  281.                              0,
  282.                              0,
  283.                              0,
  284.                              SWP_ZORDER);
  285.             return (WinDefWindowProc (hWnd, Message, mp1, mp2));
  286.             break;
  287.             }
  288.  
  289.         case WM_PAINT:
  290.             {
  291.             /*--------------------------------------------------------------*/
  292.             /*  Draw the bitmap on the screen and push it to the bottom of  */
  293.             /*  the pile.                                                   */
  294.             /*--------------------------------------------------------------*/
  295.             hPS = WinBeginPaint (hWnd, (HPS) NULL, &Rect);
  296.             WinQueryWindowRect (hWnd, &rcl);
  297.             WinSetPointer (HWND_DESKTOP,
  298.                            WinQuerySysPointer (HWND_DESKTOP, SPTR_WAIT, FALSE));
  299.  
  300.             WinDrawBitmap (hPS,
  301.                            Bitmap,
  302.                            NULL,
  303.                            (PPOINTL) &rcl,
  304.                            CLR_BLACK,
  305.                            CLR_BLACK,
  306.                            DBM_NORMAL);
  307.  
  308.             WinSetPointer (HWND_DESKTOP,
  309.                            WinQuerySysPointer (HWND_DESKTOP, SPTR_ARROW, FALSE));
  310.             
  311.             WinEndPaint (hPS);
  312.             
  313.             WinSetWindowPos (WinQueryWindow (hWnd, QW_PARENT, FALSE),
  314.                              HWND_BOTTOM,
  315.                              0,
  316.                              0,
  317.                              0,
  318.                              0,
  319.                              SWP_ZORDER);
  320.             break;
  321.             }
  322.  
  323.         default:
  324.             {
  325.             return (WinDefWindowProc (hWnd, Message, mp1, mp2));
  326.             }
  327.         }     
  328.     return (MRESULT) 0;
  329.     }
  330.  
  331. MRESULT EXPENTRY FrameSubclassProc (HWND    hWnd,
  332.                                     USHORT  Message,
  333.                                     MPARAM  mp1,
  334.                                     MPARAM  mp2)
  335.     {
  336.     switch (Message)
  337.         {
  338.         case WM_QUERYTRACKINFO:
  339.             {
  340.             /*--------------------------------------------------------------*/
  341.             /*  Prevent the user moving the window                          */
  342.             /*--------------------------------------------------------------*/
  343.             return (MRESULT)FALSE;
  344.             }
  345.  
  346.         default:
  347.             {
  348.             return (MRESULT)(* pfnwpOldFrameProc) (hWnd, Message, mp1, mp2);
  349.             }
  350.         }
  351.     }
  352.