home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c045 / 5.ddi / BITMAP / BITMAP.C$ / BITMAP.bin
Encoding:
Text File  |  1992-01-01  |  13.8 KB  |  504 lines

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