ÀÌ Ç׸ñ¿¡´Â ¼ÒÀ¯ÀÚ°¡ ±×¸° ¸Þ´º Ç׸ñÀÌ ¸Þ´º¿¡ »ç¿ë ÇÏ´Â ÀÀ¿ë ÇÁ·Î±×·¥¿¡¼ ¿¹°¡ Æ÷ÇÔ µÇ¾î ÀÖ½À´Ï´Ù. ¸Þ´º´Â ÇöÀç ±Û²ÃÀÇ Æ¯¼ºÀ» ¼³Á¤ ÇÏ´Â Ç׸ñÀ» Æ÷ÇÔ ÇÏ °í ÀûÀý ÇÑ ±Û²Ã Ư¼ºÀ» »ç¿ë ÇÏ ¿© Ç׸ñ Ç¥½Ã.
¿©±â ¾î¶»°Ô ¸Þ´º ¸®¼Ò½º Á¤ÀÇ ÆÄÀÏ¿¡ Á¤ÀÇ µÇ¾î ÀÖ´Ù. ÀϹÝ, ±½°Ô, ±â¿ïÀӲà ¹× ¹ØÁÙ ¸Þ´º Ç׸ñ¿¡ ´ë ÇÑ ¹®ÀÚ¿ ¸®¼Ò½º Á¤ÀÇ ÆÄÀÏ¿¡ ±×µéÀÇ ¹®ÀÚ¿Àº ºñ¾î ÀÖÀ¸¹Ç·Î ·±Å¸ÀÓ¿¡ ÇÒ´ç µË´Ï´Ù note.
MainMenu ¸Þ´º ½ÃÀÛ ÆË¾÷ "¾ÚÇÁ;¹®ÀÚ"½ÃÀÛ MENUITEM" ", IDM_REGUL&AR MENUITEM SEPARATOR MENUITEM" ", IDM_BOLD MENUITEM" ", IDM_ITALIC MENUITEM" ", IDM_ULINE ³¡ ³¡
¼ÒÀ¯ÀÚ°¡ ±×¸° ¸Þ´º Ç׸ñÀ» »ç¿ë ÇÏ ¿© °ü·Ã µÈ ¸Þ½ÃÁö¸¦ ó¸® ÇÏ´Â ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ Ã¢ ÇÁ·Î½ÃÀú. ÀÀ¿ë ÇÁ·Î±×·¥ WM_CREATE ¸Þ½ÃÁö¸¦ »ç¿ë ÇÏ ¿© ´ÙÀ½ ÀÛ¾÷À» ¼öÇàÇÕ´Ï´Ù:
ÅØ½ºÆ® ¹®ÀÚ¿°ú ±Û²Ã ÇÚµéÀº ÀÀ¿ë ÇÁ·Î±×·¥ Á¤ÀÇ MYITEM ±¸Á¶Ã¼ÀÇ ¹è¿¿¡ ÀúÀå µË´Ï´Ù. ÀÀ¿ë ÇÁ·Î±×·¥ Á¤ÀÇ GetAFont ÇÔ¼ö´Â ÁÖ¾îÁøµÈ ±Û²Ã Ư¼º¿¡ ÇØ´ç ÇÏ´Â ±Û²Ã ÇÚµéÀ» ¹Ýȯ ÇÕ´Ï´Ù. ±Û²ÃÀ» ¸¸µì´Ï´Ù. WM_DESTROY ¸Þ½ÃÁö¸¦ ó¸® ÇÏ´Â µ¿¾È ÇÚµéÀ» ÆÄ±« ÇÏ´Â.
WM_MEASUREITEM ¸Þ½ÃÁö¸¦ ó¸® ÇÏ´Â µ¿¾È ¿¹Á¦¿¡¼´Â ³Êºñ¿Í ³ôÀÌ ¸Þ´º Ç׸ñ ¹®ÀÚ¿ÀÇ °¡Á®¿À°í MEASUREITEMSTRUCT ±¸Á¶¿¡ ÀÌ·¯ÇÑ °ªÀ» º¹»ç ÇÕ´Ï´Ù. ¸Þ´ºÀÇ Å©±â¸¦ °è»ê ÇÏ·Á¸é ³Êºñ¿Í ³ôÀÌ °ªÀ» »ç¿ë ÇÏ´Â ½Ã½ºÅÛ.
WM_DRAWITEM ¸Þ½ÃÁö¸¦ ó¸® ÇÏ´Â µ¿¾È ¸Þ´º Ç׸ñ ¹®ÀÚ¿Àº ¹æ ¿·¿¡ È®ÀΠǥ½Ã ºñÆ®¸Ê¿¡ ´ë ÇÑ ¹®ÀÚ¿ ¿ÞÂÊÀ¸·Î ±×·ÁÁý´Ï´Ù. ¼±ÅÃÇÑ ÅØ½ºÆ® ¹× ¹è°æ »ö»ó Ç׸ñÀ» ±×¸®´Â µ¥ »ç¿ë µÇ´Â »ç¿ëÀÚ°¡ Ç׸ñÀ» ¼±Åà ÇÏ´Â °æ¿ì.
LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
typedef struct _MYITEM
{
HFONT hfont;
LPSTR psz;
} MYITEM; // structure for item font and string
MYITEM *pmyitem; // pointer to item's font and string
static MYITEM myitem[CITEMS]; // array of MYITEMS
static HMENU hmenu; // handle to main menu
static COLORREF crSelText; // text color of selected item
static COLORREF crSelBkgnd; // background color of selected item
COLORREF crText; // text color of unselected item
COLORREF crBkgnd; // background color unselected item
LPMEASUREITEMSTRUCT lpmis; // pointer to item of data
LPDRAWITEMSTRUCT lpdis; // pointer to item drawing data
HDC hdc; // handle to screen DC
SIZE size; // menu-item text extents
WORD wCheckX; // check-mark width
int nTextX; // width of menu item
int nTextY; // height of menu item
int i; // loop counter
HFONT hfontOld; // handle to old font
BOOL fSelected = FALSE; // menu-item selection flag
switch (uMsg)
{
case WM_CREATE:
// Modify the Regular, Bold, Italic, and Underline
// menu items to make them owner-drawn items. Associate
// a MYITEM structure with each item to contain the
// string and font handle for each item.
hmenu = GetMenu(hwnd);
ModifyMenu(hmenu, IDM_REGULAR, MF_BYCOMMAND |
MF_CHECKED | MF_OWNERDRAW, IDM_REGULAR,
(LPTSTR) &myitem[REGULAR]);
ModifyMenu(hmenu, IDM_BOLD, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_BOLD, (LPTSTR) &myitem[BOLD]);
ModifyMenu(hmenu, IDM_ITALIC, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_ITALIC,
(LPTSTR) &myitem[ITALIC]);
ModifyMenu(hmenu, IDM_ULINE, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_ULINE, (LPTSTR) &myitem[ULINE]);
// Retrieve each item's font handle and copy it into
// the hfont member of each item's MYITEM structure.
// Also, copy each item's string into the structures.
myitem[REGULAR].hfont = GetAFont(REGULAR);
myitem[REGULAR].psz = "Regular";
myitem[BOLD].hfont = GetAFont(BOLD);
myitem[BOLD].psz = "Bold";
myitem[ITALIC].hfont = GetAFont(ITALIC);
myitem[ITALIC].psz = "Italic";
myitem[ULINE].hfont = GetAFont(ULINE);
myitem[ULINE].psz = "Underline";
// Retrieve the text and background colors of the
// selected menu text.
crSelText = GetSysColor(COLOR_HIGHLIGHTTEXT);
crSelBkgnd = GetSysColor(COLOR_HIGHLIGHT);
return 0;
case WM_MEASUREITEM:
// Retrieve a device context for the main window.
hdc = GetDC(hwnd);
// Retrieve pointers to the menu item's
// MEASUREITEMSTRUCT structure and MYITEM structure.
lpmis = (LPMEASUREITEMSTRUCT) lParam;
pmyitem = (MYITEM *) lpmis->itemData;
// Select the font associated with the item into
// the main window's device context.
hfontOld = SelectObject(hdc, pmyitem->hfont);
// Retrieve the width and height of the item's string,
// and then copy the width and height into the
// MEASUREITEMSTRUCT structure's itemWidth and
// itemHeight members.
GetTextExtentPoint32(hdc, pmyitem->psz,
lstrlen(pmyitem->psz), &size);
lpmis->itemWidth = size.cx;
lpmis->itemHeight = size.cy;
// Select the old font back into the device context,
// and then release the device context.
SelectObject(hdc, hfontOld);
ReleaseDC(hwnd, hdc);
return TRUE;
break;
case WM_DRAWITEM:
// Get pointers to the menu item's DRAWITEMSTRUCT
// structure and MYITEM structure.
lpdis = (LPDRAWITEMSTRUCT) lParam;
pmyitem = (MYITEM *) lpdis->itemData;
// If the user has selected the item, use the selected
// text and background colors to display the item.
if (lpdis->itemState & ODS_SELECTED)
{
crText = SetTextColor(lpdis->hDC, crSelText);
crBkgnd = SetBkColor(lpdis->hDC, crSelBkgnd);
fSelected = TRUE;
}
// Remember to leave space in the menu item for the
// check-mark bitmap. Retrieve the width of the bitmap
// and add it to the width of the menu item.
wCheckX = GetSystemMetrics(SM_CXMENUCHECK);
nTextX = wCheckX + lpdis->rcItem.left;
nTextY = lpdis->rcItem.top;
// Select the font associated with the item into the
// item's device context, and then draw the string.
hfontOld = SelectObject(lpdis->hDC, pmyitem->hfont);
ExtTextOut(lpdis->hDC, nTextX, nTextY, ETO_OPAQUE,
&lpdis->rcItem, pmyitem->psz,
lstrlen(pmyitem->psz), NULL);
// Select the previous font back into the device
// context.
SelectObject(lpdis->hDC, hfontOld);
// Return the text and background colors to their
// normal state (not selected).
if (fSelected)
{
SetTextColor(lpdis->hDC, crText);
SetBkColor(lpdis->hDC, crBkgnd);
}
return TRUE;
// Process other messages.
case WM_DESTROY:
// Destroy the menu items' font handles.
for (i = 0; i < CITEMS; i++)
DeleteObject(myitem[i].hfont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return NULL;
}
HFONT GetAFont(int fnFont)
{
static LOGFONT lf; // structure for font information
// Get a handle to the ANSI fixed-pitch font, and copy
// information about the font to a LOGFONT structure.
GetObject(GetStockObject(ANSI_FIXED_FONT), sizeof(LOGFONT),
&lf);
// Set the font attributes, as appropriate.
if (fnFont == BOLD)
lf.lfWeight = FW_BOLD;
else
lf.lfWeight = FW_NORMAL;
lf.lfItalic = (fnFont == ITALIC);
lf.lfItalic = (fnFont == ULINE);
// Create the font, and then return its handle.
return CreateFont(lf.lfHeight, lf.lfWidth,
lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
lf.lfPitchAndFamily, lf.lfFaceName);
}