Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / WinMobile

Mobile Development: A Binary Clock for the taskbar

4.00/5 (2 votes)
15 Oct 2010CPOL 15.4K  
Mobile development - a binary clock for the taskbar

Do you also like the binary watches from IO?

OK, I like them and was inspired to make some binary clock for the Windows Mobile taskbar.

The time shown here is 14:23 displayed as binary dots. The top row (red) shows the hours in 24h format. The second row shows the minutes and the bottom row shows the seconds.

What you see here is encoded in binary:

01110   is equal to 0*16 1*8 1*4 1*2 0*1 = 8 + 4 + 2 = 14
10111   is equal to 1*16 0*8 1*4 1*2 1*1 = 16 + 4 + 2 + 1 = 23
00100   is equal to 0*16 0*8 1*4 0*2 0*1 = 4 = 4

So you see, it shows the time is 14:23:04.

The code itself is not very interesting except for the WM_PAINT one:

C
case WM_PAINT:

    RECT rt;
    hdc = BeginPaint(hWnd, &ps);
    GetClientRect(hWnd, &rt); //draw text
    DEBUGMSG(1, (L"Received WM_PAINT: %ix%i\n", rt.right, rt.bottom));

    //the drawing
    //hPen = CreatePen(PS_SOLID, 1, colorRed);
    //SelectObject(hdc, hbrRed);
    //SelectObject(hdc, hPen);
    //bRes = Ellipse(hdc, 1,1, 4,4);

    SYSTEMTIME st;
    GetLocalTime(&st);
    bHour=(BYTE)st.wHour;   theTime[0]=bHour;
    bMin =(BYTE)st.wMinute; theTime[1]=bMin;
    bSec =(BYTE)st.wSecond; theTime[2]=bSec;

    for (int iRow = 0; iRow < 3; iRow++)
    {
   for (int bit = 0; bit < 6; bit++)
   {
       if (! (iRow == 0 && bit == 5) ) //for hour row draw only 5 bits
       {
      iTemp=bit;
      bTemp=(((theTime[iRow] >> iTemp) & 1) == 1);
      RECT rectC;
      rectC.left=xMax - ((iOffsetX + (bit * rWidth)) + (iGapX * bit)); //we draw from right to left
      rectC.top=iOffsetY + (iRow * rHeight) + (iGapY * iRow);
      rectC.right=rectC.left + rWidth;// xMax - (iOffsetX + bit * rWidth) + rWidth;
      rectC.bottom=rectC.top + rHeight; // iOffsetY + (iRow * rHeight) + rHeight;
      if(bTemp){
          SelectObject(hdc, thePens[iRow]);
          SelectObject(hdc, theBrushes[iRow]);
      }else{
          SelectObject(hdc, hpenBlack);
          SelectObject(hdc, hbrBlack);
      }
      Ellipse(hdc, rectC.left, rectC.top,  rectC.right, rectC.bottom);
       }
   }
    }

    if(bRes)
   DEBUGMSG(1, (L"Ellipse done\n"));
    else
   DEBUGMSG(1, (L"Ellipse failed: %i\n", GetLastError()));

    EndPaint(hWnd, &ps);

    return 0;
case WM_CLOSE:

A Binary Clock in C#

Here is something more to play with, a binary clock written for Compact Framework.

The code is as given below:

C#
private void f1_paint(object sender,PaintEventArgs e) {

    // Get Graphics Object
    Graphics g = e.Graphics; 
    
    // Method under System.Drawing.Graphics
    DateTime dt = DateTime.Now;
    string s=string.Format("{0:HH:mm:ss}", dt);
    g.DrawString(s,new Font("Verdana",20,FontStyle.Regular),
            new SolidBrush(Color.Tomato),40,200);
    //g.FillEllipse(bRed, rHH16);
    //g.DrawEllipse(pRed, rHH08);
    
    byte bHour = (byte)dt.Hour;
    byte bMin = (byte)dt.Minute;
    byte bSec = (byte)dt.Second;
    int iOffsetX = 10;
    int iOffsetY = 10;
    int rWidth = 30;
    int rHeight = 30;
    int xMax = 240-rWidth+iOffsetX;
    
    // a byte array with the time
    byte[] bTimes = new byte[3];
    bTimes[0] = bHour;
    bTimes[1] = bMin;
    bTimes[2] = bSec;
    
    // a bool array with the times
    boolStruct[] boolTimes = new boolStruct[3];
    boolTimes[0] = byte2bool(bHour);
    boolTimes[1] = byte2bool(bMin);
    boolTimes[2] = byte2bool(bSec);
    
    for (int iRow = 0; iRow < 3; iRow++)
    {
        for (int bit = 0; bit < 6; bit++)
        {
            if (boolTimes[iRow].boolList[bit])
            {
                if (!((iRow == 0) && (bit == 5)))
                    g.FillEllipse(bRed, new Rectangle
                    (xMax - (iOffsetX + bit * 40), iOffsetY + 
                    (iRow * 40), rWidth, rHeight));
            }
            else
            {
                if (!((iRow == 0) && (bit == 5)))
                    g.DrawEllipse(pRed, new Rectangle
                    (xMax - (iOffsetX + bit * 40), iOffsetY + 
                    (iRow * 40), rWidth, rHeight));
            }
        }
    }
}
private class boolStruct
{
    private bool[] _boolList = new bool[6];
    public bool[] boolList
    {
        get { return _boolList; }
        set { _boolList = value; }
    }
}
private boolStruct byte2bool(byte b){
    boolStruct bArr = new boolStruct();
    for (int i = 0; i < 6; i++)
    {
        bool Temp_Bool = (((b >> i) & 1) == 1);
        bArr.boolList[i] = Temp_Bool;
    }
    return bArr;
}

License

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