Click here to Skip to main content
Click here to Skip to main content

Using Windows CE CommandBands in an MFC based application

, 1 Feb 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

eg.

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

   HWND b=NULL;
   HWND hBar=NULL;

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

   INITCOMMONCONTROLSEX icex;
   icex.dwSize = sizeof(icex);
   icex.dwICC  = ICC_BAR_CLASSES | ICC_COOL_CLASSES;
   InitCommonControlsEx(&icex);

// Use this code, if commandband images are required.
   HIMAGELIST himl=NULL;
   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
   hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BANDIMAGES));

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

//Clean up the bitmap
   DeleteObject(hbmp);

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

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

  memset(&rbi,0,sizeof(REBARBANDINFO));
  rbi[0].cbSize = sizeof(REBARBANDINFO);
  rbi[0].fMask = RBBIM_STYLE|RBBIM_ID|RBBIM_SIZE;
  rbi[0].fStyle = RBBS_NOGRIPPER;
  rbi[0].wID = ID_MENUBAND;
  rbi[0].cx = 120;

  rbi[1].cbSize = sizeof(REBARBANDINFO);
  rbi[1].fMask = RBBIM_STYLE|RBBIM_ID|RBBIM_SIZE |RBBIM_IMAGE;     
  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;
#endif
  rbi[1].wID = ID_BAND1;
  rbi[1].cx = 155;
  rbi[1].iImage=0;

  rbi[2].cbSize = sizeof(REBARBANDINFO);
  rbi[2].fMask = RBBIM_STYLE|RBBIM_ID|RBBIM_SIZE |RBBIM_IMAGE;     
  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;
#endif

  rbi[2].wID = ID_BAND2;
  rbi[2].iImage=1;


// 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);
#else
 ::CommandBar_AddButtons(hBar, sizeof(Bar1)/sizeof(TBBUTTON), &Bar1);
#endif

// 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);
#else
 ::CommandBar_AddButtons(hBar, sizeof(Bar2)/sizeof(TBBUTTON), &Bar2);
#endif

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

 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)
 {
   ResizeView();
   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;
  GetClientRect(&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.
  pWnd->MoveWindow(0,nHeight,rect.Width(),rect.Height()-nHeight,TRUE);
}

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

    ((CMainFrame*)AfxGetMainWnd())->ResizeView();

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.

License

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

Share

About the Author

Bruce Hearder
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionHow to hide commandband PinmemberAnonymous22-Apr-01 6:08 
AnswerRe: How to hide commandband PinmemberFred D.13-Nov-02 7:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 2 Feb 2000
Article Copyright 2000 by Bruce Hearder
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid