|
WM_DRAWITEM, WM_MEASUREITEM 메세지를 직접 받아서 처리하시면 가능합니다.
아래에 예제 코드가 잘 나와있네요.
You have to make the Menu owner-drawn. If you have BCB4, then this is
encapsulted for you. If you have an earlier version, then you have to use the
API or a third-party component (there are a lot of them out there). I
demonstrate the API method below, but first, let me tell you what's going on.
First, we iterate over all the items, flagging the owner-draw state for each
item. Once the items are owner-drawn, they'll send our Form two messages --
WM_MEASUREITEM and WM_DRAWITEM. We create a message handler for the first
message, and tell Windows the appropriate sizes of the menus. In the message
handler for the WM_DRAWITEM message, we draw the menu items in the new font...
// in header...
void __fastcall WMDraw(TMessage &Msg);
void __fastcall WMMeasure(TMessage &Msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DRAWITEM, TMessage, WMDraw)
MESSAGE_HANDLER(WM_MEASUREITEM, TMessage, WMMeasure)
END_MESSAGE_MAP(TForm)
//in source...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TMenuItemInfo ItemInfo;
// flag the owner-draw state for all items
// this only goes two levels deep, so you'll
// need to modify further for additional submenus
ItemInfo.cbSize = sizeof(TMenuItemInfo);
ItemInfo.fMask = MIIM_TYPE;
for (int index = 0; index < Menu->Items->Count; index++)
{
GetMenuItemInfo(Menu->Handle, index, true, &ItemInfo);
ItemInfo.fType = ItemInfo.fType | MFT_OWNERDRAW;
ItemInfo.dwTypeData = Menu->Items->Items[index]->Caption.c_str();
ItemInfo.cch = Menu->Items->Items[index]->Caption.Length() + 1;
SetMenuItemInfo(Menu->Handle, index, true, &ItemInfo);
for (int index2 = 0;
index2 < Menu->Items->Items[index]->Count;
index2++)
{
GetMenuItemInfo(Menu->Items->Items[index]->Handle,
index2, true, &ItemInfo);
ItemInfo.fType = ItemInfo.fType | MFT_OWNERDRAW;
ItemInfo.dwTypeData =
Menu->Items->Items[index]->Items[index2]->Caption.c_str();
ItemInfo.cch =
Menu->Items->Items[index]->Items[index2]->Caption.Length();
SetMenuItemInfo(Menu->Items->Items[index]->Handle,
index2, true, &ItemInfo);
}
}
}
// This is the function that the WM_MESURETEM
// message maps to. The LParam member is in the
// form of a MEASUREITEMSTRUCT structure
// This is where you tell Windows how big you want
// your menu items to be
void __fastcall TForm1::WMMeasure(TMessage &Msg)
{
LPMEASUREITEMSTRUCT lpmis = (MEASUREITEMSTRUCT *)Msg.LParam;
// use the screen's device context to calculate the
// appropriate width and height of the menu
int Index = lpmis->itemID;
char text[100];
GetMenuString(Menu->Handle, Index, text,
100, MF_BYCOMMAND);
TFont *MenuFont = new TFont();
MenuFont->Name = "Tahoma";
MenuFont->Size = 10;
MenuFont->Style = TFontStyles() << fsBold;
SIZE S;
HDC HMenuDC = GetDC(0);
HFONT OldFont = SelectObject(HMenuDC, MenuFont->Handle);
// get the appropriate "extents"
GetTextExtentPoint32(HMenuDC, text, strlen(text), &S);
ReleaseDC(0, HMenuDC);
// set the sizes appropriately
lpmis->itemWidth = S.cx;
lpmis->itemHeight = S.cy;
SelectObject(HMenuDC, OldFont);
Msg.Result = true;
}
// This is the function that the WM_DRAWITEM
// message maps to. The LParam member is in the
// form of a DRAWITEMSTRUCT structure. This is
// where you draw the menu items in your new font.
void __fastcall TForm1::WMDraw(TMessage &Msg)
{
LPDRAWITEMSTRUCT lpdis = (DRAWITEMSTRUCT *)Msg.LParam;
/* the WM_DRAWITEM may be shared (for example, BitBtns) */
/* handle the drawing if the message corresponds */
/* the target ListView */
if (lpdis->CtlType != ODT_MENU)
{
TForm::Dispatch(&Msg);
return;
}
int Index = lpdis->itemID;
UINT ItemState = lpdis->itemState;
HDC HMenuDC = lpdis->hDC;
RECT R = lpdis->rcItem;
// get the menu caption from the Index
char text[100];
GetMenuString(lpdis->hwndItem, Index, text,
100, MF_BYCOMMAND);
// this is your new font
/***********************************************/
TFont *MenuFont = new TFont();
MenuFont->Name = "Tahoma";
MenuFont->Size = 10;
MenuFont->Style = TFontStyles() << fsBold;
/***********************************************/
TBrush *MenuBrush = new TBrush();
if (ItemState & ODS_SELECTED)
{
MenuBrush->Color = clHighlight;
::SetTextColor(HMenuDC, ColorToRGB(clHighlightText));
}
else
{
MenuBrush->Color = clBtnFace;
::SetTextColor(HMenuDC, ColorToRGB(clWindowText));
}
HFONT OldFont = SelectObject(HMenuDC, MenuFont->Handle);
::SetBkMode(HMenuDC, TRANSPARENT);
::FillRect(HMenuDC, &R, MenuBrush->Handle);
::OffsetRect(&R, 2, 0);
::DrawText(HMenuDC, text, strlen(text), &R, DT_LEFT |
DT_VCENTER | DT_SINGLELINE);
SelectObject(HMenuDC, OldFont);
delete MenuFont;
delete MenuBrush;
Msg.Result = true;
}
Good luck.
--------------------------------------
Damon Chandler
알베르또 님이 쓰신 글 :
: 메뉴의 Font를 크게 하고 싶습니다.
: 불가능한건가요?
|