处理命令带消息 翻译:tellmenow
同起命令条相比,命令带控件需要更多一些维护。差别在于,命令带控件可以改变高度,这样就要求包含命令带控件的窗口必须监视控件,并且在控件尺寸变化时重新绘制窗口,很可能还要格式化其客户区。
当用户重新排列控件的时候,命令带控件会发送许多不同的WM_NOTIFY消息。为了监控控件高度,应用程序需要检查RBN_HEIGHCHANGE通知消息并做相应回应。下面的代码演示这个过程:
// This code is inside a WM_NOTIFY message handler.
LPNMHDR pnmh;
pnmh = (LPNMHDR)lParam;
if (pnmh->code == RBN_HEIGHTCHANGE) {
InvalidateRect (hWnd, NULL, TRUE);
}
如果检测到RBN_HEIGHTCHANGE消息通知,例程就简单的使窗口客户区无效,产生一个WM_PAINT消息。接下来在处理绘制消息的代码中调用UINT CommandBands_Height (HWND hwndCmdBands)来查询命令带控件的高度,并从客户区矩形中减去该高度。和命令条一样,使用BOOL CommandBands_Show (HWND HwndCmdBands, BOOL fShow)可以隐藏或者显示命令带控件。通过调用函数BOOL CommandBands_IsVisible (HWND hwndCmdBands)可以查询控件的可视状态。
CmdBand示例
CmdBand程序演示了一个相当完整的命令带控件。示例中创建了三个带区,一个是固定菜单带区,一个是包含许多按钮的带区,一个是包含编辑控件的带区。每个带区中的透明命令条和背景位图被用在创建带背景图的命令带控件中。
通过选择View菜单中的Command Bar菜单项,可以使用一个简单的命令条来替换命令带控件。通过选择View菜单中的Command Bands,可以将命令带控件重新创建并恢复到上次的配置状态。CmdBand程序代码显示在清单5-2中。
清单5-2:CmdBand 程序
CmdBand.rc
//======================================================================
// Resource file
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include "windows.h"
#include "CmdBand.h" // Program-specific stuff
//----------------------------------------------------------------------
// Icons and bitmaps
//
ID_ICON ICON "cmdband.ico" // Program icon
CmdBarBmps BITMAP "cbarbmps.bmp" // Bmp used in cmdband image list
CmdBarEditBmp BITMAP "cbarbmp2.bmp" // Bmp used in cmdband image list
CmdBarBack BITMAP "backg2.bmp" // Bmp used for cmdband background
//----------------------------------------------------------------------
// Menu
//
ID_MENU MENU DISCARDABLE
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_EXIT
END
POPUP "&View"
BEGIN
MENUITEM "Command Bar", IDM_VIEWCMDBAR
MENUITEM "Command Band", IDM_VIEWCMDBAND
END
POPUP "&Help"
BEGIN
MENUITEM "&About...", IDM_ABOUT
END
END
//----------------------------------------------------------------------
// About box dialog template
//
aboutbox DIALOG discardable 10, 10, 160, 40
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | DS_CENTER |
DS_MODALFRAME
CAPTION "About"
BEGIN
ICON ID_ICON, -1, 5, 5, 10, 10
LTEXT "CmdBand - Written for the book Programming Windows CE Copyright 2003 Douglas Boling"
-1, 40, 5, 110, 30
END
CmdBand.h
//======================================================================
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD { // Structure associates
UINT Code; // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.
};
//----------------------------------------------------------------------
// Defines used by application
//
#define IDC_CMDBAND 1 // Command band ID
#define IDC_CMDBAR 2 // Command bar ID
#define ID_ICON 10 // Icon ID
#define ID_MENU 11 // Main menu resource ID
#define IDC_EDITCTL 12
#define IDB_CMDBAND 50 // Base ID for bands
#define IDB_CMDBANDMENU 50 // Menu band ID
#define IDB_CMDBANDBTN 51 // Button band ID
#define IDB_CMDBANDEDIT 52 // Edit control band ID
// Menu item IDs
#define IDM_EXIT 100
#define IDM_VIEWCMDBAR 110
#define IDM_VIEWCMDBAND 111
#define IDM_ABOUT 120
#define NUMBANDS 3
//----------------------------------------------------------------------
// Function prototypes
//
int CreateCommandBand (HWND hWnd, BOOL fFirst);
int DestroyCommandBand (HWND hWnd);
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);
// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
// Message handlers
LRESULT DoCreateMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoNotifyMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);
// Command functions
LPARAM DoMainCommandViewCmdBar (HWND, WORD, HWND, WORD);
LPARAM DoMainCommandVCmdBand (HWND, WORD, HWND, WORD);
LPARAM DoMainCommandExit (HWND, WORD, HWND, WORD);
LPARAM DoMainCommandAbout (HWND, WORD, HWND, WORD);
// Dialog procedures
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM);
CmdBand.cpp
//======================================================================
// CmdBand - Dialog box demonstration
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "CmdBand.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("CmdBand");
HINSTANCE hInst; // Program instance handle
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
WM_NOTIFY, DoNotifyMain,
WM_COMMAND, DoCommandMain,
WM_DESTROY, DoDestroyMain,
};
// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDM_VIEWCMDBAR, DoMainCommandViewCmdBar,
IDM_VIEWCMDBAND, DoMainCommandVCmdBand,
IDM_EXIT, DoMainCommandExit,
IDM_ABOUT, DoMainCommandAbout,
};
// Command band button initialization structure
const TBBUTTON tbCBStdBtns[] = {
// BitmapIndex Command State Style UserData String
{STD_FILENEW, 210, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{STD_FILEOPEN, 211, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{STD_FILESAVE, 212, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
{STD_CUT, 213, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{STD_COPY, 214, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{STD_PASTE, 215, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
{STD_PROPERTIES, 216, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
};
// Command bar initialization structure
const TBBUTTON tbCBViewBtns[] = {
// BitmapIndex Command State Style UserData String
{0, 0, 0,
TBSTYLE_SEP, 0, 0},
{VIEW_LARGEICONS, 210, TBSTATE_ENABLED | TBSTATE_CHECKED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_SMALLICONS, 211, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_LIST, 212, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_DETAILS, 213, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{VIEW_SORTNAME, 214, TBSTATE_ENABLED | TBSTATE_CHECKED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_SORTTYPE, 215, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_SORTSIZE, 216, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_SORTDATE, 217, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0}
};
// Array that stores the band configuration
COMMANDBANDSRESTOREINFO cbr[NUMBANDS];
INT nBandOrder[NUMBANDS];
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
HWND hwndMain;
MSG msg;
// Initialize application.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
HWND hWnd;
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
// Save program instance handle in global variable.
hInst = hInstance;
#if defined(WIN32_PLATFORM_PSPC)
// If Pocket PC, allow only one instance of the application.
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return 0;
}
#endif
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Load the command bar common control class.
icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
icex.dwICC = ICC_COOL_CLASSES;
InitCommonControlsEx (&icex);
// Create main window.
hWnd = CreateWindow (szAppName, TEXT ("CmdBand Demo"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Message handling procedures for MainWindow
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
CreateCommandBand (hWnd, TRUE);
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
HWND hwndCB;
RECT rect;
HDC hdc;
POINT ptArray[2];
// Adjust the size of the client rect to take into account
// the command bar or command bands height.
GetClientRect (hWnd, &rect);
if (hwndCB = GetDlgItem (hWnd, IDC_CMDBAND))
rect.top += CommandBands_Height (hwndCB);
else
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
hdc = BeginPaint (hWnd, &ps);
ptArray[0].x = rect.left;
ptArray[0].y = rect.top;
ptArray[1].x = rect.right;
ptArray[1].y = rect.bottom;
Polyline (hdc, ptArray, 2);
ptArray[0].x = rect.right;
ptArray[1].x = rect.left;
Polyline (hdc, ptArray, 2);
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process WM_NOTIFY message for window.
//
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
LPNMHDR pnmh;
// Parse the parameters.
pnmh = (LPNMHDR)lParam;
if (pnmh->code == RBN_HEIGHTCHANGE) {
InvalidateRect (hWnd, NULL, TRUE);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandVCmdBarStd - Process View | Std Command bar command.
//
LPARAM DoMainCommandViewCmdBar (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
HWND hwndCB;
hwndCB = GetDlgItem (hWnd, IDC_CMDBAND);
if (hwndCB)
DestroyCommandBand (hWnd);
else
return 0;
// Create a minimal command bar that has only a menu and
// an exit button.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Insert the menu.
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
InvalidateRect (hWnd, NULL, TRUE);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandVCmdBand - Process View | Command band command.
//
LPARAM DoMainCommandVCmdBand (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
HWND hwndCB;
hwndCB = GetDlgItem (hWnd, IDC_CMDBAR);
if (hwndCB)
CommandBar_Destroy (hwndCB);
else
return 0;
CreateCommandBand (hWnd, FALSE);
InvalidateRect (hWnd, NULL, TRUE);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Use DialogBox to create modal dialog box.
DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
return 0;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDOK:
case IDCANCEL:
EndDialog (hWnd, 0);
return TRUE;
}
break;
}
return FALSE;
}
//----------------------------------------------------------------------
// DestroyCommandBand - Destroy command band control after saving
// the current configuration.
//
int DestroyCommandBand (HWND hWnd) {
HWND hwndCB;
INT i, nBand, nMaxBand = 0;
hwndCB = GetDlgItem (hWnd, IDC_CMDBAND);
for (i = 0; i < NUMBANDS; i++) {
// Get band index from ID value.
nBand = SendMessage (hwndCB, RB_IDTOINDEX, IDB_CMDBAND+i, 0);
// Save the band number to save order of bands.
nBandOrder[i] = nBand;
// Get the restore information.
cbr[i].cbSize = sizeof (COMMANDBANDSRESTOREINFO);
CommandBands_GetRestoreInformation (hwndCB, nBand, &cbr[i]);
}
DestroyWindow (hwndCB);
return 0;
}
//----------------------------------------------------------------------
// CreateCommandBand - Create a formatted command band control.
//
int CreateCommandBand (HWND hWnd, BOOL fFirst) {
HWND hwndCB, hwndBand, hwndChild;
INT i, nBand, nBtnIndex, nEditIndex;
LONG lStyle;
HBITMAP hBmp;
HIMAGELIST himl;
REBARBANDINFO rbi[NUMBANDS];
// Create image list control for bitmaps for minimized bands.
himl = ImageList_Create (16, 16, ILC_COLOR, 3, 0);
// Load first two images from one bitmap.
hBmp = LoadBitmap (hInst, TEXT ("CmdBarBmps"));
ImageList_Add (himl, hBmp, NULL);
DeleteObject (hBmp);
// Load third image as a single bitmap.
hBmp = LoadBitmap (hInst, TEXT ("CmdBarEditBmp"));
ImageList_Add (himl, hBmp, NULL);
DeleteObject (hBmp);
// Create a command band.
hwndCB = CommandBands_Create (hInst, hWnd, IDC_CMDBAND,
RBS_SMARTLABELS |
RBS_AUTOSIZE | RBS_VARHEIGHT, himl);
// Load bitmap used as background for command bar.
hBmp = LoadBitmap (hInst, TEXT ("CmdBarBack"));
// Initialize common REBARBANDINFO structure fields.
for (i = 0; i < dim(rbi); i++) {
rbi[i].cbSize = sizeof (REBARBANDINFO);
rbi[i].fMask = RBBIM_ID | RBBIM_IMAGE | RBBIM_SIZE |
RBBIM_BACKGROUND | RBBIM_STYLE;
rbi[i].wID = IDB_CMDBAND+i;
rbi[i].hbmBack = hBmp;
}
// If first time, initialize the restore structure since it is
// used to initialize the band size and style fields.
if (fFirst) {
nBtnIndex = 1;
nEditIndex = 2;
cbr[0].cxRestored = 130;
cbr[1].cxRestored = 210;
cbr[1].fStyle = RBBS_FIXEDBMP;
cbr[2].cxRestored = 130;
cbr[2].fStyle = RBBS_FIXEDBMP | RBBS_CHILDEDGE;
} else {
// If not first time, set order of bands depending on
// the last order.
if (nBandOrder[1] < nBandOrder[2]) {
nBtnIndex = 1;
nEditIndex = 2;
} else {
nBtnIndex = 2;
nEditIndex = 1;
}
}
// Initialize REBARBANDINFO structure for each band.
// 1. Menu band
rbi[0].fStyle = RBBS_FIXEDBMP | RBBS_NOGRIPPER;
rbi[0].cx = cbr[0].cxRestored;
rbi[0].iImage = 0;
// 2. Standard button band
rbi[nBtnIndex].fMask |= RBBIM_TEXT;
rbi[nBtnIndex].iImage = 1;
rbi[nBtnIndex].lpText = TEXT ("Std Btns");
// The next two parameters are initialized from saved data.
rbi[nBtnIndex].cx = cbr[1].cxRestored;
rbi[nBtnIndex].fStyle = cbr[1].fStyle;
// 3. Edit control band
hwndChild = CreateWindow (TEXT ("edit"), TEXT ("edit ctl"),
WS_VISIBLE | WS_CHILD | ES_MULTILINE | WS_BORDER,
0, 0, 10, 5, hWnd, (HMENU)IDC_EDITCTL, hInst, NULL);
rbi[nEditIndex].fMask |= RBBIM_TEXT | RBBIM_STYLE |
RBBIM_CHILDSIZE | RBBIM_CHILD;
rbi[nEditIndex].hwndChild = hwndChild;
rbi[nEditIndex].cxMinChild = 0;
rbi[nEditIndex].cyMinChild = 23;
rbi[nEditIndex].cyChild = 55;
rbi[nEditIndex].iImage = 2;
rbi[nEditIndex].lpText = TEXT ("Edit field");
// The next two parameters are initialized from saved data.
rbi[nEditIndex].cx = cbr[2].cxRestored;
rbi[nEditIndex].fStyle = cbr[2].fStyle;
// Add bands.
CommandBands_AddBands (hwndCB, hInst, 3, rbi);
// Add menu to first band.
hwndBand = CommandBands_GetCommandBar (hwndCB, 0);
CommandBar_InsertMenubar (hwndBand, hInst, ID_MENU, 0);
// Add standard buttons to second band.
hwndBand = CommandBands_GetCommandBar (hwndCB, nBtnIndex);
// Insert buttons
CommandBar_AddBitmap (hwndBand, HINST_COMMCTRL, IDB_STD_SMALL_COLOR,
16, 0, 0);
CommandBar_AddButtons (hwndBand, dim(tbCBStdBtns), tbCBStdBtns);
// Modify the style flags of each command bar to make transparent.
for (i = 0; i < NUMBANDS; i++) {
hwndBand = CommandBands_GetCommandBar (hwndCB, i);
lStyle = SendMessage (hwndBand, TB_GETSTYLE, 0, 0);
lStyle |= TBSTYLE_TRANSPARENT;
SendMessage (hwndBand, TB_SETSTYLE, 0, lStyle);
}
// If not the first time the command band has been created, restore
// the user's last configuration.
if (!fFirst) {
for (i = 0; i < NUMBANDS; i++) {
if (cbr[i].fMaximized) {
nBand = SendMessage (hwndCB, RB_IDTOINDEX,
cbr[i].wID, 0);
SendMessage (hwndCB, RB_MAXIMIZEBAND, nBand, TRUE);
}
}
}
// Add exit button to command band.
CommandBands_AddAdornments (hwndCB, hInst, 0, NULL);
return 0;
}
CmdBand示例中,在CreateCommandBand例程里创建了命令带。该例程最初在DoCreateMain中被调用,随后又在菜单处理函数DoMain-CommandVCmdBand中被调用。程序使用RBS_SMARTLABELS风格创建了命令带控件,在最小化和被还原或最大化时使用图像列表和文本标签来标记每个带区。图像列表被创建,并使用位图初始化,当带区被最小化时需要使用这些位图。
REBARBANDINFO结构数组被初始化,用来定义三个带区。如果控件之前被销毁,那么来自COMMANDBANDSRESTOREINFO结构的数据被用来初始化风格域以及cx域。CreateCommandBand例程中,通过查看控件最后一次被销毁时保存的带区索引,对按钮和编辑带区的顺序做了一个假设。虽然用这个方法来判断带区之前的顺序不完全可靠,但它为您提供了一个很好的参考评估。
当命令带控件创建以后,会修改每个带区中的命令条来设置TBS_TRANSPARENT风格。这个过程中,通过使用每个带区的背景位图,演示了如何使用背景位图来使命令带控件有合适的外观。
当程序CmdBand使用命令条来替换命令带区控件时,应用程序首先调用DestroyCommandBand函数来保存当前配置信息,并销毁命令带控件。该函数使用CommandBands_GetRestoreInformation来查询每个带区的尺寸和风格。该函数还保存了每个带区的索引,作为将来推测按钮和编辑带区当前顺序的数据。第一个带区--菜单带区--使用的是RBBS_NOGRIPPER风格,所以不存在位置问题。