View Full Version: Simple Clock Sourcecode

C++ Learning Community > C++ Creations > Simple Clock Sourcecode


Title: Simple Clock Sourcecode


Xception - June 24, 2003 04:37 PM (GMT)
Ask if you have any questions.
CODE

/* Clock */
/* made by */
/* Xception */

#include <windows.h>
#include <time.h>

//  Declare Windows procedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{
   HWND hwnd;                   // This is the handle for the window
   MSG messages;               // Here messages to the application are saved
   WNDCLASSEX wincl;          // Data structure for the windowclass
   RECT rect;                // rectangle for desktop resolution
 
   // The Window structure
   wincl.hInstance = hInstance;
   wincl.lpszClassName = "MainWnd";
   wincl.lpfnWndProc = WindowProcedure;   // This function is called by windows
   wincl.style = CS_DBLCLKS;              // Catch double-clicks
   wincl.cbSize = sizeof (WNDCLASSEX);

   // Use default icon and mouse-pointer
   wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
   wincl.lpszMenuName = NULL;         // No menu
   wincl.cbClsExtra = 0;              // No extra bytes after the window class
   wincl.cbWndExtra = 0;              // structure or the window instance
   wincl.hbrBackground = (HBRUSH) COLOR_WINDOWFRAME; // background color

   // Register the window class, and if it fails quit the program
   if (!RegisterClassEx (&wincl))
       return 0;
   // Store rectangle of Desktop in rect    
       GetWindowRect(GetDesktopWindow(),&rect);
   // Create the window
   hwnd = CreateWindowEx (
          0,                 // no extended window style
          "MainWnd",         // Window Classname
          "Clock",           // Window Title Text
          WS_SYSMENU|WS_MINIMIZEBOX, // Window style: Systemmenu+Minimizebox
          (int)rect.right/2-75,       // center window horizontally
          (int)rect.bottom/2-35,      // and vertically
          150,                 // Window width
          70,                 // Window height
          NULL,        // Window has no parent window
          NULL,        // no menu
          hInstance,       // Window Instance handle
          NULL);                 // No Window Creation data
         
   ShowWindow (hwnd, nCmdShow); // Show the window
   SetTimer(hwnd,1,1000,NULL); // Set timer to 1 second
   // Run the message loop. It will run until GetMessage() returns 0
   while (GetMessage (&messages, NULL, 0, 0))
   {
       // Translate virtual-key messages into character messages
       TranslateMessage(&messages);
       // Send message to WindowProcedure
       DispatchMessage(&messages);
   }

   // The program return-value is 0 - The value that PostQuitMessage() gave
   return messages.wParam;
}


//  This function is called by the Windows function DispatchMessage()

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char t[7]="Time: ";  // string "Time: "
   switch (message) // handle the messages
   {
   HDC hdc;        // handle to a device context
   char curtime[9]; // buffer for time string
       case WM_DESTROY:
           KillTimer(hwnd,1); // kill the timer at program end
           PostQuitMessage (0);       // send a WM_QUIT to the message queue
           break;
       case WM_TIMER:
       InvalidateRect(hwnd,NULL,false); // refresh the output each second  
       case WM_PAINT:
           PAINTSTRUCT ps;
           hdc=BeginPaint(hwnd,&ps);
           _strtime(curtime);
           // draw the time on the window
           TextOut(hdc,23,10,strcat(t,curtime),6+strlen(curtime));
           EndPaint(hwnd,&ps);
           break;
       default:    // for messages that we don't deal with
           return DefWindowProc (hwnd, message, wParam, lParam);
   }
   return 0;
}

dr voodoo - June 24, 2003 05:40 PM (GMT)
When I try to compile your code (I use Dev-C++) I get the following error:

QUOTE
[Linker error] undefined reference to `TextOutA@20'


There's no indication of a line or file so I have no idea what I'm doing wrong or what is wrong in your code. :unsure:

Xception - June 24, 2003 09:11 PM (GMT)
You have to link with gdi(libgdi32.a) library
And it's a Win32 Gui project. I don't have to add libgdi32.a manually, maybe you compiled it as console project? If not then add the libgdi32 library to the linker options.

Dragon - June 24, 2003 09:34 PM (GMT)
Hey, Xception, that's pretty cool. The only thing I can do right now in C++ is using different controls (buttons, etc.). The clock doesn't seem that hard either. I have a question, though. When I use LRESULT CALLBACK ..., I use WndProc instead of WindowProcedure. There is no difference there, is there? If you could, please see my topic in C++ Help. I need some help from somebody who's quite a bit more experienced than the rest of us here. :)

Shadow of the Moon - June 24, 2003 09:35 PM (GMT)
That is pretty cool Xception. But what should we expect from someone of your caliber. :)

Xception - June 24, 2003 09:51 PM (GMT)
Dragon, you can call the window callback procedure what you want, in the window structure you tell windows the name of the callback procedure:

CODE
wincl.lpfnWndProc = WindowProcedure;   // This function is called by windows



StarReaver2 - June 24, 2003 10:01 PM (GMT)
Sweet! You ARE a good programmer Xception. Is there any chance you can make it say AM and PM other than using 24 hour time? because at the time i tried it out it said: 15:56:03

That would be a good update for it.

RealDesertFox - June 24, 2003 10:13 PM (GMT)
When I compile the script, the box around the time is in white, now my W98 settings are set so that windows and box are grey. Is there a way to alter the colour of the box to Windows settings?

Dragon - June 24, 2003 11:28 PM (GMT)
QUOTE
Dragon, you can call the window callback procedure what you want, in the window structure you tell windows the name of the callback procedure:


Alright.

QUOTE
Sweet! You ARE a good programmer Xception. Is there any chance you can make it say AM and PM other than using 24 hour time? because at the time i tried it out it said: 15:56:03


Is it really that hard to read 24-hr. time? It shouldn't be that hard, though, to convert it to AM/PM time.

StarReaver2 - June 24, 2003 11:39 PM (GMT)
I'm not bad at reading 24-hour time...i'm just not used to it. it was 3:56 PM when i used it.

Dragon - June 24, 2003 11:54 PM (GMT)
And 3 seconds. B)

Dark_Sonic - June 25, 2003 07:43 AM (GMT)
And I'm not good in reading those AM/PM times... I live in Finland and here we use ONLY 24 hour system... ;)

dr voodoo - June 25, 2003 02:48 PM (GMT)
QUOTE
maybe you compiled it as console project?

I did. Now it works. Looks nice.

I'll see if I can make any sence out of that code :) .

sparkeh - October 23, 2005 11:23 PM (GMT)
Nice looking clock you got there, you should theme it, lol

ih8censorship - October 24, 2005 01:45 AM (GMT)
HOLY THREAD RESURECTION!

for a minute there i thought dr voodoo had a little brain fart, then i realized it was a post from when he was a beginner. just goes to show everyone starts in a simmilar way :)

Neken - October 24, 2005 02:00 AM (GMT)
i don't understand people who ressurect topics and doesnt add anything to it except "good job" ;)




* Hosted for free by InvisionFree