Click here to Skip to main content
15,795,059 members
Articles / Mobile Apps

Using Windows CE CommandBands in an MFC based application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Feb 2000CPOL 124.2K   172   24   16
An introduction to using CommandBars in CE.

Sample Image - commandbar.jpg

CommandBands are those neat little draggable strips that appear at the top of most CE based applications. They have been around since at least CE 2.00, and do give an application a much more professional appearance than the "standard" fixed toolbar that is available in AppWiz.

CommandBands are containers for other controls. Most commonly CommandBands. Each of these bands is a container for menus, buttons and other controls.

The sample code provided here will work with CE 2.0 and greater.
There are a few examples of generating CommandBands on the Net, but most are not designed for MFC applications, and there are a few "gotchas" to be aware of when using commandbands in a MFC app.

If we change CMainFrame's OnCreate, we can get CommandBands working.


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
      return -1;

   HWND hBar=NULL;

#if defined(_WIN32_WCE) && (_WIN32_WCE < 201)  
  hBar = this->m_poCommandBar->m_hCommandBar;
  if (hBar !=NULL)
    delete this->m_poCommandBar;
    this->m_poCommandBar = NULL;
   HINSTANCE hInst = AfxGetInstanceHandle();

   icex.dwSize = sizeof(icex);

// Use this code, if commandband images are required.
   HBITMAP hbmp;

//Imagelist for custom bitmap, which is used by the RBBS_IMAGE flag
   himl = ImageList_Create(16, 16, ILC_COLOR, 1, 0);

//Load the custom TOOLBAR pictures

//Add the TOOLBAR pictures to the image list
   ImageList_Add(himl, hbmp, 0);

//Clean up the bitmap

  b = (HWND)::CommandBands_Create(hInst, this->m_hWnd, ID_BANDS, 
  if (b==NULL) return -1;

  theApp.m_hRebars= b;
// Will be making 3 bands (1 for the menu, and 2 for the toolbars)

  rbi[0].cbSize = sizeof(REBARBANDINFO);
  rbi[0].fStyle = RBBS_NOGRIPPER;
  rbi[0].wID = ID_MENUBAND;
  rbi[0].cx = 120;

  rbi[1].cbSize = sizeof(REBARBANDINFO);
  rbi[1].fStyle = RBBS_GRIPPERALWAYS;
// Is on a PalmPC then put this bar on a new line
#if defined(_WIN32_WCE_PSPC)
   rbi[1].fStyle = rbi[1].fStyle | RBBS_BREAK;
  rbi[1].wID = ID_BAND1;
  rbi[1].cx = 155;

  rbi[2].cbSize = sizeof(REBARBANDINFO);
  rbi[2].fStyle = RBBS_GRIPPERALWAYS;
// Is on a PalmPC then put this bar on a new line
#if defined(_WIN32_WCE_PSPC)
  rbi[2].fStyle = rbi[2].fStyle | RBBS_BREAK;

  rbi[2].wID = ID_BAND2;

// Now ad all three bar to the CommandBenad
  if(!::CommandBands_AddBands(b, hInst, 3, rbi))
   return -1;

// Get a Handle to the first bar (Bar 0), aw we are going to insert a Menu
// into it
  hBar = ::CommandBands_GetCommandBar(b,0);
  ASSERT(hBar != NULL);
  ::CommandBar_InsertMenubar(hBar, hInst, IDR_MAINFRAME, 0);

// Get a Handle to ther second Bar (Bar 1). Will be putting a toolbar in it
  hBar = ::CommandBands_GetCommandBar(b,1);
  ASSERT(hBar != NULL);

 //Add the Bitmaps to the Second Commandbar
 //These are system standard bitmaps
 ::CommandBar_AddBitmap(hBar, hInst, IDB_BAND1, 
                       sizeof(Bar1)/sizeof(TBBUTTON), 0, 0);
//Add the Buttons to the Second Commandbar
//Here's a quirk..
#if (_WIN32_WCE < 201)
 CommandBar_AddButtons(hBar, sizeof(Bar1)/sizeof(TBBUTTON), &Bar1);
 ::CommandBar_AddButtons(hBar, sizeof(Bar1)/sizeof(TBBUTTON), &Bar1);

// Get a Handle to the third Bar (Bar 2). Will be putting a toolbar in it
  hBar = ::CommandBands_GetCommandBar(b,2);
  ASSERT(hBar != NULL);

 //Add the Bitmaps to the Second Commandbar
 //These are system standard bitmaps
 ::CommandBar_AddBitmap(hBar, hInst, IDB_BAND2,
                        sizeof(Bar2)/sizeof(TBBUTTON), 0, 0);
//Add the Buttons to the Second Commandbar
//Here's a quirk..
#if (_WIN32_WCE < 201)
 CommandBar_AddButtons(hBar, sizeof(Bar2)/sizeof(TBBUTTON), &Bar2);
 ::CommandBar_AddButtons(hBar, sizeof(Bar2)/sizeof(TBBUTTON), &Bar2);

//If not a PalmPC add the addornments(Closebutton)
#if !defined(_WIN32_WCE_PSPC)
  CommandBands_AddAdornments(b, hInst, 0, NULL);

 return 0;

We will also have to handle resizing of the View, as CMainFrame does not handle it any more. Simply put the following code in the CMainFrame's OnNotify method.

BOOL CMainFrame::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
// Are we dealling with CommandBands ?
if (wParam == ID_BANDS)
   return (TRUE);
 return CFrameWnd::OnNotify(wParam, lParam, pResult);

void CMainFrame::ResizeView()
  HWND rebars = theApp.m_hRebars;
  // Make sure we have a valid handle to the Commandbands
  if (rebars==NULL) return;
  // Get the first View 
  CView* pWnd = (CView*)GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
  ASSERT (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CView)));
  CRect rect;
  // Get the Height of the commandBand. This is changeable, so always get 
  //it when required.
  int nHeight =(UINT)::SendMessage(rebars, RB_GETBARHEIGHT, 0, 0);
  //Move the View into position.

One last thing to do, we need to make a change to your CMyApp::InitInitialise method.

Just before the final line where its says

return TRUE;

Insert the following line


This forces the view to resize properly, so that the view sits in its correct position with respect to CommandBands.

Thats it.. You now have a MFC-CE based application with Commandbands in it.

Special thanks goes to Mario Arruda and Brian Weeres for spotting a bug in the previous version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Generalsos Pin
buaafang5-Apr-03 16:22
buaafang5-Apr-03 16:22 
Generalcannot run MFC applications Pin
c_anands30-Dec-02 23:21
c_anands30-Dec-02 23:21 
Questionhow to delete the "view" in the menu of "peghelp.exe" in ppc? Pin
mydearsky10-Dec-02 16:57
mydearsky10-Dec-02 16:57 
QuestionDropdown list in the toolbar?!? Pin
Daniel Strigl10-Sep-02 10:05
Daniel Strigl10-Sep-02 10:05 
Questiondisabling buttons? Pin
1-Jun-01 1:27
suss1-Jun-01 1:27 
AnswerRe: disabling buttons? Pin
1-Jun-01 2:52
suss1-Jun-01 2:52 
GeneralRe: disabling buttons? Pin
12-Jun-01 5:05
suss12-Jun-01 5:05 
QuestionHow to do the same withour MFC and respecting WinCE 3.0 conventions. Pin
Chris Pichon2-May-01 4:20
Chris Pichon2-May-01 4:20 
QuestionHow to hide commandband Pin
22-Apr-01 6:08
suss22-Apr-01 6:08 
AnswerRe: How to hide commandband Pin
Fred D.13-Nov-02 7:50
Fred D.13-Nov-02 7:50 
GeneralThere is one more step you have to do... Pin
Roger Sager31-Aug-00 3:07
Roger Sager31-Aug-00 3:07 
GeneralCE 2.11 Pin
Mario Arruda17-Jan-00 6:53
sussMario Arruda17-Jan-00 6:53 
GeneralRe: CE 2.11 Pin
Brian Weeres19-Jan-00 20:50
Brian Weeres19-Jan-00 20:50 
GeneralRe: CE 2.11 Pin
Brian Weeres19-Jan-00 21:10
Brian Weeres19-Jan-00 21:10 
GeneralThanks for Finding the error Pin
Bruce Hearder19-Jan-00 21:25
Bruce Hearder19-Jan-00 21:25 
GeneralRe: Thanks for Finding the error Pin
Brian Weeres29-Jan-00 17:29
Brian Weeres29-Jan-00 17:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.