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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| typedef struct tagMYITEM
{
HFONT hfont;
int cchItemText;
char szItemText[256];
} MYITEM;
#define IDM_REGULAR 11
#define IDM_BOLD 12
#define IDM_ITALIC 13
#define IDM_UNDERLINE 14
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
HFONT CreateMenuItemFont(UINT uID)
{
LOGFONT lf;
ZeroMemory(&lf, sizeof(lf));
lf.lfHeight = 20;
lstrcpy(lf.lfFaceName, "CHANLPlain");
switch (uID)
{
case IDM_BOLD:
lf.lfWeight = FW_HEAVY;
break;
case IDM_ITALIC:
lf.lfItalic = TRUE;
break;
case IDM_UNDERLINE:
lf.lfUnderline = TRUE;
break;
}
return CreateFontIndirect(&lf);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::On_MEASUREITEM(TMessage& Msg)
{
// si l'appel est émis pour un menu
if ((UINT) Msg.WParam == 0)
{
MEASUREITEMSTRUCT *lpmis = (MEASUREITEMSTRUCT *) Msg.LParam;
MYITEM *pMyItem = (MYITEM *) lpmis->itemData;
HDC hdc = GetDC(Handle);
HFONT hfntOld = SelectObject(hdc, pMyItem->hfont);
SIZE size;
GetTextExtentPoint32(hdc, pMyItem->szItemText,
pMyItem->cchItemText, &size);
lpmis->itemWidth = size.cx + 5;
lpmis->itemHeight = size.cy;
SelectObject(hdc, hfntOld);
ReleaseDC(Handle, hdc);
}
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::On_DRAWITEM(TMessage& Msg)
{
// si l'appel est émis pour un menu
if ((UINT) Msg.WParam == 0)
{
DRAWITEMSTRUCT *lpdis = (DRAWITEMSTRUCT *) Msg.LParam;
MYITEM *pMyItem = (MYITEM *) lpdis->itemData;
COLORREF clrPrevText, clrPrevBkgnd;
HFONT hfntPrev;
int x, y;
// Set the appropriate foreground and background colors.
if (lpdis->itemState & ODS_SELECTED)
{
clrPrevText = SetTextColor(lpdis->hDC,
GetSysColor(COLOR_HIGHLIGHTTEXT));
clrPrevBkgnd = SetBkColor(lpdis->hDC,
GetSysColor(COLOR_HIGHLIGHT));
}
else
{
clrPrevText = SetTextColor(lpdis->hDC,
GetSysColor(COLOR_MENUTEXT));
clrPrevBkgnd = SetBkColor(lpdis->hDC,
GetSysColor(COLOR_MENU));
}
// Determine where to draw and leave space for a check mark.
x = lpdis->rcItem.left;
y = lpdis->rcItem.top;
x += GetSystemMetrics(SM_CXMENUCHECK);
// Select the font and draw the text.
hfntPrev = SelectObject(lpdis->hDC, pMyItem->hfont);
ExtTextOut(lpdis->hDC, x, y, ETO_OPAQUE,
&lpdis->rcItem, pMyItem->szItemText,
pMyItem->cchItemText, NULL);
// Restore the original font and colors.
SelectObject(lpdis->hDC, hfntPrev);
SetTextColor(lpdis->hDC, clrPrevText);
SetBkColor(lpdis->hDC, clrPrevBkgnd);
}
TForm::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
// Pour faire un travail propre il faut mettre dans FormCreate
// pour pouvoir détruire dans FormDestroy
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HMENU hmenuBar = GetMenu(Handle);
char buf[256];
MENUITEMINFO mii ={ sizeof(mii)};
MYITEM *pMyItem;
int Max = GetMenuItemCount(hmenuBar);
for(int i = 0; i < Max; i++)
{
GetMenuString(hmenuBar, i, buf, 256, MF_BYPOSITION);
pMyItem = (MYITEM *) LocalAlloc(LMEM_FIXED, sizeof(MYITEM));
// Create the font used to draw the item.
pMyItem->hfont = CreateMenuItemFont(IDM_BOLD);
strcpy(pMyItem->szItemText, buf);
pMyItem->cchItemText = strlen(buf);
// Change the item to an owner-drawn item, and save
// the address of the item structure as item data.
mii.fMask = 0x00000020 | 0x00000100;
mii.fType = MFT_OWNERDRAW;
mii.dwItemData = (DWORD) pMyItem;
SetMenuItemInfo(hmenuBar, i, TRUE, &mii);
// On s'intéresse mainteant au sous-menu
HMENU hsm = GetSubMenu(hmenuBar, i);
int Maxs = GetMenuItemCount(hsm);
for(int j = 0; j < Maxs; j++)
{
GetMenuString(hsm, j, buf, 256, MF_BYPOSITION);
pMyItem = (MYITEM *) LocalAlloc(LMEM_FIXED, sizeof(MYITEM));
// Create the font used to draw the item.
pMyItem->hfont = CreateMenuItemFont(IDM_BOLD);
strcpy(pMyItem->szItemText, buf);
pMyItem->cchItemText = strlen(buf);
// Change the item to an owner-drawn item, and save
// the address of the item structure as item data.
mii.fMask = 0x00000020 | 0x00000100;
mii.fType = MFT_OWNERDRAW;
mii.dwItemData = (DWORD) pMyItem;
SetMenuItemInfo(hsm, j, TRUE, &mii);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
HMENU hmenuBar = GetMenu(Handle);
char buf[256];
MENUITEMINFO mii ={ sizeof(mii)};
MYITEM *pMyItem;
int Max = GetMenuItemCount(hmenuBar);
for(int i = 0; i < Max; i++)
{
mii.fMask = 0x00000020 | 0x00000100;
GetMenuItemInfo(hmenuBar, i, TRUE, &mii);
LocalFree((HLOCAL)mii.dwItemData);
// On s'intéresse mainteant au sous-menu
HMENU hsm = GetSubMenu(hmenuBar, i);
int Maxs = GetMenuItemCount(hsm);
for(int j = 0; j < Maxs; j++)
{
mii.fMask = 0x00000020 | 0x00000100;
GetMenuItemInfo(hsm, j, TRUE, &mii);
LocalFree((HLOCAL)mii.dwItemData);
}
}
}
//--------------------------------------------------------------------------- |
Partager