home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / crbitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  6.9 KB  |  185 lines

  1. #include <windows.h>
  2. #include "crbitmap.h"
  3.  
  4. HANDLE hInst;    
  5. HBITMAP hBitmap;                    /* bitmap structure handle */
  6.  
  7. short nBlobBitmap[] = {
  8.         0x0000, 0x0000,
  9.         0x0000, 0x0000,
  10.         0x000f, 0xf000,
  11.         0x000f, 0xf000,
  12.         0x00ff, 0xff00,
  13.         0x00ff, 0xff00,
  14.         0x0fff, 0xfff0,
  15.         0x0fff, 0xfff0,
  16.         0x0ff0, 0x0ff0,
  17.         0x0ff0, 0x0ff0,
  18.         0xfff0, 0x0fff,
  19.         0xfff0, 0x0fff,
  20.         0xff00, 0x00ff,
  21.         0xff00, 0x00ff,
  22.         0xffff, 0xffff,
  23.         0xffff, 0xffff};
  24.  
  25. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  26. HANDLE hInstance;       /* current instance */
  27. HANDLE hPrevInstance;   /* previous instance */
  28. LPSTR lpCmdLine;        /* command line */
  29. int nCmdShow;           /* show window type (open/icon) */
  30. {
  31.     HWND hWnd;          /* window handle */
  32.     MSG msg;            /* message */
  33.     HMENU hMenu;        /* handle to the menu */
  34.     
  35.     if  (!hPrevInstance)        /* app already initialized? */
  36.         if (!CreateBitMapInit(hInstance))       /* nope, init it */
  37.             return(NULL);               /* couldn't init */
  38.  
  39.     hInst = hInstance;          /* store the current instance */
  40.     
  41.     hWnd = CreateWindow("CreateBitMap", /* window class */
  42.         "CreateBitMap Sample Application",      /* window name */
  43.         WS_OVERLAPPEDWINDOW,            /* window style */
  44.         CW_USEDEFAULT,                  /* x position */
  45.         CW_USEDEFAULT,                  /* y position */
  46.         CW_USEDEFAULT,                  /* width */
  47.         CW_USEDEFAULT,                  /* height */
  48.         NULL,                           /* parent handle */
  49.         NULL,                           /* menu or child */
  50.         hInstance,                      /* instance */
  51.         NULL);                          /* additional info */
  52.         
  53.     if (!hWnd)                  /* did we get a valid handle? */
  54.         return(NULL);           /* nope, couldn't open window */
  55.         
  56.     ShowWindow(hWnd, nCmdShow);         /* show the window */
  57.     UpdateWindow(hWnd);                 /* send the WM_PAINT msg */
  58.     
  59.     while (GetMessage(&msg, NULL, NULL, NULL))
  60.     {
  61.         TranslateMessage(&msg);
  62.         DispatchMessage(&msg);
  63.     }
  64.     return(msg.wParam);         /* return value from PostQuitMessage */
  65. }
  66.  
  67. /* register the window */
  68. BOOL CreateBitMapInit(hInstance)
  69. HANDLE hInstance; /* current instance */
  70. {
  71.     HANDLE hMemory;             /* handle to allocated memory */
  72.     PWNDCLASS pWndClass;        /* structure pointer */
  73.     BOOL bSuccess;              /* saves RegisterClass status */
  74.     
  75.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  76.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  77.     
  78.     pWndClass->lpszClassName = (LPSTR)  "CreateBitMap";
  79.     pWndClass->hInstance = hInstance;
  80.     pWndClass->lpfnWndProc = CreateBitMapWndProc;
  81.     pWndClass->style = NULL;
  82.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  83.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  84.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  85.     pWndClass->lpszMenuName = (LPSTR) NULL;
  86.     
  87.     bSuccess = RegisterClass(pWndClass);
  88.     
  89.     LocalUnlock(hMemory);               /* unlock the memory */
  90.     LocalFree(hMemory);                 /* return it to windows */
  91.     
  92.     return (bSuccess);
  93. }
  94.  
  95. /* process messages to the window */
  96.  
  97. long FAR PASCAL CreateBitMapWndProc(hWnd, message, wParam, lParam)
  98. HWND hWnd;                              /* window handle */
  99. unsigned message;                       /* type of message */
  100. WORD wParam;                            /* additional information */
  101. LONG lParam;                            /* additional information */
  102. {
  103.     FARPROC lpProcAbout;                /* pointer to "About" procedure */
  104.     HMENU hMenu;                        /* menu handle */
  105.     HDC hMemoryDC;                      /* memory device context */
  106.     HDC hDC;                            /* display device context */
  107.     HANDLE hOldObject;                  /* return code from SelectObject */
  108.  
  109.     switch (message)
  110.     {
  111.         case WM_CREATE :                /* add command to system menu */
  112.             hMenu = GetSystemMenu(hWnd, FALSE);
  113.             ChangeMenu(hMenu, NULL, NULL, NULL,
  114.                 MF_APPEND | MF_SEPARATOR);
  115.             ChangeMenu(hMenu, NULL, "A&bout CreateBitMap...",
  116.             ID_ABOUT, MF_APPEND | MF_STRING);
  117.             /* create bitmap */
  118.             hBitmap = CreateBitmap(32, 16, 1, 1, (LPSTR)nBlobBitmap);
  119.             if (hBitmap == NULL)
  120.                 MessageBox(NULL, (LPSTR)"Error creating Bitmap",
  121.                            NULL, IDOK);
  122.             break;
  123.             
  124.         case WM_SYSCOMMAND :
  125.             switch (wParam)
  126.             {
  127.                 case ID_ABOUT:
  128.                     lpProcAbout = MakeProcInstance(About, hInst);
  129.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  130.                     FreeProcInstance(lpProcAbout);
  131.                     break;
  132.                     
  133.                 default:
  134.                     return(DefWindowProc(hWnd, message, wParam, lParam));
  135.             }
  136.             break;
  137.  
  138.         case WM_PAINT:
  139.             hDC = GetDC(hWnd);
  140.             hMemoryDC = CreateCompatibleDC(hDC);
  141.             hOldObject = SelectObject(hMemoryDC, hBitmap);
  142.             if (hOldObject == NULL)
  143.                 MessageBox(NULL, (LPSTR)"Error selecting object",
  144.                            NULL, IDOK);
  145.             BitBlt(hDC, 50, 30, 32, 16, hMemoryDC, 0, 0, SRCCOPY);
  146.             /* clean up */
  147.             SelectObject(hMemoryDC, hOldObject);
  148.             DeleteDC(hMemoryDC);
  149.             ReleaseDC(hWnd, hDC);
  150.             break;
  151.                         
  152.         case WM_DESTROY:                /* quit application */
  153.         DeleteObject(hBitmap);    /* delete bitmap */
  154.             PostQuitMessage(NULL);      /* notify windows */
  155.             break;
  156.             
  157.         default:                        /* pass it on if unprocessed */
  158.             return(DefWindowProc(hWnd, message, wParam, lParam));
  159.     }
  160.     return(NULL);
  161. }
  162.                         
  163. /* this function handles the "About" box */
  164.  
  165. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  166. HWND hDlg;
  167. unsigned message;                       /* type of message */
  168. WORD wParam;                            /* additional information */
  169. LONG lParam;                            /* additional information */
  170. {
  171.     switch (message)
  172.     {
  173.         case WM_INITDIALOG:             /* init dialog box */
  174.             return(TRUE);               /* don't need to do anything */
  175.         case WM_COMMAND:                /* received a command */
  176.             if (wParam == IDOK)         /* OK box selected? */
  177.             {
  178.                 EndDialog (hDlg, NULL);         /* then exit */
  179.                 return(TRUE);
  180.             }
  181.             break;
  182.     }
  183.     return(FALSE);                      /* didn't process a message */
  184. }
  185.