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