BitmapMenu.docx
《BitmapMenu.docx》由会员分享,可在线阅读,更多相关《BitmapMenu.docx(8页珍藏版)》请在冰豆网上搜索。
BitmapMenu
#include
#include"resource.h"
VOIDWINAPILoadChartBitmaps(HBITMAP*paHbm);
VOIDWINAPICreateLineBitmaps(HBITMAP*paHbm,HWNDhwnd);
VOIDWINAPIAddBitmapMenu(HWNDhwnd,LPTSTRlpszText,UINTuID,HBITMAP*paHbm,intcItems);
LRESULTCALLBACKMainWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam);
//Menu-itemidentifiers
#defineIDM_SOLID500
#defineIDM_DASH501
#defineIDM_DOT502
#defineIDM_DASHDOT503
#defineIDM_DASHDOTDOT504
//NumberofitemsontheChartandLinesmenus
#defineC_LINES5
#defineC_CHARTS3
//Dimensionsofthelinebitmaps
#defineCX_LINEBMP40
#defineCY_LINEBMP10
HINSTANCEg_hinst;
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
PSTRszCmdLine,intiCmdShow)
{
TCHARszAppName[16],szCaption[64],szErrMsg[64];
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
LoadString(hInstance,IDS_APPNAME,szAppName,
sizeof(szAppName)/sizeof(TCHAR));
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,szAppName);
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(szAppName));
LoadStringA(hInstance,IDS_ERRMSG,(char*)szErrMsg,
sizeof(szErrMsg));
MessageBoxA(NULL,(char*)szErrMsg,
(char*)szAppName,MB_ICONERROR);
return0;
}
g_hinst=hInstance;
hwnd=CreateWindow(szAppName,szCaption,
WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
LRESULTCALLBACKMainWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
staticHBITMAPaHbmLines[C_LINES];
staticHBITMAPaHbmChart[C_CHARTS];
inti;
switch(uMsg)
{
caseWM_CREATE:
//Callapplication-definedfunctionstoloadthe
//bitmapsfortheChartmenuandcreatethosefor
//theLinesmenu.
LoadChartBitmaps(aHbmChart);
CreateLineBitmaps(aHbmLines,hwnd);
//Callanapplication-definedfunctiontocreate
//menuscontainingthebitmapmenuitems.Thefunction
//alsoaddsamenunametothewindow'smenubar.
AddBitmapMenu(
hwnd,//menubar'sownerwindow
TEXT("&Chart"),//textofmenunameonmenubar
IDB_PIE,//IDoffirstitemonmenu
aHbmChart,//arrayofbitmaphandles
C_CHARTS//numberofitemsonmenu
);
AddBitmapMenu(hwnd,TEXT("&Lines"),IDM_SOLID,
aHbmLines,C_LINES);
DrawMenuBar(hwnd);
break;
caseWM_DESTROY:
for(i=0;iDeleteObject(aHbmLines[i]);
for(i=0;iDeleteObject(aHbmChart[i]);
PostQuitMessage(0);
break;
//Processadditionalmessageshere.
default:
return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}
return0;
}
VOIDWINAPILoadChartBitmaps(HBITMAP*paHbm)
{
paHbm[0]=LoadBitmap(g_hinst,MAKEINTRESOURCE(IDB_PIE));
paHbm[1]=LoadBitmap(g_hinst,MAKEINTRESOURCE(IDB_LINE));
paHbm[2]=LoadBitmap(g_hinst,MAKEINTRESOURCE(IDB_BAR));
}
VOIDWINAPICreateLineBitmaps(HBITMAP*paHbm,HWNDhwnd)
{
HDChdc=GetDC(hwnd);
HDChdcMem=CreateCompatibleDC(hdc);
COLORREFclrMenu=GetSysColor(COLOR_MENU);
HBRUSHhbrOld;
HPENhpenOld;
HBITMAPhbmOld;
intfnDrawMode;
inti;
//Createabrushusingthemenubackgroundcolor,
//andselectitintothememoryDC.
hbrOld=SelectObject(hdcMem,CreateSolidBrush(clrMenu));
//Createthebitmaps.Selecteachoneintothememory
//DCthatwascreatedanddrawinit.
for(i=0;i{
//CreatethebitmapandselectitintotheDC.
paHbm[i]=CreateCompatibleBitmap(hdc,
CX_LINEBMP,CY_LINEBMP);
hbmOld=SelectObject(hdcMem,paHbm[i]);
//Fillthebackgroundusingthebrush.
PatBlt(hdcMem,0,0,CX_LINEBMP,CY_LINEBMP,PATCOPY);
//CreatethepenandselectitintotheDC.
hpenOld=SelectObject(hdcMem,
CreatePen(PS_SOLID+i,1,RGB(0,0,0)));
//Drawtheline.Topreservethebackgroundcolorwhere
//thepeniswhite,usetheR2_MASKPENdrawingmode.
fnDrawMode=SetROP2(hdcMem,R2_MASKPEN);
MoveToEx(hdcMem,0,CY_LINEBMP/2,NULL);
LineTo(hdcMem,CX_LINEBMP,CY_LINEBMP/2);
SetROP2(hdcMem,fnDrawMode);
//Deletethepen,andselecttheoldpenandbitmap.
DeleteObject(SelectObject(hdcMem,hpenOld));
SelectObject(hdcMem,hbmOld);
}
//Deletethebrushandselecttheoriginalbrush.
DeleteObject(SelectObject(hdcMem,hbrOld));
//DeletethememoryDCandreleasethedesktopDC.
DeleteDC(hdcMem);
ReleaseDC(hwnd,hdc);
}
VOIDWINAPIAddBitmapMenu(
HWNDhwnd,//windowthatownedthemenubar
LPTSTRlpszText,//textofmenunameonmenubar
UINTuID,//IDoffirstbitmapmenuitem
HBITMAP*paHbm,//bitmapsforthemenuitems
intcItems)//numberbitmapmenuitems
{
HMENUhmenuBar=GetMenu(hwnd);
HMENUhmenuPopup=CreatePopupMenu();
MENUITEMINFOmii;
inti;
//Addthebitmapmenuitemstothemenu.
mii.cbSize=sizeof(MENUITEMINFO);
for(i=0;i{
mii.fMask=MIIM_ID|MIIM_BITMAP;
mii.wID=uID+i;
mii.hbmpItem=paHbm[i];
//mii.dwItemData=IDStart+i;
InsertMenuItem(hmenuPopup,i,TRUE,&mii);
}
//Addamenunametothemenubar.
mii.fMask=MIIM_STRING|MIIM_DATA|MIIM_SUBMENU;
mii.fType=MFT_STRING;
mii.hSubMenu=hmenuPopup;
mii.dwTypeData=lpszText;
InsertMenuItem(hmenuBar,
GetMenuItemCount(hmenuBar),TRUE,&mii);
}