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);
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
00033
00034
00035 HFONT CMyMenu::CreatePopupMenuTitleFont()
00036 {
00037
00038 HFONT hfont = (HFONT)GetStockObject(ANSI_VAR_FONT);
00039 if (hfont)
00040 {
00041 LOGFONT lf;
00042 if (GetObject(hfont, sizeof(LOGFONT), &lf))
00043 {
00044 lf.lfWeight = FW_BOLD;
00045
00046 return CreateFontIndirect(&lf);
00047 }
00048 }
00049 return NULL;
00050 }
00051
00052
00053
00054
00055
00056 void CMyMenu::AddTitle(char *title)
00057 {
00058
00059
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
00069 HDC dc= GetDC(NULL);
00070
00071 HFONT hfontOld = (HFONT)SelectObject(dc,m_hFont);
00072
00073 SIZE size;
00074 GetTextExtentPoint32(dc,m_title,m_title.GetSize(),&size);
00075
00076 SelectObject(dc, hfontOld);
00077
00078 size.cx += GetSystemMetrics(SM_CXMENUCHECK)+8;
00079
00080
00081
00082
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--;
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;
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
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
00124 HFONT hfontOld = (HFONT)SelectObject(di->hDC, m_hFont);
00125
00126
00127 di->rcItem.left += GetSystemMetrics(SM_CXMENUCHECK)+8;
00128
00129
00130 DrawText(di->hDC, m_title, -1, &di->rcItem, DT_SINGLELINE|DT_VCENTER|DT_LEFT);
00131
00132
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 }