home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_sdk / bitmap / bitmap.c next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  16.5 KB  |  533 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Bitmap.c
  4.  
  5.     PURPOSE: Demonstrates how to use bitmap
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.         MakeColorBitmap(HWND) - creates a color bitmap
  15.  
  16.     COMMENTS:
  17.  
  18.         This application is linked with select.exe which is a library
  19.         module.  For the source code, look in the \select\ directory, and
  20.         in Appendix C of the Windows Programmer's Learning Guide.
  21.  
  22. ****************************************************************************/
  23.  
  24. #include "windows.h"
  25. #include "bitmap.h"
  26. #include "select.h"                /* used to link with select.exe */
  27.  
  28. HANDLE hInst;
  29.  
  30. /* Patterns used for the background */
  31.  
  32. short White[] =  { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  33. short Black[] =  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  34. short Zigzag[] = { 0xFF, 0xF7, 0xEB, 0xDD, 0xBE, 0x7F, 0xFF, 0xFF };
  35. short CrossHatch[] = { 0xEF, 0xEF, 0xEF, 0xEF, 0x00, 0xEF, 0xEF, 0xEF };
  36.  
  37. /* handles used for the various bitmaps */
  38.  
  39. HBITMAP hPattern1;
  40. HBITMAP hPattern2;
  41. HBITMAP hPattern3;
  42. HBITMAP hPattern4;
  43. HBITMAP hBitmap1;
  44. HBITMAP hBitmap2;
  45. HBITMAP hBitmap3;
  46. HBITMAP hMenuBitmap1;
  47. HBITMAP hMenuBitmap2;
  48. HBITMAP hMenuBitmap3;
  49. HBITMAP hBitmap;
  50. HBITMAP hOldBitmap;
  51.  
  52. HBRUSH hBrush;                         /* brush handle                       */
  53. int fStretchMode;                      /* type of stretch mode to use        */
  54.  
  55. HDC hDC;                               /* handle to device context           */
  56. HDC hMemoryDC;                         /* handle to memory device context    */
  57. BITMAP Bitmap;                         /* bitmap structure                   */
  58.  
  59. BOOL bTrack = FALSE;                   /* TRUE if user is selecting a region */
  60. RECT Rect;
  61.  
  62. /* The following variables keep track of which menu item is checked */
  63.  
  64. WORD wPrevBitmap = IDM_BITMAP1;
  65. WORD wPrevPattern = IDM_PATTERN1;
  66. WORD wPrevMode = IDM_WHITEONBLACK;
  67. WORD wPrevItem;
  68.  
  69. int Shape = SL_BLOCK;            /* shape to use for the selection rectangle */
  70.  
  71. /****************************************************************************
  72.  
  73.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  74.  
  75.     PURPOSE: calls initialization function, processes message loop
  76.  
  77. ****************************************************************************/
  78.  
  79. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  80. HANDLE hInstance;
  81. HANDLE hPrevInstance;
  82. LPSTR lpCmdLine;
  83. int nCmdShow;
  84. {
  85.     MSG msg;
  86.  
  87.     if (!hPrevInstance)
  88.     if (!InitApplication(hInstance))
  89.         return (FALSE);
  90.  
  91.     if (!InitInstance(hInstance, nCmdShow))
  92.         return (FALSE);
  93.  
  94.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  95.     TranslateMessage(&msg);
  96.     DispatchMessage(&msg);
  97.     }
  98.     return (msg.wParam);
  99. }
  100.  
  101.  
  102. /****************************************************************************
  103.  
  104.     FUNCTION: InitApplication(HANDLE)
  105.  
  106.     PURPOSE: Initializes window data and registers window class
  107.  
  108. ****************************************************************************/
  109.  
  110. BOOL InitApplication(hInstance)
  111. HANDLE hInstance;
  112. {
  113.     WNDCLASS  wc;
  114.  
  115.     wc.style = CS_VREDRAW | CS_HREDRAW;
  116.     wc.lpfnWndProc = MainWndProc;
  117.     wc.cbClsExtra = 0;
  118.     wc.cbWndExtra = 0;
  119.     wc.hInstance = hInstance;
  120.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  121.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  122.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  123.     wc.lpszMenuName =  "BitmapMenu";
  124.     wc.lpszClassName = "BitmapWClass";
  125.  
  126.     return (RegisterClass(&wc));
  127. }
  128.  
  129.  
  130. /****************************************************************************
  131.  
  132.     FUNCTION:  InitInstance(HANDLE, int)
  133.  
  134.     PURPOSE:  Saves instance handle and creates main window
  135.  
  136. ****************************************************************************/
  137.  
  138. BOOL InitInstance(hInstance, nCmdShow)
  139.     HANDLE          hInstance;
  140.     int             nCmdShow;
  141. {
  142.     HWND            hwnd;
  143.  
  144.     hInst = hInstance;
  145.  
  146.     hwnd = CreateWindow(
  147.         "BitmapWClass",
  148.         "Bitmap Sample Application",
  149.         WS_OVERLAPPEDWINDOW,
  150.         CW_USEDEFAULT,
  151.         CW_USEDEFAULT,
  152.         CW_USEDEFAULT,
  153.         CW_USEDEFAULT,
  154.         NULL,
  155.         NULL,
  156.         hInstance,
  157.         NULL
  158.     );
  159.  
  160.     if (!hwnd)
  161.         return (FALSE);
  162.  
  163.     ShowWindow(hwnd, nCmdShow);
  164.     UpdateWindow(hwnd);
  165.     return (TRUE);
  166.  
  167. }
  168.  
  169. /****************************************************************************
  170.  
  171.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  172.  
  173.     PURPOSE:  Processes messages
  174.  
  175.     MESSAGES:
  176.  
  177.     WM_COMMAND    - application menu (About dialog box)
  178.         WM_CREATE      - create window and objects
  179.         WM_LBUTTONDOWN - begin selection
  180.         WM_MOUSEMOVE   - keep track of mouse movement during selection
  181.         WM_LBUTTONUP   - end selection, draw bitmap
  182.         WM_RBUTTONUP   - draw bitmap without resizing
  183.         WM_ERASEBKGND  - erase background and redraw
  184.         WM_DESTROY     - destroy window
  185.  
  186.     COMMENTS:
  187.  
  188.         User may select a "normal" size bitmap by pressing the right mouse
  189.         button, or set the size of the bitmap to display by using the left
  190.         button to define a region.  The routines to display the selection
  191.         region are defined in the library module select.exe.
  192.     WM_DESTROY    - destroy window
  193.  
  194. ****************************************************************************/
  195.  
  196. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  197. HWND hWnd;
  198. unsigned message;
  199. WORD wParam;
  200. LONG lParam;
  201. {
  202.     FARPROC lpProcAbout;
  203.  
  204.     HMENU hMenu;
  205.     HBRUSH hOldBrush;
  206.     HBITMAP hOurBitmap;
  207.  
  208.     switch (message) {
  209.         case WM_CREATE:                            /* message: create window */
  210.  
  211.             hPattern1 = CreateBitmap(8, 8, 1, 1, (LPSTR) White);
  212.             hPattern2 = CreateBitmap(8, 8, 1, 1, (LPSTR) Black);
  213.             hPattern3 = CreateBitmap(8, 8, 1, 1, (LPSTR) Zigzag);
  214.             hPattern4 = CreateBitmap(8, 8, 1, 1, (LPSTR) CrossHatch);
  215.  
  216.             hBitmap1 = LoadBitmap(hInst, "dog");
  217.             hBitmap2 = LoadBitmap(hInst, "cat");
  218.             hBitmap3 = MakeColorBitmap(hWnd);
  219.  
  220.             hMenuBitmap1 = LoadBitmap(hInst, "dog");
  221.             hMenuBitmap2 = LoadBitmap(hInst, "cat");
  222.             hMenuBitmap3 = MakeColorBitmap(hWnd);
  223.  
  224.             hMenu = CreateMenu();
  225.         AppendMenu(hMenu, MF_STRING | MF_CHECKED, IDM_PATTERN1, "&White");
  226.         AppendMenu(hMenu, MF_STRING, IDM_PATTERN2, "&Black");
  227.             AppendMenu(hMenu, MF_BITMAP, IDM_PATTERN3, 
  228.                (LPSTR)(LONG)hPattern3);
  229.             AppendMenu(hMenu, MF_BITMAP, IDM_PATTERN4, 
  230.                (LPSTR)(LONG)hPattern4);
  231.  
  232.         ModifyMenu(GetMenu(hWnd), 1, MF_POPUP | MF_BYPOSITION, 
  233.                (WORD)hMenu, "&Pattern");
  234.  
  235.             hMenu = CreateMenu();
  236.  
  237.             /* Use bitmaps for menu items */
  238.             AppendMenu(hMenu, MF_BITMAP | MF_CHECKED, IDM_BITMAP1,
  239.                (LPSTR)(LONG) hMenuBitmap1);
  240.             AppendMenu(hMenu, MF_BITMAP, IDM_BITMAP2,
  241.                (LPSTR)(LONG) hMenuBitmap2);
  242.             AppendMenu(hMenu, MF_BITMAP, IDM_BITMAP3,
  243.                (LPSTR)(LONG) hMenuBitmap3);
  244.             ModifyMenu(GetMenu(hWnd), 0, MF_POPUP | MF_BYPOSITION,
  245.                        (WORD) hMenu, "&Bitmap");
  246.  
  247.             hBrush = CreatePatternBrush(hPattern1);
  248.             fStretchMode = IDM_BLACKONWHITE;
  249.  
  250.             /* Select the first bitmap */
  251.  
  252.             hDC = GetDC(hWnd);
  253.             hMemoryDC = CreateCompatibleDC(hDC);
  254.             ReleaseDC(hWnd, hDC);
  255.             hOldBitmap = SelectObject(hMemoryDC, hBitmap1);
  256.             GetObject(hBitmap1, 16, (LPSTR) &Bitmap);
  257.  
  258.             break;
  259.  
  260.         case WM_LBUTTONDOWN:           /* message: left mouse button pressed */
  261.  
  262.             /* Start selection of region */
  263.  
  264.             bTrack = TRUE;
  265.             SetRectEmpty(&Rect);
  266.             StartSelection(hWnd, MAKEPOINT(lParam), &Rect,
  267.                 (wParam & MK_SHIFT) ? (SL_EXTEND | Shape) : Shape);
  268.             break;
  269.  
  270.         case WM_MOUSEMOVE:                        /* message: mouse movement */
  271.  
  272.             /* Update the selection region */
  273.  
  274.             if (bTrack)
  275.                 UpdateSelection(hWnd, MAKEPOINT(lParam), &Rect, Shape);
  276.             break;
  277.  
  278.         case WM_LBUTTONUP:            /* message: left mouse button released */
  279.  
  280.             if (bTrack) {
  281.                /* End the selection */
  282.  
  283.                EndSelection(MAKEPOINT(lParam), &Rect);
  284.                ClearSelection(hWnd, &Rect, Shape);
  285.  
  286.                hDC = GetDC(hWnd);
  287.                SetStretchBltMode(hDC, fStretchMode);
  288.                StretchBlt(hDC, Rect.left, Rect.top,
  289.                    Rect.right - Rect.left, Rect.bottom - Rect.top,
  290.                   hMemoryDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, SRCCOPY);
  291.                ReleaseDC(hWnd, hDC);
  292.             }
  293.  
  294.             bTrack = FALSE;
  295.             break;
  296.  
  297.         case WM_RBUTTONUP:           /* message: right mouse button released */
  298.  
  299.             /* Display a normal sized bitmap */
  300.  
  301.             hDC = GetDC(hWnd);
  302.             BitBlt(hDC, LOWORD(lParam), HIWORD(lParam),
  303.                 Bitmap.bmWidth, Bitmap.bmHeight, hMemoryDC, 0, 0, SRCCOPY);
  304.             ReleaseDC(hWnd, hDC);
  305.             break;
  306.  
  307.         case WM_ERASEBKGND:                    /* messsage: erase background */
  308.  
  309.             /* Repaint the background */
  310.  
  311.             UnrealizeObject(hBrush);
  312.             hOldBrush = SelectObject(wParam, hBrush);
  313.             GetClientRect(hWnd, &Rect);
  314.             PatBlt(wParam, Rect.left, Rect.top, 
  315.                 Rect.right-Rect.left, Rect.bottom-Rect.top, PATCOPY);
  316.             SelectObject(wParam, hOldBrush);
  317.             return TRUE;
  318.  
  319.     case WM_COMMAND:
  320.             switch (wParam) {
  321.                 case IDM_ABOUT:
  322.             lpProcAbout = MakeProcInstance(About, hInst);
  323.             DialogBox(hInst,
  324.                 "AboutBox",
  325.                 hWnd,
  326.                 lpProcAbout);
  327.             FreeProcInstance(lpProcAbout);
  328.             break;
  329.  
  330.                 case IDM_BITMAP1:
  331.                     wPrevItem = wPrevBitmap;
  332.                     wPrevBitmap = wParam;
  333.                     GetObject(hBitmap1, 16, (LPSTR) &Bitmap);
  334.                     SelectObject(hMemoryDC, hBitmap1);
  335.                     break;
  336.  
  337.                 case IDM_BITMAP2:
  338.                     wPrevItem = wPrevBitmap;
  339.                     wPrevBitmap = wParam;
  340.                     GetObject(hBitmap2, 16, (LPSTR) &Bitmap);
  341.                     SelectObject(hMemoryDC, hBitmap2);
  342.                     break;
  343.  
  344.                 case IDM_BITMAP3:
  345.                     wPrevItem = wPrevBitmap;
  346.                     wPrevBitmap = wParam;
  347.                     GetObject(hBitmap3, 16, (LPSTR) &Bitmap);
  348.                     hOurBitmap = SelectObject(hMemoryDC, hBitmap3);
  349.                     break;
  350.  
  351.                 /* Pattern menu: select the background brush to use */
  352.  
  353.                 case IDM_PATTERN1:
  354.                     wPrevItem = wPrevPattern;
  355.                     wPrevPattern = wParam;
  356.                     DeleteObject(hBrush);
  357.                     hBrush = CreatePatternBrush(hPattern1);
  358.                     InvalidateRect(hWnd, (LPRECT) NULL, TRUE);
  359.                     UpdateWindow(hWnd);
  360.                     break;
  361.  
  362.                 case IDM_PATTERN2:
  363.                     wPrevItem = wPrevPattern;
  364.                     wPrevPattern = wParam;
  365.                     DeleteObject(hBrush);
  366.                     hBrush = CreatePatternBrush(hPattern2);
  367.                     InvalidateRect(hWnd, (LPRECT) NULL, TRUE);
  368.                     UpdateWindow(hWnd);
  369.                     break;
  370.  
  371.                 case IDM_PATTERN3:
  372.                     wPrevItem = wPrevPattern;
  373.                     wPrevPattern = wParam;
  374.                     DeleteObject(hBrush);
  375.                     hBrush = CreatePatternBrush(hPattern3);
  376.                     InvalidateRect(hWnd, (LPRECT) NULL, TRUE);
  377.                     UpdateWindow(hWnd);
  378.                     break;
  379.  
  380.                 case IDM_PATTERN4:
  381.                     wPrevItem = wPrevPattern;
  382.                     wPrevPattern = wParam;
  383.                     DeleteObject(hBrush);
  384.                     hBrush = CreatePatternBrush(hPattern4);
  385.                     InvalidateRect(hWnd, (LPRECT) NULL, TRUE);
  386.                     UpdateWindow(hWnd);
  387.                     break;
  388.  
  389.                 /* Mode menu: select the stretch mode to use */
  390.  
  391.                 case IDM_BLACKONWHITE:
  392.                     wPrevItem = wPrevMode;
  393.                     wPrevMode = wParam;
  394.                     fStretchMode = BLACKONWHITE;
  395.                     break;
  396.  
  397.                 case IDM_WHITEONBLACK:
  398.                     wPrevItem = wPrevMode;
  399.                     wPrevMode = wParam;
  400.                     fStretchMode = WHITEONBLACK;
  401.                     break;
  402.  
  403.                 case IDM_COLORONCOLOR:
  404.                     wPrevItem = wPrevMode;
  405.                     wPrevMode = wParam;
  406.                     fStretchMode = COLORONCOLOR;
  407.                     break;
  408.             }
  409.  
  410.             /* Uncheck the old item, check the new item */
  411.  
  412.             CheckMenuItem(GetMenu(hWnd), wPrevItem, MF_UNCHECKED);
  413.             CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
  414.             break;
  415.  
  416.  
  417.     case WM_DESTROY:
  418.             SelectObject(hMemoryDC, hOldBitmap);
  419.             DeleteDC(hMemoryDC);
  420.             DeleteObject(hBrush);
  421.             DeleteObject(hPattern1);
  422.             DeleteObject(hPattern2);
  423.             DeleteObject(hPattern3);
  424.             DeleteObject(hPattern4);
  425.             DeleteObject(hBitmap1);
  426.             DeleteObject(hBitmap2);
  427.             DeleteObject(hBitmap3);
  428.             DeleteObject(hMenuBitmap1);
  429.             DeleteObject(hMenuBitmap2);
  430.             DeleteObject(hMenuBitmap3);
  431.  
  432.         PostQuitMessage(0);
  433.         break;
  434.  
  435.     default:
  436.         return (DefWindowProc(hWnd, message, wParam, lParam));
  437.     }
  438.     return (NULL);
  439. }
  440.  
  441.  
  442. /****************************************************************************
  443.  
  444.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  445.  
  446.     PURPOSE:  Processes messages for "About" dialog box
  447.  
  448.     MESSAGES:
  449.  
  450.     WM_INITDIALOG - initialize dialog box
  451.     WM_COMMAND    - Input received
  452.  
  453. ****************************************************************************/
  454.  
  455. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  456. HWND hDlg;
  457. unsigned message;
  458. WORD wParam;
  459. LONG lParam;
  460. {
  461.     switch (message) {
  462.     case WM_INITDIALOG:
  463.         return (TRUE);
  464.  
  465.     case WM_COMMAND:
  466.         if (wParam == IDOK
  467.                 || wParam == IDCANCEL) {
  468.         EndDialog(hDlg, TRUE);
  469.         return (TRUE);
  470.         }
  471.         break;
  472.     }
  473.     return (FALSE);
  474. }
  475.  
  476.  
  477.  
  478. /****************************************************************************
  479.  
  480.     FUNCTION: MakeColorBitmap(HWND)
  481.  
  482.     PURPOSE: Creates a color bitmap
  483.  
  484.     COMMENTS:
  485.  
  486.         This creates a plaid color bitmap by using overlappying colors.
  487.  
  488. ****************************************************************************/
  489.  
  490. HBITMAP MakeColorBitmap(hWnd)
  491. HWND hWnd;
  492. {
  493.     HDC hDC;
  494.     HDC hMemoryDC;
  495.     HBITMAP hBitmap;
  496.     HBITMAP hOldBitmap;
  497.     HBRUSH hRedBrush;
  498.     HBRUSH hGreenBrush;
  499.     HBRUSH hBlueBrush;
  500.     HBRUSH hOldBrush;
  501.  
  502.     hDC = GetDC(hWnd);
  503.     hMemoryDC = CreateCompatibleDC(hDC);
  504.     hBitmap = CreateCompatibleBitmap(hDC, 64, 32);
  505.     hOldBitmap = SelectObject(hMemoryDC, hBitmap);
  506.     hRedBrush = CreateSolidBrush(RGB(255,0,0));
  507.     hGreenBrush = CreateSolidBrush(RGB(0,255,0));
  508.     hBlueBrush = CreateSolidBrush(RGB(0,0,255));
  509.  
  510.     PatBlt(hMemoryDC, 0, 0, 64, 32, BLACKNESS);
  511.     hOldBrush = SelectObject(hMemoryDC, hRedBrush);
  512.     PatBlt(hMemoryDC, 0, 0, 24, 11, PATORDEST);
  513.     PatBlt(hMemoryDC, 40, 10, 24, 12, PATORDEST);
  514.     PatBlt(hMemoryDC, 20, 21, 24, 11, PATORDEST);
  515.     SelectObject(hMemoryDC, hGreenBrush);
  516.     PatBlt(hMemoryDC, 20, 0, 24, 11, PATORDEST);
  517.     PatBlt(hMemoryDC, 0, 10, 24, 12, PATORDEST);
  518.     PatBlt(hMemoryDC, 40, 21, 24, 11, PATORDEST);
  519.     SelectObject(hMemoryDC, hBlueBrush);
  520.     PatBlt(hMemoryDC, 40, 0, 24, 11, PATORDEST);
  521.     PatBlt(hMemoryDC, 20, 10, 24, 12, PATORDEST);
  522.     PatBlt(hMemoryDC, 0, 21, 24, 11, PATORDEST);
  523.  
  524.     SelectObject(hMemoryDC, hOldBrush);
  525.     DeleteObject(hRedBrush);
  526.     DeleteObject(hGreenBrush);
  527.     DeleteObject(hBlueBrush);
  528.     SelectObject(hMemoryDC, hOldBitmap);
  529.     DeleteDC(hMemoryDC);
  530.     ReleaseDC(hWnd, hDC);
  531.     return (hBitmap);
  532. }
  533.