{"id":720,"date":"2010-07-29T16:30:30","date_gmt":"2010-07-29T14:30:30","guid":{"rendered":"http:\/\/www.hjgode.de\/wp\/?p=720"},"modified":"2010-07-29T16:32:22","modified_gmt":"2010-07-29T14:32:22","slug":"mobile-development-a-battery-monitor-for-the-taskbar","status":"publish","type":"post","link":"https:\/\/www.hjgode.de\/wp\/2010\/07\/29\/mobile-development-a-battery-monitor-for-the-taskbar\/","title":{"rendered":"Mobile Development: A battery monitor for the taskbar"},"content":{"rendered":"<p>Hello<\/p>\n<p>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.<\/p>\n<p><a rel=\"attachment wp-att-722\" href=\"http:\/\/www.hjgode.de\/wp\/2010\/07\/29\/mobile-development-a-battery-monitor-for-the-taskbar\/screenshot2_taskbaraddon2\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-722\" title=\"screenshot2_taskbaraddon2\" src=\"http:\/\/www.hjgode.de\/wp\/wp-content\/uploads\/2010\/07\/screenshot2_taskbaraddon2.gif\" alt=\"\" width=\"48\" height=\"48\" \/><\/a><\/p>\n<p>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.<\/p>\n<h2>The code<\/h2>\n<p>The drawings are done from arrays of POINT or RECT structures. So it is easy to change the drawings:<\/p>\n<p><!--more--><\/p>\n<pre language=\"cplusplus\" escaped=\"true\">POINT pointsBatt[]={\r\n\t{22, 3},\r\n\t{4, 3},\r\n\t{4, 8},\r\n\t{2, 8},\r\n\t{2, 16},\r\n\t{4, 16},\r\n\t{4, 21},\r\n\t{22, 21},\r\n\t{22, 19},\r\n\t{6, 19},\r\n\t{6, 5},\r\n\t{22, 5}\r\n};\r\n\r\n\/\/4 different bars inside batt\r\nRECT rectBatt2[] = {\r\n\t{ 7, 7, 10, 17},\r\n\t{11, 7, 14, 17},\r\n\t{15, 7, 18, 17},\r\n\t{19, 7, 22, 17}\r\n};\r\n\/\/ a flash sign build as polygon\r\nPOINT pointsFlash[] = {\r\n\t{13, 2},\r\n\t{ 9,12},\r\n\t{14,10},\r\n\t{13,21},\r\n\t{17, 8},\r\n\t{12,10},\r\n\t{13, 2}\r\n};\r\n<\/pre>\n<p>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).<\/p>\n<p>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:<\/p>\n<pre language=\"cplusplus\" escaped=\"true\">\t\tcase WM_UPDATESIGNAL: \/\/background thread message, signal is in wParam\r\n\t\t\tiSignal = (int)wParam;\r\n\t\t\tDEBUGMSG(1, (L\"Received WM_UPDATESIGNAL: %i\\n\", iSignal));\r\n\t\t\t\/\/force a wm_paint message\r\n\t\t\tRECT rtUpdate;\r\n\t\t\tGetClientRect(g_hWnd, &amp;rtUpdate);\r\n\t\t\tInvalidateRect(g_hWnd, &amp;rtUpdate, true);\r\n\t\t\tbreak;\r\n\t\tcase WM_PAINT:\r\n\r\n\t\t\tRECT rt;\r\n\t\t\thdc = BeginPaint(hWnd, &amp;ps);\r\n\t\t\t\tGetClientRect(hWnd, &amp;rt); \/\/draw text\r\n\r\n\t\t\t\t\/\/the batt polygon\r\n\t\t\t\thPen = CreatePen(PS_SOLID, 0, colorBatt);\r\n\t\t\t\tSelectObject(hdc, hBrBatt);\r\n\t\t\t\tSelectObject(hdc, hPen);\r\n\t\t\t\tx = sizeof(pointsBatt)\/sizeof(POINT);\r\n\t\t\t\tPolygon(hdc, pointsBatt, x);\r\n\r\n\t\t\t\tswitch(iSignal){\r\n\t\t\t\t\tcase BATTERY_FLAG_HIGH:\r\n\t\t\t\t\t\t\/\/draw all bars\r\n\t\t\t\t\t\t\/\/ the right bar\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[0], hbrGreen);\r\n\t\t\t\t\t\t\/\/ the mid bar\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[1], hbrGreen);\r\n\t\t\t\t\t\t\/\/ the left bars\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[2], hbrGreen);\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[3], hbrGreen);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase BATTERY_FLAG_LOW:\r\n\t\t\t\t\t\t\/\/draw two bar\r\n\t\t\t\t\t\t\/\/ the right bar\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[0], hbrYellow);\r\n\t\t\t\t\t\t\/\/ the mid yellow bar\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[1], hbrYellow);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase BATTERY_FLAG_CRITICAL:\r\n\t\t\t\t\t\t\/\/draw one bar\r\n\t\t\t\t\t\t\/\/ the right red bar\r\n\t\t\t\t\t\tFillRect(hdc, &amp;rectBatt2[0], hbrRed);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase BATTERY_FLAG_CHARGING:\r\n\t\t\t\t\t\t\/\/draw flash sign\r\n\t\t\t\t\t\t\/\/the flash symbol\r\n\t\t\t\t\t\thPen = CreatePen(PS_SOLID, 0, colorYellow);\r\n\t\t\t\t\t\tSelectObject(hdc, hbrYellow);\r\n\t\t\t\t\t\tSelectObject(hdc, hPen);\r\n\t\t\t\t\t\tx = sizeof(pointsFlash)\/sizeof(POINT);\r\n\t\t\t\t\t\tPolygon(hdc, pointsFlash, x);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase BATTERY_FLAG_NO_BATTERY:\r\n\t\t\t\t\t\t\/\/draw no bar\r\n\t\t\t\t\tcase BATTERY_FLAG_UNKNOWN:\r\n\t\t\t\t\t\t\/\/draw no bar\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\t\/\/draw no bar\r\n\t\t\t\t\t\t\/\/the batt polygon\r\n\t\t\t\t\t\thPen = CreatePen(PS_SOLID, 0, colorUnknown);\r\n\t\t\t\t\t\tSelectObject(hdc, hbrUnknown);\r\n\t\t\t\t\t\tSelectObject(hdc, hPen);\r\n\t\t\t\t\t\tx = sizeof(pointsBatt)\/sizeof(POINT);\r\n\t\t\t\t\t\tPolygon(hdc, pointsBatt, x);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t};\r\n\t\t\tEndPaint(hWnd, &amp;ps);\r\n\t\t\treturn 0;\r\n<\/pre>\n<p>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.<\/p>\n<p><a rel=\"attachment wp-att-723\" href=\"http:\/\/www.hjgode.de\/wp\/2010\/07\/29\/mobile-development-a-battery-monitor-for-the-taskbar\/screenshot_taskbaraddon2\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-723\" title=\"screenshot_taskbaraddon2\" src=\"http:\/\/www.hjgode.de\/wp\/wp-content\/uploads\/2010\/07\/screenshot_taskbaraddon2-225x300.gif\" alt=\"\" width=\"225\" height=\"300\" srcset=\"https:\/\/www.hjgode.de\/wp\/wp-content\/uploads\/2010\/07\/screenshot_taskbaraddon2-225x300.gif 225w, https:\/\/www.hjgode.de\/wp\/wp-content\/uploads\/2010\/07\/screenshot_taskbaraddon2-112x150.gif 112w, https:\/\/www.hjgode.de\/wp\/wp-content\/uploads\/2010\/07\/screenshot_taskbaraddon2.gif 240w\" sizes=\"(max-width: 225px) 100vw, 225px\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>The position itself is saved\/loaded from the registry. If the rekistry key is missing, it will be created automatically:<\/p>\n<pre language=\"cplusplus\" escaped=\"true\">void saveReg(DWORD dwVal){\r\n\tHKEY hKeyRes;\r\n\tDWORD dwDispo=0;\r\n\tLONG lRes=0;\r\n\r\n\t\/\/ensure the regKey exists\r\n\tlRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, NULL, REG_OPTION_NON_VOLATILE, 0, NULL, &amp;hKeyRes, &amp;dwDispo);\r\n\t\/\/RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0,0, &amp;hKey);\r\n\tif( lRes == ERROR_SUCCESS ){\r\n\t\t\/\/save the value\r\n\t\tDWORD dwType = REG_DWORD;\r\n\t\tlRes = RegSetValueEx(hKeyRes, L\"position\", 0, dwType, (byte*)&amp;dwVal, sizeof(DWORD));\r\n\t\tif(lRes != ERROR_SUCCESS)\r\n\t\t\tDEBUGMSG(1, (L\"RegSetValueEx failed: lRes=0x%0x\\n\", lRes));\r\n\t\tRegCloseKey(hKeyRes);\r\n\t}\r\n\telse{\r\n\t\tDEBUGMSG(1, (L\"RegCreateKeyEx failed: lRes=0x%0x\\n\", lRes));\r\n\t}\r\n}\r\n\r\nDWORD readReg(){\r\n\tDWORD dwVal=80;\r\n\tHKEY hKey;\r\n\tDWORD dwDispo=0;\r\n\tLONG lRes=0;\r\n\r\n\t\/\/ensure the regKey exists\r\n\tlRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0,0, &amp;hKey);\r\n\tif( lRes == ERROR_SUCCESS ){\r\n\t\t\/\/load the value\r\n\t\tDWORD dwType = REG_DWORD;\r\n\t\tDWORD dwSize = sizeof(DWORD);\r\n\t\tlRes = RegQueryValueEx (hKey, L\"position\", 0, &amp;dwType, (byte*) &amp;dwVal, &amp;dwSize);\r\n\t\tif(lRes != ERROR_SUCCESS)\r\n\t\t\tDEBUGMSG(1, (L\"RegGetValueEx failed: lRes=0x%0x\\n\", lRes));\r\n\t\tRegCloseKey(hKey);\r\n\t\treturn dwVal;\r\n\t}\r\n\telse{\r\n\t\tDEBUGMSG(1, (L\"RegOpenKeyEx failed: lRes=0x%0x\\n\", lRes));\r\n\t\treturn 80;\r\n\t}\r\n}\r\n<\/pre>\n<p>The code is written in Embedded Visual C 4, but you can use it in VS2005\/2008 too.<\/p>\n[Download not found]\n<p>If you only like to use the app, here is the ArmV4i executable (WM5\/6 compatible)<\/p>\n[Download not found]\n","protected":false},"excerpt":{"rendered":"<p>A battery status monitor for the windows mobile taskbar<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[178,3,4],"tags":[],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-programming","category-tools"],"_links":{"self":[{"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/posts\/720"}],"collection":[{"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/comments?post=720"}],"version-history":[{"count":8,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/posts\/720\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/posts\/720\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/media?parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/categories?post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hjgode.de\/wp\/wp-json\/wp\/v2\/tags?post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}