Windows/MFC
CMenu Inner Image (메뉴안에 이미지 넣기)
aucd29
2013. 10. 2. 18:04
1. Create a menu item in the view's menu resource and mainframe's menu resource with a unique ID (let's say ID_HARIKRISHNA). Uncheck the Popup menu check box.
2. Now, open the resource file (yourproject.rc file), go to the menu resource block, and add following flags for the above menu item.
Ex. For the menuitem named "harikrishna"
MENUITEM "harikrishna", ID_HARIKRISHNA, MFT_STRING | MFT_OWNERDRAW | MFT_RIGHTJUSTIFY,MFS_ENABLED
3. Now, override the OnMeasureItem CMainfrm class.
4. Add the following code in OnMeasureItem:
[code]
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (ID_LOGO == lpMeasureItemStruct->itemID &&
ODT_MENU == lpMeasureItemStruct->CtlType)
{
CBitmap bitmap;
bitmap.LoadBitmap(IDB_LOGO);
CSize size = bitmap.GetBitmapDimension();
BITMAP bm;
GetObject(bitmap.GetSafeHandle(), sizeof(bm), &bm);
lpMeasureItemStruct->itemWidth = bm.bmWidth;
lpMeasureItemStruct->itemHeight = bm.bmHeight;
}
else
CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
[/code]
5. Add the following code in the OnDrawItem method
[code]
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (ID_LOGO == lpDrawItemStruct->itemID && ODT_MENU == lpDrawItemStruct->CtlType)
{
CBitmap bitmap;
bitmap.LoadBitmap(IDB_LOGO);
HDC hTempDC = ::CreateCompatibleDC(lpDrawItemStruct->hDC);
::SelectObject(hTempDC, bitmap.GetSafeHandle());
BitBlt(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.top,
lpDrawItemStruct->rcItem.right, lpDrawItemStruct->rcItem.bottom, hTempDC, 0, 0,
SRCCOPY);
}
else
{
CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
}
[/code]
That's it. You are done.
[code]
1. 프로젝트 하나 만들고
2. 메뉴에 LOGO 라고 적고 ID_LOGO는 자동 할당이 되고 옵션을 주는데 MFT_OWNERDRAW 는 rc 파일을 열어서 직접 타이핑 하는 방법 밖에 없다.
3. MainFrm에서 WM_DRAWITEM, WM_MEASUREITEM 을 추가하고 코드를 붙여넣기한다.. 이러면 끝...
ps. lpMeasureItemStruct->itemWidth = bm.bmWidth - 14; 식으로 -를 해주면 완전히 오른쪽에 붙게 된다.
ps. 만일 메뉴내에 이미지를 refresh 해야 한다면은 DrawMenuBar(); 를 이용하면 된다.
http://www.codeguru.com/forum/showthread.php?p=1415344#post1415344
[/code]
Link : http://www.codeguru.com/cpp/controls/menu/bitmappedmenus/article.php/c3725/
2. Now, open the resource file (yourproject.rc file), go to the menu resource block, and add following flags for the above menu item.
Ex. For the menuitem named "harikrishna"
MENUITEM "harikrishna", ID_HARIKRISHNA, MFT_STRING | MFT_OWNERDRAW | MFT_RIGHTJUSTIFY,MFS_ENABLED
3. Now, override the OnMeasureItem CMainfrm class.
4. Add the following code in OnMeasureItem:
[code]
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (ID_LOGO == lpMeasureItemStruct->itemID &&
ODT_MENU == lpMeasureItemStruct->CtlType)
{
CBitmap bitmap;
bitmap.LoadBitmap(IDB_LOGO);
CSize size = bitmap.GetBitmapDimension();
BITMAP bm;
GetObject(bitmap.GetSafeHandle(), sizeof(bm), &bm);
lpMeasureItemStruct->itemWidth = bm.bmWidth;
lpMeasureItemStruct->itemHeight = bm.bmHeight;
}
else
CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
[/code]
5. Add the following code in the OnDrawItem method
[code]
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (ID_LOGO == lpDrawItemStruct->itemID && ODT_MENU == lpDrawItemStruct->CtlType)
{
CBitmap bitmap;
bitmap.LoadBitmap(IDB_LOGO);
HDC hTempDC = ::CreateCompatibleDC(lpDrawItemStruct->hDC);
::SelectObject(hTempDC, bitmap.GetSafeHandle());
BitBlt(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.top,
lpDrawItemStruct->rcItem.right, lpDrawItemStruct->rcItem.bottom, hTempDC, 0, 0,
SRCCOPY);
}
else
{
CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
}
[/code]
That's it. You are done.
[code]
1. 프로젝트 하나 만들고
2. 메뉴에 LOGO 라고 적고 ID_LOGO는 자동 할당이 되고 옵션을 주는데 MFT_OWNERDRAW 는 rc 파일을 열어서 직접 타이핑 하는 방법 밖에 없다.
3. MainFrm에서 WM_DRAWITEM, WM_MEASUREITEM 을 추가하고 코드를 붙여넣기한다.. 이러면 끝...
ps. lpMeasureItemStruct->itemWidth = bm.bmWidth - 14; 식으로 -를 해주면 완전히 오른쪽에 붙게 된다.
ps. 만일 메뉴내에 이미지를 refresh 해야 한다면은 DrawMenuBar(); 를 이용하면 된다.
http://www.codeguru.com/forum/showthread.php?p=1415344#post1415344
[/code]
Link : http://www.codeguru.com/cpp/controls/menu/bitmappedmenus/article.php/c3725/