Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

MyMenu.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 
00003 #include "MyString.h"
00004 #include "MyMenu.h"
00005 
00006 CMyMenu::CMyMenu()
00007 {
00008         m_hFont = CreatePopupMenuTitleFont();
00009         clLeft=GetSysColor(COLOR_ACTIVECAPTION);
00010         clRight=GetSysColor(27);  //COLOR_GRADIENTACTIVECAPTION
00011         clText=GetSysColor(COLOR_CAPTIONTEXT);
00012         
00013         hinst_msimg32 = LoadLibrary( "msimg32.dll" );
00014         m_bCanDoGradientFill = FALSE;
00015         
00016         if(hinst_msimg32)
00017         {
00018                 m_bCanDoGradientFill = TRUE;            
00019                 dllfunc_GradientFill = ((LPFNDLLFUNC1) GetProcAddress( hinst_msimg32, "GradientFill" ));
00020         }
00021         bDrawEdge=false;
00022         flag_edge=BDR_SUNKENINNER;
00023 }
00024 
00025 CMyMenu::~CMyMenu()
00026 {
00027         DeleteObject(m_hFont);
00028         FreeLibrary( hinst_msimg32 );
00029 }
00030 
00032 // CTitleMenu message handlers
00033 
00034 
00035 HFONT CMyMenu::CreatePopupMenuTitleFont()
00036 {
00037         // start by getting the stock menu font
00038         HFONT hfont = (HFONT)GetStockObject(ANSI_VAR_FONT);
00039         if (hfont) 
00040         { 
00041             LOGFONT lf; //get the complete LOGFONT describing this font
00042             if (GetObject(hfont, sizeof(LOGFONT), &lf)) 
00043                 {
00044                         lf.lfWeight = FW_BOLD;  // set the weight to bold
00045                         // recreate this font with just the weight changed
00046                         return CreateFontIndirect(&lf);
00047                 }
00048         }
00049         return NULL;
00050 }
00051 
00052 
00053 //
00054 // This function adds a title entry to a popup menu
00055 //
00056 void CMyMenu::AddTitle(char *title)
00057 {
00058         // insert an empty owner-draw item at top to serve as the title
00059         // note: item is not selectable (disabled) but not grayed
00060         m_title=title;
00061         InsertMenu(m_hMenu,0, MF_BYPOSITION | MF_OWNERDRAW | MF_STRING | MF_DISABLED, 0,NULL);
00062 }
00063 
00064 
00065 
00066 void CMyMenu::MeasureItem(LPMEASUREITEMSTRUCT mi)
00067 {
00068         // get the screen dc to use for retrieving size information
00069         HDC dc= GetDC(NULL);
00070         // select the title font
00071         HFONT hfontOld = (HFONT)SelectObject(dc,m_hFont);
00072         // compute the size of the title
00073         SIZE size;
00074         GetTextExtentPoint32(dc,m_title,m_title.GetSize(),&size);
00075         // deselect the title font
00076         SelectObject(dc, hfontOld);
00077         // add in the left margin for the menu item
00078         size.cx += GetSystemMetrics(SM_CXMENUCHECK)+8;
00079 
00080 
00081         //Return the width and height
00082         //+ include space for border
00083         const int nBorderSize = 2;
00084         mi->itemWidth = size.cx + nBorderSize;
00085         mi->itemHeight = size.cy + nBorderSize;
00086 }
00087 
00088 
00089 void CMyMenu::DrawItem(LPDRAWITEMSTRUCT di)
00090 {
00091         COLORREF crOldBk = SetBkColor(di->hDC, clLeft);
00092         
00093         if(m_bCanDoGradientFill&&(clLeft!=clRight))
00094         {
00095                 TRIVERTEX rcVertex[2];
00096                 di->rcItem.right--; // exclude this point, like FillRect does 
00097                 di->rcItem.bottom--;
00098                 rcVertex[0].x=di->rcItem.left;
00099                 rcVertex[0].y=di->rcItem.top;
00100                 rcVertex[0].Red=GetRValue(clLeft)<<8;   // color values from 0x0000 to 0xff00 !!!!
00101                 rcVertex[0].Green=GetGValue(clLeft)<<8;
00102                 rcVertex[0].Blue=GetBValue(clLeft)<<8;
00103                 rcVertex[0].Alpha=0x0000;
00104                 rcVertex[1].x=di->rcItem.right; 
00105                 rcVertex[1].y=di->rcItem.bottom;
00106                 rcVertex[1].Red=GetRValue(clRight)<<8;
00107                 rcVertex[1].Green=GetGValue(clRight)<<8;
00108                 rcVertex[1].Blue=GetBValue(clRight)<<8;
00109                 rcVertex[1].Alpha=0;
00110                 GRADIENT_RECT rect;
00111                 rect.UpperLeft=0;
00112                 rect.LowerRight=1;
00113                 
00114                 // fill the area 
00115                 GradientFill( di->hDC,rcVertex,2,&rect,1,GRADIENT_FILL_RECT_H);
00116         }
00117         else ExtTextOut(di->hDC, 0, 0, ETO_OPAQUE, &di->rcItem, NULL, 0, NULL);
00118         if(bDrawEdge) DrawEdge(di->hDC, &di->rcItem, flag_edge, BF_RECT);
00119  
00120 
00121         int modeOld = ::SetBkMode(di->hDC, TRANSPARENT);
00122         COLORREF crOld = ::SetTextColor(di->hDC, clText);
00123         // select font into the dc
00124         HFONT hfontOld = (HFONT)SelectObject(di->hDC, m_hFont);
00125 
00126         // add the menu margin offset
00127         di->rcItem.left += GetSystemMetrics(SM_CXMENUCHECK)+8;
00128 
00129         // draw the text left aligned and vertically centered
00130         DrawText(di->hDC, m_title, -1, &di->rcItem, DT_SINGLELINE|DT_VCENTER|DT_LEFT);
00131 
00132         //Restore font and colors...
00133         SelectObject(di->hDC, hfontOld);
00134         ::SetBkMode(di->hDC, modeOld);
00135         ::SetBkColor(di->hDC, crOldBk);
00136         ::SetTextColor(di->hDC, crOld);
00137 }
00138 
00139 
00140 BOOL CMyMenu::GradientFill(HDC hdc, CONST PTRIVERTEX pVertex, DWORD dwNumVertex, PVOID pMesh, DWORD dwNumMesh, DWORD dwMode)
00141 {
00142         return dllfunc_GradientFill(hdc,pVertex,dwNumVertex,pMesh,dwNumMesh,dwMode); 
00143 }

Generated on Fri Aug 20 19:19:47 2004 for 3d Controls by doxygen 1.3.6