This repository was archived by the owner on Nov 16, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathContextMenu.cpp
More file actions
137 lines (116 loc) · 3.88 KB
/
Copy pathContextMenu.cpp
File metadata and controls
137 lines (116 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ContextMenu.cpp: implementation of the ContextMenu class.
//
//////////////////////////////////////////////////////////////////////
#include "StdBhvTree.h"
//#include "customtreecontrol.h"
#include "ContextMenu.h"
#include "mmsystem.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ContextMenu::~ContextMenu()
{
for( int i = 0; i < m_cptrMenuItems.GetSize(); i++ )
delete (ContextMenuItem*)( m_cptrMenuItems.GetAt( i ) );
m_cptrMenuItems.RemoveAll();
}
/////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
/////////////////////////////////////////////////////////////////////////////
ContextMenu& ContextMenu::AppendMenuItem(UINT nFlags, UINT nID, CString csText, CDC* pDC)
{
ContextMenuItem* cccItem = new ContextMenuItem(csText);
// Store the pointer
m_cptrMenuItems.Add( cccItem );
// Append menu
//CMenu::AppendMenu( nFlags | MF_OWNERDRAW, nID, (ODDCHAR*)cccItem );
CMenu::AppendMenu(nFlags | MF_OWNERDRAW, nID, (LPCTSTR)cccItem);
// Calculate the size of the menu's text
if( !csText.IsEmpty() )
{
CSize cSize = pDC->GetTextExtent( csText );
m_iWidth = max( m_iWidth, cSize.cx );
m_iHeight = max( m_iHeight, 8 + cSize.cy );
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////
// PROTECTED METHODS
/////////////////////////////////////////////////////////////////////////////
void ContextMenu::MeasureItem( LPMEASUREITEMSTRUCT lpMIS )
{
// Separator?
if( GetMenuState( lpMIS->itemID, MF_BYCOMMAND ) & MF_SEPARATOR )
{
lpMIS->itemWidth = m_iWidth;
lpMIS->itemHeight = 6;
}
else
{
lpMIS->itemWidth = m_iWidth;
lpMIS->itemHeight = m_iHeight;
}
}
void ContextMenu::DrawItem( LPDRAWITEMSTRUCT lpDIS )
{
// Get the relevant information
CDC* pDC = CDC::FromHandle( lpDIS->hDC );
CRect rItem = lpDIS->rcItem;
BOOL bSelected = lpDIS->itemState & ODS_SELECTED;
UINT nAction = lpDIS->itemAction;
UINT nState = GetMenuState( lpDIS->itemID, MF_BYCOMMAND );
ContextMenuItem* cccItem = reinterpret_cast<ContextMenuItem*>( lpDIS->itemData );
// Does this menu item need to be drawn?
if( nAction & ODA_SELECT || nAction & ODA_DRAWENTIRE )
{
// Background
pDC->FillSolidRect( rItem, m_crBackground );
// Separator or Text
if( nState & MF_SEPARATOR )
{
rItem.DeflateRect( 4, 2, 4, 2 );
// A thin rectangle that could be anything you want
pDC->FillSolidRect( rItem, m_crBorder );
}
else
{
// Prepare the device context and store previous values
COLORREF crOldColor = pDC->SetTextColor( m_crText );
int iMode = pDC->SetBkMode( TRANSPARENT );
CFont* pOldFont = pDC->SelectObject( m_pFont );
// Is the item disabled?
if( nState & MFS_DISABLED )
{
rItem.DeflateRect( 8, 0, 0, 0 );
pDC->SetTextColor( m_crDisabled );
pDC->DrawText( cccItem->m_csText, rItem, DT_VCENTER | DT_LEFT | DT_SINGLELINE );
}
else
{
// If the item is selected, paint a rectangle, change the background color
// and play the sound file if relevant
if( bSelected )
{
rItem.DeflateRect( 2, 2, 2, 2 );
pDC->Draw3dRect( rItem, m_crBorder, m_crBorder );
rItem.DeflateRect( 1, 1, 1, 1 );
pDC->FillSolidRect( rItem, m_crSelected );
rItem.DeflateRect( 5, -3, 0, -3 );
}
else
rItem.DeflateRect( 8, 0, 0, 0 );
// Last, draw the text on top of everything else
pDC->DrawText( cccItem->m_csText, rItem, DT_VCENTER | DT_LEFT | DT_SINGLELINE );
}
// Clean up
pDC->SelectObject ( pOldFont );
pDC->SetBkMode ( iMode );
pDC->SetTextColor ( crOldColor );
}
}
}