Mobile Development: A battery monitor for the taskbar

Hello

this time I will show how I did a battery monitor that shows on the taskbar. The C source code uses the drawing functions DrawRect and Polygon to draw a battery symbol onto the taskbar. The drawing is updated every second by a background thread, that queries the battery status using GetSystemPowerStatusEx2. The thread then sends the battery flag reading using SendMessage with WM_USER.

Why did I write an app that already exists? If you look at your windows mobile taskbar, you will find, that the battery or the clock disappear from the taskbar. They will not be visible in all programs, depending on the space left on the taskbar. If you need to have a battery monitor that is shown all the time, this one may be for you. It will just show all the time on top of the taskbar.

The code

The drawings are done from arrays of POINT or RECT structures. So it is easy to change the drawings:

POINT pointsBatt[]={
	{22, 3},
	{4, 3},
	{4, 8},
	{2, 8},
	{2, 16},
	{4, 16},
	{4, 21},
	{22, 21},
	{22, 19},
	{6, 19},
	{6, 5},
	{22, 5}
};

//4 different bars inside batt
RECT rectBatt2[] = {
	{ 7, 7, 10, 17},
	{11, 7, 14, 17},
	{15, 7, 18, 17},
	{19, 7, 22, 17}
};
// a flash sign build as polygon
POINT pointsFlash[] = {
	{13, 2},
	{ 9,12},
	{14,10},
	{13,21},
	{17, 8},
	{12,10},
	{13, 2}
};

The drawings were first created on paper and then I entered the coordinates in these arrays (do you know an app, that will allow to draw on screen and then shows/saves an array of points? Let me know).

Depending on the return code send from the background thread, only parts of the drawing are done on the screen to reflect the actual status:

		case WM_UPDATESIGNAL: //background thread message, signal is in wParam
			iSignal = (int)wParam;
			DEBUGMSG(1, (L"Received WM_UPDATESIGNAL: %i\n", iSignal));
			//force a wm_paint message
			RECT rtUpdate;
			GetClientRect(g_hWnd, &rtUpdate);
			InvalidateRect(g_hWnd, &rtUpdate, true);
			break;
		case WM_PAINT:

			RECT rt;
			hdc = BeginPaint(hWnd, &ps);
				GetClientRect(hWnd, &rt); //draw text

				//the batt polygon
				hPen = CreatePen(PS_SOLID, 0, colorBatt);
				SelectObject(hdc, hBrBatt);
				SelectObject(hdc, hPen);
				x = sizeof(pointsBatt)/sizeof(POINT);
				Polygon(hdc, pointsBatt, x);

				switch(iSignal){
					case BATTERY_FLAG_HIGH:
						//draw all bars
						// the right bar
						FillRect(hdc, &rectBatt2[0], hbrGreen);
						// the mid bar
						FillRect(hdc, &rectBatt2[1], hbrGreen);
						// the left bars
						FillRect(hdc, &rectBatt2[2], hbrGreen);
						FillRect(hdc, &rectBatt2[3], hbrGreen);
						break;
					case BATTERY_FLAG_LOW:
						//draw two bar
						// the right bar
						FillRect(hdc, &rectBatt2[0], hbrYellow);
						// the mid yellow bar
						FillRect(hdc, &rectBatt2[1], hbrYellow);
						break;
					case BATTERY_FLAG_CRITICAL:
						//draw one bar
						// the right red bar
						FillRect(hdc, &rectBatt2[0], hbrRed);
						break;
					case BATTERY_FLAG_CHARGING:
						//draw flash sign
						//the flash symbol
						hPen = CreatePen(PS_SOLID, 0, colorYellow);
						SelectObject(hdc, hbrYellow);
						SelectObject(hdc, hPen);
						x = sizeof(pointsFlash)/sizeof(POINT);
						Polygon(hdc, pointsFlash, x);
						break;
					case BATTERY_FLAG_NO_BATTERY:
						//draw no bar
					case BATTERY_FLAG_UNKNOWN:
						//draw no bar
					default:
						//draw no bar
						//the batt polygon
						hPen = CreatePen(PS_SOLID, 0, colorUnknown);
						SelectObject(hdc, hbrUnknown);
						SelectObject(hdc, hPen);
						x = sizeof(pointsBatt)/sizeof(POINT);
						Polygon(hdc, pointsBatt, x);
						break;
				};
			EndPaint(hWnd, &ps);
			return 0;

I first searched a way to have a free floating window on top of all others before I realized, that I can use the window handle of the taskbar as the parent window handle of my window.

When you tap and release the stylus on the battery window, you will get a popup menu and there you can exit the application or open the options dialog. The current option dialog only controls the horizontal position of the battery window on the taskbar. Maybe you like to extend the options and enable the user to change colors etc.

The position itself is saved/loaded from the registry. If the rekistry key is missing, it will be created automatically:

void saveReg(DWORD dwVal){
	HKEY hKeyRes;
	DWORD dwDispo=0;
	LONG lRes=0;

	//ensure the regKey exists
	lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, NULL, REG_OPTION_NON_VOLATILE, 0, NULL, &hKeyRes, &dwDispo);
	//RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0,0, &hKey);
	if( lRes == ERROR_SUCCESS ){
		//save the value
		DWORD dwType = REG_DWORD;
		lRes = RegSetValueEx(hKeyRes, L"position", 0, dwType, (byte*)&dwVal, sizeof(DWORD));
		if(lRes != ERROR_SUCCESS)
			DEBUGMSG(1, (L"RegSetValueEx failed: lRes=0x%0x\n", lRes));
		RegCloseKey(hKeyRes);
	}
	else{
		DEBUGMSG(1, (L"RegCreateKeyEx failed: lRes=0x%0x\n", lRes));
	}
}

DWORD readReg(){
	DWORD dwVal=80;
	HKEY hKey;
	DWORD dwDispo=0;
	LONG lRes=0;

	//ensure the regKey exists
	lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0,0, &hKey);
	if( lRes == ERROR_SUCCESS ){
		//load the value
		DWORD dwType = REG_DWORD;
		DWORD dwSize = sizeof(DWORD);
		lRes = RegQueryValueEx (hKey, L"position", 0, &dwType, (byte*) &dwVal, &dwSize);
		if(lRes != ERROR_SUCCESS)
			DEBUGMSG(1, (L"RegGetValueEx failed: lRes=0x%0x\n", lRes));
		RegCloseKey(hKey);
		return dwVal;
	}
	else{
		DEBUGMSG(1, (L"RegOpenKeyEx failed: lRes=0x%0x\n", lRes));
		return 80;
	}
}

The code is written in Embedded Visual C 4, but you can use it in VS2005/2008 too.

[Download not found]

If you only like to use the app, here is the ArmV4i executable (WM5/6 compatible)

[Download not found]

Leave a Reply