📄 rebar.c
字号:
// Create rebar control.
dwStyle = WS_VISIBLE | WS_BORDER | WS_CHILD | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS | RBS_VARHEIGHT | RBS_BANDBORDERS |
CCS_NODIVIDER | CCS_NOPARENTALIGN;
if (!(hwndRB = CreateWindowEx (0,
REBARCLASSNAME,
NULL,
dwStyle,
0,
0,
CW_USEDEFAULT,
100,
hwnd,
(HMENU)ID_REBAR,
g_hInst,
NULL)))
{
return NULL;
}
// Set the characteristics of the rebar control.
himlRB = ImageList_Create (32, 32, ILC_COLORDDB | ILC_MASK, 1, 0);
hIcon = LoadIcon (g_hInst, MAKEINTRESOURCE (IDI_REBAR));
ImageList_AddIcon (himlRB, hIcon);
rbi.cbSize = sizeof (rbi);
rbi.fMask = RBIM_IMAGELIST;
rbi.himl = himlRB;
if (!SendMessage (hwndRB, RB_SETBARINFO, 0, (LPARAM)&rbi))
return NULL;
// Create a toolbar.
dwStyle = WS_VISIBLE | WS_CHILD | TBSTYLE_TOOLTIPS |
CCS_NOPARENTALIGN | CCS_NORESIZE;
if (!(hwndTB = CreateToolbarEx (hwnd,
dwStyle,
(UINT) ID_TOOLBAR,
NUMIMAGES,
g_hInst,
IDB_TOOLBAR,
tbButton,
sizeof (tbButton) / sizeof (TBBUTTON),
BUTTONWIDTH,
BUTTONHEIGHT,
IMAGEWIDTH,
IMAGEHEIGHT,
sizeof (TBBUTTON))))
{
return NULL;
}
// Add ToolTips to the toolbar.
SendMessage (hwndTB, TB_SETTOOLTIPS, (WPARAM) NUMIMAGES,
(LPARAM) szToolTips);
// Retrieve the dimensions of the bounding rectangle of the toolbar.
GetWindowRect (hwndTB, &rect);
memset (&rbbi[0], 0, sizeof (rbbi[0]));
rbbi[0].cbSize = sizeof (REBARBANDINFO);
rbbi[0].fMask = RBBIM_SIZE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_ID
| RBBIM_STYLE | RBBIM_TEXT | RBBIM_BACKGROUND | 0;
rbbi[0].cxMinChild = rect.right - rect.left + 2;
rbbi[0].cyMinChild = rect.bottom - rect.top + 2;
rbbi[0].cx = 250;
rbbi[0].fStyle = RBBS_BREAK | RBBS_GRIPPERALWAYS;
rbbi[0].wID = ID_TOOLBAR;
rbbi[0].hwndChild = hwndTB;
rbbi[0].lpText = TEXT("Toolbar");
rbbi[0].hbmBack = LoadBitmap (g_hInst, MAKEINTRESOURCE (IDB_BKGRD));
// Insert the toolbar band in the rebar control.
SendMessage (hwndRB, RB_INSERTBAND, (WPARAM)-1,
(LPARAM) (LPREBARBANDINFO)&rbbi[0]);
// Create a combo box.
dwStyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL |
WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
CBS_AUTOHSCROLL | CBS_DROPDOWN;
if (!(hwndCombo = CreateWindowEx (0,
TEXT("combobox"),
NULL,
dwStyle,
0, 0, 100, 200,
hwndRB,
(HMENU)ID_COMBOBOX,
g_hInst,
NULL)))
{
return NULL;
}
// Add 10 items to the combo box.
for (index = 0; index < 10; index++)
{
wsprintf (szString, TEXT("Item %d"), index + 1);
SendMessage (hwndCombo, CB_ADDSTRING, 0, (LPARAM) szString);
}
// Select the first item as default.
SendMessage (hwndCombo, CB_SETCURSEL, (WPARAM)0, 0);
// Retrieve the dimensions of the bounding rectangle of the combo box.
GetWindowRect (hwndCombo, &rect);
memset (&rbbi[1], 0, sizeof (rbbi[1]));
rbbi[1].cbSize = sizeof (REBARBANDINFO);
rbbi[1].fMask = RBBIM_SIZE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_ID
| RBBIM_STYLE | RBBIM_TEXT | RBBIM_BACKGROUND
| RBBIM_IMAGE | 0;
rbbi[1].cxMinChild = rect.right - rect.left;
rbbi[1].cyMinChild = rect.bottom - rect.top;
rbbi[1].cx = 100;
rbbi[1].fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP | 0;
rbbi[1].wID = ID_COMBOBOX;
rbbi[1].hwndChild = hwndCombo;
rbbi[1].lpText = TEXT("ComboBox");
rbbi[1].hbmBack = LoadBitmap (g_hInst, MAKEINTRESOURCE (IDB_BKGRD));
rbbi[1].iImage = 0;
// Insert the combo box band in the rebar control.
SendMessage (hwndRB, RB_INSERTBAND, (WPARAM)-1,
(LPARAM) (LPREBARBANDINFO)&rbbi[1]);
// Reposition the rebar control.
MoveRebar (hwnd, hwndRB);
return hwndRB;
}
/***********************************************************************
FUNCTION:
MoveRebar
PURPOSE:
Moves the rebar to the top or the bottom.
***********************************************************************/
void MoveRebar (HWND hwnd, HWND hwndRB)
{
RECT rect, // Contains the coordinates of the main window's
// client area
rectRB; // Contains the dimensions of the bounding
// rectangle of the rebar control
int iX, iY, // Upper-left corner position of the bounding
// rectangle of the rebar control
iWidth, iHeight, // Width and height of the bounding rectangle
// of the rebar control
iCBHeight; // Height of the command bar.
// Get the height of the command bar.
iCBHeight = CommandBar_Height (g_hwndCB);
// Retrieve the coordinates of the main window's client area.
GetClientRect (hwnd, &rect);
// Retrieve the dimensions of the bounding rectangle of the rebar.
GetWindowRect (hwndRB, &rectRB);
switch (g_wSide)
{
case TOP:
default:
// Align the rebar along the top of the window.
iX = 0;
iY = iCBHeight;
iWidth = rect.right - rect.left;
iHeight = rect.bottom - rect.top - iCBHeight;
break;
case BOTTOM:
// Align the rebar along the bottom of the window.
iX = 0;
iY = rect.bottom - (rectRB.bottom - rectRB.top);
iWidth = rect.right - rect.left;
iHeight = rectRB.bottom - rectRB.top;
break;
}
// Change the position and dimensions of the rebar control.
MoveWindow (hwndRB, iX, iY, iWidth, iHeight, TRUE);
}
/***********************************************************************
FUNCTION:
DoNotify
PURPOSE:
Handles the WM_NOTIFY messages.
***********************************************************************/
LRESULT DoNotify (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
LPNMHDR lpNM = (LPNMHDR)lParam;
switch (lpNM->code)
{
default:
break;
}
return FALSE;
}
/***********************************************************************
FUNCTION:
HandleMenuPopup
PURPOSE:
Handles the WM_INITMENUPOPUP messages.
***********************************************************************/
LRESULT HandleMenuPopup (HMENU hMenu)
{
UINT uSelect;
switch (g_wSide)
{
case TOP:
default:
uSelect = IDM_VIEW_TOP;
break;
case BOTTOM:
uSelect = IDM_VIEW_BOTTOM;
break;
}
// Check the menu item and make it a radio item. Uncheck the rest of
// the menu items in the associated group and clear the radio-item
// type flag for those items.
CheckMenuRadioItem (hMenu, IDM_VIEW_TOP, IDM_VIEW_BOTTOM, uSelect,
MF_BYCOMMAND);
return 0;
}
/***********************************************************************
FUNCTION:
HandleCommand
PURPOSE:
Handles the WM_COMMAND messages.
***********************************************************************/
LRESULT HandleCommand (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
switch (GET_WM_COMMAND_ID (wParam, lParam))
{
case ID_COMBOBOX:
break;
case IDM_VIEW_TOP:
if (g_wSide != TOP)
{
// Destroy the existing rebar.
DestroyWindow (g_hwndRB);
// Make sure the rebar aligns on the top.
g_wSide = TOP;
// Create a new rebar.
g_hwndRB = CreateRebar (hwnd);
// Reposition the rebar.
MoveRebar (hwnd, g_hwndRB);
}
break;
case IDM_VIEW_BOTTOM:
if (g_wSide != BOTTOM)
{
// Destroy the existing rebar.
DestroyWindow (g_hwndRB);
// Make sure the rebar aligns on the bottom.
g_wSide = BOTTOM;
// Create a new rebar.
g_hwndRB = CreateRebar (hwnd);
// Reposition the rebar.
MoveRebar (hwnd, g_hwndRB);
}
break;
case IDM_HELP_ABOUT:
DialogBox (g_hInst, MAKEINTRESOURCE (IDD_ABOUTDLG), g_hwndMain,
AboutDialogProc);
break;
case IDM_FILE_EXIT:
CommandBar_Destroy (g_hwndCB);
DestroyWindow (g_hwndRB);
DestroyWindow (hwnd);
break;
default:
break;
}
return TRUE;
}
// END OF REBAR.C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -