You need a WM_CREATE case and supporting variable and function declarations to create or load the bitmaps and to set the menus. The WM_CREATE case creates four 8-by-8-pixel, monochrome bitmaps to be used as patterns in a pattern brush for the window background. It also creates or loads three 64-by-32-pixel bitmaps to be displayed in the window. To let the user choose a bitmap or pattern for viewing, the WM_CREATE case adds them to the Bitmap and Pattern menus by using the AppendMenu function. Finally, the case sets the initial values of the brush, bitmap, and stretching modes and creates the memory device context from which the bitmaps are copied.
The WM_CREATE case creates the four patterns by using the CreateBitmap function. It loads two bitmaps, dog and cat, and creates a third by using the MakeColorBitmap function defined within the application. Once the patterns and bitmaps have been created, the WM_CREATE case creates pop-up menus, appends the patterns and bitmaps to the menus, and replaces the existing Bitmap and Pattern menus with the new pop-up menus. Next, the hBrush, hBitmap, and fStretchMode variables are set to the initial values for the background brush, bitmap, and stretching modes. Finally, the case creates the memory device context from which the bitmaps will be copied to the display. Add the following statements to your window function:
case WM_CREATE: /* message: create window */
hPattern1 = CreateBitmap(8, 8, 1, 1, (LPSTR) White);
hPattern2 = CreateBitmap(8, 8, 1, 1, (LPSTR) Black);
hPattern3 = CreateBitmap(8, 8, 1, 1, (LPSTR) Zigzag);
hPattern4 = CreateBitmap(8, 8, 1, 1, (LPSTR) CrossHatch);
hBitmap1 = LoadBitmap(hInst, “dog”);
hBitmap2 = LoadBitmap(hInst, “cat”);
hBitmap3 = MakeColorBitmap(hWnd);
hMenuBitmap1 = LoadBitmap(hInst, “dog”);
hMenuBitmap2 = LoadBitmap(hInst, “cat”);
hMenuBitmap3 = MakeColorBitmap(hWnd);
hMenu = CreateMenu();
AppendMenu(hMenu, MF_STRING | MF_CHECKED, IDM_PATTERN1, “&White”);
AppendMenu(hMenu, MF_STRING, IDM_PATTERN2, “&Black”);
AppendMenu(hMenu, MF_BITMAP, IDM_PATTERN3, (LPSTR) (LONG) hPattern3);
AppendMenu(hMenu, MF_BITMAP, IDM_PATTERN4, (LPSTR) (LONG) hPattern4);
ModifyMenu(GetMenu(hWnd), 1, MF_POPUP | MF_BYPOSITION, hMenu, “&Pattern”);
hMenu = CreateMenu();
AppendMenu(hMenu, MF_BITMAP | MF_CHECKED, IDM_BITMAP1, (LPSTR) (LONG) hMenuBitmap1);
AppendMenu(hMenu, MF_BITMAP, IDM_BITMAP2, (LPSTR) (LONG) hMenuBitmap2);
AppendMenu(hMenu, MF_BITMAP, IDM_BITMAP3, (LPSTR) (LONG) hMenuBitmap3);
ModifyMenu(GetMenu(hWnd), 0, MF_POPUP | MF_BYPOSITION, “&Bitmap”, hMenu);
hBrush = CreatePatternBrush(hPattern1);
fStretchMode = IDM_BLACKONWHITE;
hDC = GetDC(hWnd);
hMemoryDC = CreateCompatibleDC(hDC);
ReleaseDC(hWnd, hDC);
hOldBitmap = SelectObject(hMemoryDC, hBitmap1);
GetObject(hBitmap1, 16, (LPSTR) &Bitmap);
break;
The CreateBitmap and LoadBitmap functions work as described in earlier sections in this chapter. The MakeColorBitmap function is created for this application. It creates and draws a color bitmap, using the same method described in “Creating and Filling a Blank Bitmap”. The statements of this function are given later in this section. Notice that each bitmap is loaded or created twice. This is required since no single bitmap handle may be selected into two device contexts at the same time. To display in a menu requires a selection, and to display in the client area also requires a selection.
The CreateMenu function creates an empty menu and returns a handle to the menu. The ChangeMenu functions that specify the pattern handles add the patterns as menu items to the new menu. The MF_BITMAP option specifies that a bitmap will be added. The CheckMenuItem function places a checkmark next to the current menu item, and the last ChangeMenu function replaces the existing Pattern menu. The same steps are then repeated for the Bitmap menu.
The CreateCompatibleDC function creates a memory device context that is compatible with the display. The SelectObject function selects the current bitmap into the memory device context so that it is ready to be copied to the display. The GetObject function copies the dimensions of the bitmap into the Bitmap structure. The structure can then be used in subsequent BitBlt and StretchBlt functions to specify the width and height of the bitmap.
The following MakeColorBitmap function creates a color bitmap by creating a bitmap that is compatible with the display, then paints a plaid color pattern by using red, green, and blue brushes and the PatBlt function. Add the following function definition to the end of your source file:
HBITMAP MakeColorBitmap(hWnd)
HWND hWnd;
{
HDC hDC;
HDC hMemoryDC;
HBITMAP hBitmap;
HBITMAP hOldBitmap;
HBRUSH hRedBrush;
HBRUSH hGreenBrush;
HBRUSH hBlueBrush;
HBRUSH hOldBrush;
hDC = GetDC(hWnd);
hMemoryDC = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap(hDC, 64, 32);
hOldBitmap = SelectObject(hMemoryDC, hBitmap);
hRedBrush = CreateSolidBrush(RGB(255,0,0));
hGreenBrush = CreateSolidBrush(RGB(0,255,0));
hBlueBrush = CreateSolidBrush(RGB(0,0,255));
PatBlt(hMemoryDC, 0, 0, 64, 32, BLACKNESS);
hOldBrush = SelectObject(hMemoryDC, hRedBrush);
PatBlt(hMemoryDC, 0, 0, 24, 11, PATORDEST);
PatBlt(hMemoryDC, 40, 10, 24, 12, PATORDEST);
PatBlt(hMemoryDC, 20, 21, 24, 11, PATORDEST);
SelectObject(hMemoryDC, hGreenBrush);
PatBlt(hMemoryDC, 20, 0, 24, 11, PATORDEST);
PatBlt(hMemoryDC, 0, 10, 24, 12, PATORDEST);
PatBlt(hMemoryDC, 40, 21, 24, 11, PATORDEST);
SelectObject(hMemoryDC, hBlueBrush);
PatBlt(hMemoryDC, 40, 0, 24, 11, PATORDEST);
PatBlt(hMemoryDC, 20, 10, 24, 12, PATORDEST);
PatBlt(hMemoryDC, 0, 21, 24, 11, PATORDEST);
SelectObject(hMemoryDC, hOldBrush);
DeleteObject(hRedBrush);
DeleteObject(hGreenBrush);
DeleteObject(hBlueBrush);
SelectObject(hMemoryDC, hOldBitmap);
DeleteDC(hMemoryDC);
ReleaseDC(hWnd, hDC);
return (hBitmap);
}
For details on the steps carried out in this function, see “Creating a Bitmap with Hard-Coded Bits”.