C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[19079] Re:Form의 Font를 변경하고 싶습니다.
유영인.Chris [cuperido] 1798 읽음    2002-06-03 16:26
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를 크게 하고 싶습니다.
: 불가능한건가요?

+ -

관련 글 리스트
19067 [질문]ADOQuery에러가 발생했는데 무슨 뜻인지요 촬리 1459 2002/06/03
19074     불완전한 답변? 김규식 1047 2002/06/03
19079     Re:Form의 Font를 변경하고 싶습니다. 유영인.Chris 1798 2002/06/03
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.