Click here to Skip to main content
15,867,308 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.7K   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.

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)


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 15:22
buaafang5-Apr-03 15:22 
Generalcannot run MFC applications Pin
c_anands30-Dec-02 22:21
c_anands30-Dec-02 22:21 
Questionhow to delete the "view" in the menu of "peghelp.exe" in ppc? Pin
mydearsky10-Dec-02 15:57
mydearsky10-Dec-02 15:57 
QuestionDropdown list in the toolbar?!? Pin
Daniel Strigl10-Sep-02 9:05
Daniel Strigl10-Sep-02 9:05 
Questiondisabling buttons? Pin
1-Jun-01 0:27
suss1-Jun-01 0:27 
How can I disable buttons in commandband? Usual MFC method is not
working (UPDATE_COMMAND_UI message)...
Confused | :confused:

yu.
AnswerRe: disabling buttons? Pin
1-Jun-01 1:52
suss1-Jun-01 1:52 
GeneralRe: disabling buttons? Pin
12-Jun-01 4:05
suss12-Jun-01 4:05 
QuestionHow to do the same withour MFC and respecting WinCE 3.0 conventions. Pin
Chris Pichon2-May-01 3:20
Chris Pichon2-May-01 3:20 
QuestionHow to hide commandband Pin
22-Apr-01 5:08
suss22-Apr-01 5:08 
AnswerRe: How to hide commandband Pin
Fred D.13-Nov-02 6:50
Fred D.13-Nov-02 6:50 
GeneralThere is one more step you have to do... Pin
Roger Sager31-Aug-00 2:07
Roger Sager31-Aug-00 2:07 
GeneralCE 2.11 Pin
Mario Arruda17-Jan-00 5:53
sussMario Arruda17-Jan-00 5:53 
GeneralRe: CE 2.11 Pin
Brian Weeres19-Jan-00 19:50
Brian Weeres19-Jan-00 19:50 
GeneralRe: CE 2.11 Pin
Brian Weeres19-Jan-00 20:10
Brian Weeres19-Jan-00 20:10 
GeneralThanks for Finding the error Pin
Bruce Hearder19-Jan-00 20:25
Bruce Hearder19-Jan-00 20:25 
GeneralRe: Thanks for Finding the error Pin
Brian Weeres29-Jan-00 16:29
Brian Weeres29-Jan-00 16: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.