ÀÌ Ç׸ñ¿¡ ¸Þ´º¿¡¼ È®ÀζõÀ» ½Ã¹Ä·¹ÀÌ¼Ç ÇÏ´Â ¹æ¹ýÀ» º¸¿© ÁÖ´Â ¿¹°¡ Æ÷ÇÔ µÇ¾î ÀÖ½À´Ï´Ù. ÀÌ ¿¹Á¦¿¡¼´Â Ç׸ñÀ» »ç¿ëÀÚ°¡ ±½°Ô, ±â¿ïÀÓ²Ã, ¼³Á¤ ÇÏ °í ÇöÀç ±Û²ÃÀÇ Æ¯¼ºÀ» ¹ØÁÙ ¹®ÀÚ ¸Þ´º¸¦ Æ÷ÇÔ ÇÕ´Ï´Ù. ÇØ´ç ¸Þ´º Ç׸ñ; ¿·¿¡ ÀÖ´Â È®Àζõ¿¡ È®ÀΠǥ½Ã°¡ Ç¥½Ã µË´Ï´Ù ±Û²Ã Ư¼ºÀÌ ½ÇÇà ¶§, ±×·¸Áö ¾ÊÀ¸¸é, ºó È®ÀζõÀ» ÇØ´ç Ç׸ñ ¿·¿¡ Ç¥½Ã µË´Ï´Ù.
ÀÌ ¿¹Á¦¿¡¼´Â µÎ °³ÀÇ ºñÆ®¸Ê ±âº» üũ ¸¶Å© ºñÆ®¸Ê ¹Ù²ß´Ï´Ù: üũ »óÀÚ¿Í ºó »óÀÚ ºñÆ®¸Ê ºñÆ®¸Ê. üũ üũ »óÀÚ ºñÆ®¸Ê Ç׸ñÀÇ Ã¼Å© ¸¶Å© Ư¼ºÀ» MF_CHECKED·Î ¼³Á¤ ÇÏ¸é ±½°Ô, ±â¿ïÀӲà ¶Ç´Â ¹ØÁÙ ¸Þ´º Ç׸ñ ¿·¿¡ Ç¥½Ã µË´Ï´Ù. üũ ¸¶Å© Ư¼ºÀ» MF_UNCHECKED·Î ¼³Á¤ µÇÁö ¾ÊÀº ¶Ç´Â ºó È®Àζõ ºñÆ®¸Ê Ç¥½Ã µË´Ï´Ù.
È®Àζõ ¹× ¶óµð¿À ´ÜÃß¿¡ »ç¿ë µÇ´Â À̹ÌÁö°¡ Æ÷ÇÔ µÈ ¹Ì¸® Á¤ÀÇ µÈ ºñÆ®¸ÊÀ» Á¦°ø ÇÏ´Â ½Ã½ºÅÛ. ¿¹Á¦¿¡¼´Â È®ÀÎ ÇÏ °í ºó È®ÀζõÀ» °Ý¸®, µÎ °³ÀÇ º°µµ ºñÆ®¸ÊÀ» º¹»ç ÇÏ °í ¹®ÀÚ ¸Þ´ºÀÇ Ç׸ñ¿¡ ´ë ÇÑ È®ÀÎ ÇÏ °í È®ÀÎ µÇÁö ¾ÊÀº ºñÆ®¸ÊÀ¸·Î »ç¿ë ÇÏ.
¿¹Á¦¿¡¼´Â È£Ãâ ÇÏ´Â ½Ã½ºÅÛ Á¤ÀÇ È®Àζõ ºñÆ®¸Ê¿¡ ´ë ÇÑ ÇÚµéÀ» °Ë»ö ÇÏ´Â LoadBitmap ÇÔ¼ö¸¦ hInstance ¸Å°³ º¯¼ö·Î OBM_CHECKBOXES lpBitmapName ¸Å°³ º¯¼ö·Î NULLÀ» ÁöÁ¤ ÇÕ´Ï´Ù. ºñÆ®¸ÊÀÇ À̹ÌÁö´Â ¸ðµÎ µ¿ÀÏÇÑ Å©±â¸¦ ¶§¹®¿¡ ¿¹Á¦¿¡¼´Â ºñÆ®¸ÊÀÇ ³Êºñ¿Í ³ôÀÌ Çà°ú ¿¿¡ ÀÖ´Â À̹ÌÁöÀÇ ¼ö·Î ³ª´©¾î ±×µé °Ý¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.
¸®¼Ò½º Á¤ÀÇ ÆÄÀÏÀÇ ´ÙÀ½ ºÎºÐ¿¡¼´Â ¹®ÀÚ ¸Þ´º¿¡¼ ¸Þ´º Ç׸ñÀ» Á¤ÀÇ ÇÏ´Â ¹æ¹ýÀ» º¸¿© ÁÝ´Ï´Ù. ¾ø´Â ±Û²Ã Ư¼ºÀº óÀ½ Àû¿ë ÇϹǷΠÀÏ¹Ý Ç׸ñ¿¡ ´ë ÇÑ Ã¼Å© ¸¶Å© Ư¼º üũ ¼³Á¤ µÇ °í ±âº»ÀûÀ¸·Î ³ª¸ÓÁö Ç׸ñÀÇ Ã¼Å© Ç¥½Ã Ư¼ºÀº ¼³Á¤ µÇÁö ¾ÊÀº note.
# include "men3.h" MainMenu ¸Þ´º ½ÃÀÛ Æ˾÷ "¾ÚÇÁ;¹®ÀÚ"½ÃÀÛ MENUITEM"& ÀϹÝ", IDM_REGULAR, üũ MENUITEM SEPARATOR MENUITEM"& ±½°Ô", IDM_BOLD MENUITEM"& ±â¿ïÀÓ²Ã", IDM_ITALIC MENUITEM"& ¹ØÁÙ", IDM_ULINE ³¡ ³¡
ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ Çì´õ ÆÄÀÏÀÇ °ü·Ã ³»¿ëÀ» ¿îÇ×ÇÕ´Ï´Ù.
/ / ¸Þ´º Ç׸ñ ½Äº°ÀÚ # define IDM_REGULAR 0x1 # define IDM_BOLD 0x2 # define IDM_ITALIC 0x4 # define IDM_ULINE 0x8 / / # define üũ ¸¶Å© Ç÷¡±× 1 # üũ Ãë¼Ò 2 Á¤ÀÇ / / ±Û²Ã Ư¼º ¸¶½ºÅ© # ATTRIBMASK 0xe Á¤ÀÇ / / ÇÁ·ÎÅä ŸÀÔ ±â´É LRESULT APIENTRY MainWndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP GetMyCheckBitmaps(UINT);
¹ÙÀÌÆ® CheckOrUncheckMenuItem (¹ÙÀÌÆ®, HMENU)
´ÙÀ½ ¿¹Á¦¿¡¼´Â È®ÀΠǥ½Ã ºñÆ®¸Ê; ¸¸µé â ÇÁ·Î½ÃÀúÀÇ ºÎºÐ ±½°Ô, ±â¿ïÀӲà ¹× ¹ØÁÙ ¸Þ´º Ç׸ñ;ÀÇ Ã¼Å© ¸¶Å© Ư¼º ¼³Á¤ üũ ¸¶Å© ºñÆ®¸Ê Æı«.
LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hbmpCheck; // handle to checked bitmap
static HBITMAP hbmpUncheck; // handle to unchecked bitmap
static HMENU hmenu; // handle to main menu
BYTE fbFontAttrib; // font-attribute flags
switch (uMsg)
{
case WM_CREATE:
// Call the application-defined GetMyCheckBitmaps
// function to get the predefined checked and
// unchecked check box bitmaps.
hbmpCheck = GetMyCheckBitmaps(CHECK);
hbmpUncheck = GetMyCheckBitmaps(UNCHECK);
// Set the checked and unchecked bitmaps for the menu
// items.
hmenu = GetMenu(hwndMain);
SetMenuItemBitmaps(hmenu, IDM_BOLD, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);
SetMenuItemBitmaps(hmenu, IDM_ITALIC, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);
SetMenuItemBitmaps(hmenu, IDM_ULINE, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
// Process the menu commands.
case IDM_REGULAR:
case IDM_BOLD:
case IDM_ITALIC:
case IDM_ULINE:
// CheckOrUncheckMenuItem is an application-
// defined function that sets the menu item
// check marks and returns the user-selected
// font attributes.
fbFontAttrib = CheckOrUncheckMenuItem(
(BYTE) LOWORD(wParam), hmenu);
// Set the font attributes.
return 0;
// Process other command messages.
default:
break;
}
break;
// Process other window messages.
case WM_DESTROY:
// Destroy the checked and unchecked bitmaps.
DeleteObject(hbmpCheck);
DeleteObject(hbmpUncheck);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwndMain, uMsg, wParam, lParam);
}
return NULL;
}
HBITMAP GetMyCheckBitmaps(UINT fuCheck)
{
COLORREF crBackground; // background color
HBRUSH hbrBackground; // background brush
HBRUSH hbrTargetOld; // original background brush
HDC hdcSource; // source device context
HDC hdcTarget; // target device context
HBITMAP hbmpCheckboxes; // handle to check-box bitmap
BITMAP bmCheckbox; // structure for bitmap data
HBITMAP hbmpSourceOld; // handle to original source bitmap
HBITMAP hbmpTargetOld; // handle to original target bitmap
HBITMAP hbmpCheck; // handle to check-mark bitmap
RECT rc; // rectangle for check-box bitmap
WORD wBitmapX; // width of check-mark bitmap
WORD wBitmapY; // height of check-mark bitmap
// Get the menu background color and create a solid brush
// with that color.
crBackground = GetSysColor(COLOR_MENU);
hbrBackground = CreateSolidBrush(crBackground);
// Create memory device contexts for the source and
// destination bitmaps.
hdcSource = CreateCompatibleDC((HDC) NULL);
hdcTarget = CreateCompatibleDC(hdcSource);
// Get the size of the the system default check-mark bitmap and
// create a compatible bitmap of the same size.
wBitmapX = GetSystemMetrics(SM_CXMENUCHECK);
wBitmapY = GetSystemMetrics(SM_CYMENUCHECK);
hbmpCheck = CreateCompatibleBitmap(hdcSource, wBitmapX,
wBitmapY);
// Select the background brush and bitmap into the target DC.
hbrTargetOld = SelectObject(hdcTarget, hbrBackground);
hbmpTargetOld = SelectObject(hdcTarget, hbmpCheck);
// Use the selected brush to initialize the background color
// of the bitmap in the target device context.
PatBlt(hdcTarget, 0, 0, wBitmapX, wBitmapY, PATCOPY);
// Load the predefined check box bitmaps and select it
// into the source DC.
hbmpCheckboxes = LoadBitmap((HINSTANCE) NULL,
(LPTSTR) OBM_CHECKBOXES);
hbmpSourceOld = SelectObject(hdcSource, hbmpCheckboxes);
// Fill a BITMAP structure with information about the
// check box bitmaps, and then find the upper-left corner of
// the unchecked check box or the checked check box.
GetObject(hbmpCheckboxes, sizeof(BITMAP), &bmCheckbox);
if (fuCheck == UNCHECK)
{
rc.left = 0;
rc.right = (bmCheckbox.bmWidth / 4);
}
else
{
rc.left = (bmCheckbox.bmWidth / 4);
rc.right = (bmCheckbox.bmWidth / 4) * 2;
}
rc.top = 0;
rc.bottom = (bmCheckbox.bmHeight / 3);
// Copy the appropriate bitmap into the target DC. If the
// check-box bitmap is larger than the default check-mark
// bitmap, use StretchBlt to make it fit; otherwise, just
// copy it.
if (((rc.right - rc.left) > (int) wBitmapX) ||
((rc.bottom - rc.top) > (int) wBitmapY))
{
StretchBlt(hdcTarget, 0, 0, wBitmapX, wBitmapY,
hdcSource, rc.left, rc.top, rc.right - rc.left,
rc.bottom - rc.top, SRCCOPY);
}
else
{
BitBlt(hdcTarget, 0, 0, rc.right - rc.left,
rc.bottom - rc.top,
hdcSource, rc.left, rc.top, SRCCOPY);
}
// Select the old source and destination bitmaps into the
// source and destination DCs, and then delete the DCs and
// the background brush.
SelectObject(hdcSource, hbmpSourceOld);
SelectObject(hdcTarget, hbrTargetOld);
hbmpCheck = SelectObject(hdcTarget, hbmpTargetOld);
DeleteObject(hbrBackground);
DeleteObject(hdcSource);
DeleteObject(hdcTarget);
// Return the handle to the new check-mark bitmap.
return hbmpCheck;
}
BYTE CheckOrUncheckMenuItem(BYTE bMenuItemID, HMENU hmenu)
{
DWORD fdwMenu;
static BYTE fbAttributes;
switch (bMenuItemID)
{
case IDM_REGULAR:
// Whenever the Regular menu item is selected, add a
// check mark to it and then remove check marks from
// any font-attribute menu items.
CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |
MF_CHECKED);
if (fbAttributes & ATTRIBMASK)
{
CheckMenuItem(hmenu, IDM_BOLD, MF_BYCOMMAND |
MF_UNCHECKED);
CheckMenuItem(hmenu, IDM_ITALIC, MF_BYCOMMAND |
MF_UNCHECKED);
CheckMenuItem(hmenu, IDM_ULINE, MF_BYCOMMAND |
MF_UNCHECKED);
}
fbAttributes = IDM_REGULAR;
return fbAttributes;
case IDM_BOLD:
case IDM_ITALIC:
case IDM_ULINE:
// Toggle the check mark for the selected menu item and
// set the font attribute flags appropriately.
fdwMenu = GetMenuState(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND);
if (!(fdwMenu & MF_CHECKED))
{
CheckMenuItem(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND | MF_CHECKED);
fbAttributes |= bMenuItemID;
}
else
{
CheckMenuItem(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND | MF_UNCHECKED);
fbAttributes ^= bMenuItemID;
}
// If any font attributes are currently selected,
// remove the check mark from the Regular menu item;
// if no attributes are selected, add a check mark
// to the Regular menu item.
if (fbAttributes & ATTRIBMASK)
{
CheckMenuItem(hmenu, IDM_REGULAR,
MF_BYCOMMAND | MF_UNCHECKED);
fbAttributes &= (BYTE) ~IDM_REGULAR;
}
else
{
CheckMenuItem(hmenu, IDM_REGULAR,
MF_BYCOMMAND | MF_CHECKED);
fbAttributes = IDM_REGULAR;
}
return fbAttributes;
}
}