Click here to Skip to main content
15,895,859 members
Articles / Mobile Apps / Windows Mobile

Header Bars in Windows CE Using Win32

Rate me:
Please Sign up or sign in to vote.
4.24/5 (14 votes)
30 Aug 2003CPOL7 min read 81.9K   450   29  
Creating and using a header bar style control in Windows CE.
#include "stdafx.h"
#include <commctrl.h>

BOOL CreateHeaderRowButtons(HWND hwndHeader, TBBUTTON* pLeft, TBBUTTON* pRight)
{
	if (pLeft == NULL && pRight == NULL)
	{
		ASSERT(FALSE); // you must have either a left or a right button, you cannot have neither
		return FALSE;
	}

	// tell the toolbar the size of the button structure
	SendMessage(hwndHeader, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);

	// now create an empty button
	TBBUTTON btnEmpty = { -1 /*bitmap*/,0 /*command*/,TBSTATE_INDETERMINATE, TBSTYLE_AUTOSIZE, {0,0} /*reserved*/, 0 /*data*/,-1 /*string*/};
	ZeroMemory(&btnEmpty, sizeof(btnEmpty));
	btnEmpty.iBitmap = -1;
	btnEmpty.idCommand = 0;
	btnEmpty.fsState = TBSTATE_INDETERMINATE;
	btnEmpty.fsStyle = TBSTYLE_AUTOSIZE;
	btnEmpty.dwData = 0;
	btnEmpty.iString = -1;

	// we will have most three buttons at least two buttons.
	TBBUTTON buttons[3];

	int  iCurrentButton = 0;

	// if there is a left button then copy it into the first button index
	if (pLeft != NULL)
	{
		CopyMemory(&buttons[iCurrentButton++], pLeft, sizeof(TBBUTTON));
	}

	// now copy the empty button into either the first index if there is
	// no left button or the middle index if thee is aleft button
	CopyMemory(&buttons[iCurrentButton++], &btnEmpty, sizeof(TBBUTTON));

	// finally copy the right button into the middle index if there
	// is no left button or copy it into the third index if there
	// is a left button.
	if (pRight != NULL)
	{
		CopyMemory(&buttons[iCurrentButton++], pRight, sizeof(TBBUTTON));
	}

	// Now add the 2 or three buttons into the toolbar
	VERIFY(SendMessage(hwndHeader, TB_ADDBUTTONS, iCurrentButton, (LPARAM)&buttons));

	// the final thing left to do then is calulate the size of the
	// middle button based on the sizes of the left and/or right buttons.
	RECT rcLeft;
	ZeroMemory(&rcLeft, sizeof(rcLeft));
	RECT rcRight;
	ZeroMemory(&rcRight, sizeof(rcRight));

	// if there is a left button then get its dimensions.
	// Note that we do not use TBN_GETBUTTONINFO becuase this does
	// not return the correct size. Proberly need to find out why though!
	if (pLeft != NULL)
	{
		VERIFY(SendMessage(hwndHeader, TB_GETITEMRECT, 0, (LPARAM)&rcLeft));
	}
	else
		; // no left bitmap

	// we do the same for the right button if there is one
	if (pRight != NULL)
	{
		VERIFY(SendMessage(hwndHeader, TB_GETITEMRECT, pLeft == NULL ? 1 : 2, (LPARAM)&rcRight));
	}
			
	// Next we get the entire width of the header bar
	RECT rc;
	VERIFY(GetClientRect(hwndHeader, &rc));

	// and calculate and set its size to be the width of the bar less the width of the left and right buttons
	// if the buttons are present.
	TBBUTTONINFO tbiMiddle;
	tbiMiddle.cbSize = sizeof(TBBUTTONINFO);
	tbiMiddle.dwMask = TBIF_SIZE;
	tbiMiddle.cx = (WORD)((rc.right - rc.left) - (rcRight.right - rcRight.left) - (rcLeft.right - rcLeft.left));
	VERIFY(SendMessage(hwndHeader, TB_SETBUTTONINFO, (WPARAM)0, (LPARAM)&tbiMiddle));

	return TRUE;
}

HWND CreateStatusBar(HINSTANCE hInst, HWND hwnd)
{
	RECT rc;
	VERIFY(GetClientRect(hwnd, &rc));
	HWND hwndStatus = CreateWindow(STATUSCLASSNAME, _T("status"), CCS_NOPARENTALIGN | WS_CHILD | WS_VISIBLE,  rc.left, rc.bottom-26-30, rc.right - rc.left, 30, hwnd, NULL, hInst, NULL);
	ASSERT(IsWindow(hwndStatus));

	SendMessage(hwndStatus, SB_SETTEXT, SBT_NOBORDERS, (LPARAM)_T("Status bar text here"));
	
	InvalidateRect(hwndStatus, NULL, TRUE);
	return hwndStatus;
}

HWND CreateHeaderBar(HINSTANCE hInst, HWND hwnd)
{
	RECT rc;
	VERIFY(GetClientRect(hwnd, &rc));

	HWND hwndHeaderControl = CreateWindow(TOOLBARCLASSNAME, _T("Headerbar control"), TBSTYLE_CUSTOMERASE | WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_LIST | WS_BORDER, rc.left, rc.top, rc.right - rc.left, 44, hwnd, NULL, hInst, NULL);
	ASSERT(IsWindow(hwndHeaderControl));
	
	return hwndHeaderControl;
}

void ShowItemSelected(HWND hwndTree, HTREEITEM hti)
{
	if (hti == 0)
	{
		MessageBox(GetForegroundWindow(), _T("User cancelled selection"), _T("Caption"), MB_OK | MB_ICONINFORMATION);
	}
	else
	{
		TCHAR buffer[255];
		LPTSTR lpszStart = _tcscpy(buffer, _T("User selected ")) + 14;
		
		TV_ITEM tvi;
		tvi.mask = TVIF_TEXT;
		tvi.hItem = hti;
		tvi.pszText = lpszStart;
		tvi.cchTextMax = 255;
		VERIFY(TreeView_GetItem(hwndTree, &tvi));
		
		MessageBox(GetForegroundWindow(), buffer, _T("Caption"), MB_OK | MB_ICONINFORMATION);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United Kingdom United Kingdom
I make computers talk to other computers - well thats that I tell everyone I do. What I really do is connectivity and syncronization software for Windows Ce, PalmOS, Symbian on client devices and MFC/ATL/.NET on the Windows Servers.

I am also an expert with Microsoft Exchange and Databases and pretty good with Lotus Notes as well.

Comments and Discussions