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