1 // TMenu.cpp : implementation file
10 static char THIS_FILE
[] = __FILE__
;
13 /////////////////////////////////////////////////////////////////////////////
18 HFONT hfont
= CreatePopupMenuTitleFont();
25 m_Font
.DeleteObject();
31 /////////////////////////////////////////////////////////////////////////////
32 // CTMenu message handlers
35 HFONT
CTMenu::CreatePopupMenuTitleFont()
37 // start by getting the stock menu font
38 HFONT hfont
= (HFONT
)GetStockObject(ANSI_VAR_FONT
);
41 LOGFONT lf
; //get the complete LOGFONT describing this font
42 if (GetObject(hfont
, sizeof(LOGFONT
), &lf
))
44 lf
.lfWeight
= FW_BOLD
; // set the weight to bold
45 // recreate this font with just the weight changed
46 return ::CreateFontIndirect(&lf
);
54 // This function adds a title entry to a popup menu
56 void CTMenu::AddMenuTitle(LPCTSTR lpszTitle
)
58 // insert an empty owner-draw item at top to serve as the title
59 // note: item is not selectable (disabled) but not grayed
60 m_strTitle
=CString(lpszTitle
);
61 InsertMenu(0, MF_BYPOSITION
| MF_OWNERDRAW
| MF_STRING
| MF_DISABLED
, 0);
66 void CTMenu::MeasureItem(LPMEASUREITEMSTRUCT mi
)
68 // get the screen dc to use for retrieving size information
70 dc
.Attach(::GetDC(NULL
));
71 // select the title font
72 HFONT hfontOld
= (HFONT
)SelectObject(dc
.m_hDC
, (HFONT
)m_Font
);
73 // compute the size of the title
74 CSize size
= dc
.GetTextExtent(m_strTitle
);
75 // deselect the title font
76 ::SelectObject(dc
.m_hDC
, hfontOld
);
77 // add in the left margin for the menu item
78 size
.cx
+= GetSystemMetrics(SM_CXMENUCHECK
)+8;
80 //Return the width and height
81 //+ include space for border
82 const int nBorderSize
= 2;
83 mi
->itemWidth
= size
.cx
+ nBorderSize
;
84 mi
->itemHeight
= size
.cy
+ nBorderSize
;
88 void CTMenu::DrawItem(LPDRAWITEMSTRUCT di
)
90 //Draw the background and a sunken rect...
91 COLORREF crOldBk
= ::SetBkColor(di
->hDC
, ::GetSysColor(COLOR_INACTIVECAPTION
));
92 ::ExtTextOut(di
->hDC
, 0, 0, ETO_OPAQUE
, &di
->rcItem
, NULL
, 0, NULL
);
93 ::DrawEdge(di
->hDC
, &di
->rcItem
, BDR_SUNKENINNER
, BF_RECT
);
95 int modeOld
= ::SetBkMode(di
->hDC
, TRANSPARENT
);
96 COLORREF crOld
= ::SetTextColor(di
->hDC
, GetSysColor(COLOR_CAPTIONTEXT
));
97 // select font into the dc
98 HFONT hfontOld
= (HFONT
)SelectObject(di
->hDC
, (HFONT
)m_Font
);
100 // add the menu margin offset
101 di
->rcItem
.left
+= GetSystemMetrics(SM_CXMENUCHECK
)+8;
102 // draw the text left aligned and vertically centered
103 ::DrawText(di
->hDC
, m_strTitle
, -1, &di
->rcItem
, DT_SINGLELINE
|DT_VCENTER
|DT_LEFT
);
105 //Restore font and colors...
106 ::SelectObject(di
->hDC
, hfontOld
);
107 ::SetBkMode(di
->hDC
, modeOld
);
108 ::SetBkColor(di
->hDC
, crOldBk
);
109 ::SetTextColor(di
->hDC
, crOld
);