Solved

Creating an Analog clock

Posted on 2010-08-28
2
645 Views
Last Modified: 2013-12-14
I need to write a program for an analog clock with graphics of the face, numbers, and hands. I am so confused. I know that this is the current time, how do I do it with the graphics to show the face?

#include <time.h>
#include <stdio.h>

 void main()
   {
     time_t t; // correction
     time( &t );
     puts( ctime( &t ));
   }
 
0
Comment
Question by:Rowley4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 33551626
From http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=11795&lngWId=3
This has hour, minute, and second hand. No numbers, but from the code, you may be able to finish the job.
    Hope this gets you started.   Good Luck!

//**************************************
// Name: Analog Clock - Win32
// Description:Shows the time in a win32, top most, tool window.
//Credit goes to: T J Betsworth, I used his Circle code, really nice code of yours btw.
// By: michael coder
//
// Side Effects:You will know the time :P
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=11795&lngWId=3//for details.//**************************************

#include <windows.h>
#include <math.h>
#include <time.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "ComboBox App";
HWND hwnd; 
HDC hdc;
HWND analogpic;
HBITMAP analog;
HPEN hPen1 = CreatePen(PS_SOLID,1,RGB(255,0,0));
HPEN hPen2 = CreatePen(PS_SOLID,2,RGB(0,0,255));
HPEN hPen3 = CreatePen(PS_SOLID,6,RGB(0,0,0));
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)
{
   MSG messages; 
   WNDCLASSEX wincl;
   wincl.hInstance = hThisInstance;
   wincl.lpszClassName = szClassName;
   wincl.lpfnWndProc = WindowProcedure;
   wincl.style = CS_DBLCLKS;
   wincl.cbSize = sizeof (WNDCLASSEX);
   wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
   wincl.lpszMenuName = NULL; 
   wincl.cbClsExtra = 0;
   wincl.cbWndExtra = 0; 
   wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
   if (!RegisterClassEx (&wincl))
      return 0;
   hwnd = CreateWindowEx (
      WS_EX_PALETTEWINDOW, 
      szClassName,
      "Time",
      WS_POPUPWINDOW|WS_CAPTION, 
      0,
      0,
      103,
      122,
      HWND_DESKTOP,
      NULL,
      hThisInstance,
      NULL
      );
   hdc = GetDC(hwnd);
   analogpic = CreateWindow("static", "", WS_CHILD | WS_VISIBLE | SS_BITMAP, 0, 0,50, 50, hwnd, NULL, hThisInstance, NULL);
   analog = (HBITMAP)LoadImage(0,"clock.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
   SendMessage(analogpic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)analog);

   ShowWindow (hwnd, nFunsterStil);
   while (GetMessage (&messages, NULL, 0, 0))
   {
      TranslateMessage(&messages);
      DispatchMessage(&messages);
   }
   return 1;
}
void DrawPoint(int x, int y, HPEN pen)
{
   SelectObject(hdc,pen);
   MoveToEx(hdc,x,y,NULL);
   LineTo(hdc,x,y);
}
void DrawLine(int x1, int y1, int x2, int y2, HPEN pen){
   SelectObject(hdc,pen);
   MoveToEx(hdc,x1,y1,NULL);
   LineTo(hdc,x2,y2);
}
void DrawHourHand(int originx, int originy, float value,int handlength)
{
   DrawLine(originx, originy, (handlength*cos(6.3*value/60))+originx, (handlength*sin(6.3*value/60))+originy,hPen2);
}
void DrawMinuteHand(int originx, int originy, float value, int handlength)
{
   DrawLine(originx, originy, (handlength*cos(6.3*value/60))+originx, (handlength*sin(6.3*value/60))+originy,hPen2);
}
void DrawSecondHand(int originx, int originy, float value, int handlength)
{
   DrawLine(originx, originy, (handlength*cos(6.3*value/60))+originx, (handlength*sin(6.3*value/60))+originy,hPen1);
}
void Time()
{
   int pointx = GetSystemMetrics(SM_CXSCREEN)/2;
   int pointy = GetSystemMetrics(SM_CYSCREEN)/2;
   char timeStr [9];
   char hour[2];
   char minute[2];
   char second[2];
   int Hour;
   int Minute;
   int Second;
   while(1)
   {
      _strtime( timeStr );
      hour[0] = timeStr[0];
      hour[1] = timeStr[1];
      minute[0] = timeStr[3];
      minute[1] = timeStr[4];
      second[0] = timeStr[6];
      second[1] = timeStr[7];
      Hour = atoi(hour);
      Minute = atoi(minute);
      Second = atoi(second);
      if(Hour>12)
         Hour = Hour - 12;
      DrawHourHand(50,50,((Hour*5)-15)+(5*Minute/60),33);
      DrawMinuteHand(50,50,45+Minute,47);
      DrawSecondHand(50,50,45+Second,48);
      DrawPoint(50,50,hPen3);
      Sleep(100);
      SendMessage(analogpic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)analog);
   }
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   // static HWND hwndList, hwndText;
   switch (message) /* handle the messages */
   {
   case WM_CREATE: 
      CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Time,0,0,0); 
      break;
   case WM_DESTROY:
      PostQuitMessage (0);/* send a WM_QUIT to the message queue */
      break;
   default: /* for messages that we don't deal with */
      return DefWindowProc (hwnd, message, wParam, lParam);
   }
   return 0;
}

Open in new window

0
 
LVL 8

Expert Comment

by:Pramod Bugudai
ID: 33556713
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question