1、BitmapMenu#include #include resource.hVOID WINAPI LoadChartBitmaps(HBITMAP *paHbm);VOID WINAPI CreateLineBitmaps(HBITMAP *paHbm, HWND hwnd);VOID WINAPI AddBitmapMenu(HWND hwnd, LPTSTR lpszText, UINT uID, HBITMAP *paHbm, int cItems); LRESULT CALLBACK MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wPara
2、m, LPARAM lParam );/ Menu-item identifiers #define IDM_SOLID 500 #define IDM_DASH 501 #define IDM_DOT 502#define IDM_DASHDOT 503 #define IDM_DASHDOTDOT 504 / Number of items on the Chart and Lines menus #define C_LINES 5 #define C_CHARTS 3 / Dimensions of the line bitmaps #define CX_LINEBMP 40 #defi
3、ne CY_LINEBMP 10 HINSTANCE g_hinst ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) TCHAR szAppName 16, szCaption 64, szErrMsg 64 ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; LoadString (hInstance, IDS_APPNAME, szAppName, sizeof (szAppName) / sizeof (TC
4、HAR) ; LoadString (hInstance, IDS_CAPTION, szCaption, sizeof (szCaption) / sizeof (TCHAR) ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = MainWindowProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (hInstance, sz
5、AppName) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName, sizeof (szApp
6、Name) ; LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg, sizeof (szErrMsg) ; MessageBoxA (NULL, (char *) szErrMsg, (char *) szAppName, MB_ICONERROR) ; return 0 ; g_hinst =hInstance; hwnd = CreateWindow (szAppName, szCaption, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT,
7、CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ;LRESULT CALLBACK MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPa
8、ram ) static HBITMAP aHbmLinesC_LINES; static HBITMAP aHbmChartC_CHARTS; int i; switch (uMsg) case WM_CREATE: / Call application-defined functions to load the / bitmaps for the Chart menu and create those for / the Lines menu. LoadChartBitmaps(aHbmChart); CreateLineBitmaps(aHbmLines,hwnd); / Call an
9、 application-defined function to create / menus containing the bitmap menu items. The function / also adds a menu name to the windows menu bar. AddBitmapMenu( hwnd, / menu bars owner window TEXT(&Chart), / text of menu name on menu bar IDB_PIE, / ID of first item on menu aHbmChart, / array of bitmap
10、 handles C_CHARTS / number of items on menu ); AddBitmapMenu(hwnd, TEXT(&Lines), IDM_SOLID, aHbmLines, C_LINES); DrawMenuBar(hwnd); break; case WM_DESTROY: for (i = 0; i C_LINES; i+) DeleteObject(aHbmLinesi); for (i = 0; i C_CHARTS; i+) DeleteObject(aHbmCharti); PostQuitMessage(0); break; / Process
11、additional messages here. default: return (DefWindowProc(hwnd, uMsg, wParam, lParam); return 0; VOID WINAPI LoadChartBitmaps(HBITMAP *paHbm) paHbm0 = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_PIE); paHbm1 = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_LINE); paHbm2 = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB
12、_BAR); VOID WINAPI CreateLineBitmaps(HBITMAP *paHbm, HWND hwnd) HDC hdc = GetDC(hwnd); HDC hdcMem = CreateCompatibleDC(hdc); COLORREF clrMenu = GetSysColor(COLOR_MENU); HBRUSH hbrOld; HPEN hpenOld; HBITMAP hbmOld; int fnDrawMode; int i; / Create a brush using the menu background color, / and select
13、it into the memory DC. hbrOld = SelectObject(hdcMem, CreateSolidBrush(clrMenu); / Create the bitmaps. Select each one into the memory / DC that was created and draw in it. for (i = 0; i C_LINES; i+) / Create the bitmap and select it into the DC. paHbmi = CreateCompatibleBitmap(hdc, CX_LINEBMP, CY_LI
14、NEBMP); hbmOld = SelectObject(hdcMem, paHbmi); / Fill the background using the brush. PatBlt(hdcMem, 0, 0, CX_LINEBMP, CY_LINEBMP, PATCOPY); / Create the pen and select it into the DC. hpenOld = SelectObject(hdcMem, CreatePen(PS_SOLID + i, 1, RGB(0, 0, 0); / Draw the line. To preserve the background
15、 color where / the pen is white, use the R2_MASKPEN drawing mode. fnDrawMode = SetROP2(hdcMem, R2_MASKPEN); MoveToEx(hdcMem, 0, CY_LINEBMP / 2, NULL); LineTo(hdcMem, CX_LINEBMP, CY_LINEBMP / 2); SetROP2(hdcMem, fnDrawMode); / Delete the pen, and select the old pen and bitmap. DeleteObject(SelectObje
16、ct(hdcMem, hpenOld); SelectObject(hdcMem, hbmOld); / Delete the brush and select the original brush. DeleteObject(SelectObject(hdcMem, hbrOld); / Delete the memory DC and release the desktop DC. DeleteDC(hdcMem); ReleaseDC(hwnd, hdc); VOID WINAPI AddBitmapMenu( HWND hwnd, / window that owned the men
17、u bar LPTSTR lpszText, / text of menu name on menu bar UINT uID, / ID of first bitmap menu item HBITMAP *paHbm, / bitmaps for the menu items int cItems ) / number bitmap menu items HMENU hmenuBar = GetMenu(hwnd); HMENU hmenuPopup = CreatePopupMenu(); MENUITEMINFO mii; int i; / Add the bitmap menu it
18、ems to the menu. mii.cbSize = sizeof(MENUITEMINFO); for (i = 0; i cItems; i+) mii.fMask = MIIM_ID | MIIM_BITMAP; mii.wID = uID + i; mii.hbmpItem = paHbmi; / mii.dwItemData = IDStart + i; InsertMenuItem(hmenuPopup, i, TRUE, &mii); / Add a menu name to the menu bar. mii.fMask = MIIM_STRING | MIIM_DATA | MIIM_SUBMENU; mii.fType = MFT_STRING; mii.hSubMenu = hmenuPopup; mii.dwTypeData = lpszText; InsertMenuItem(hmenuBar, GetMenuItemCount(hmenuBar), TRUE, &mii);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1