john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

win click follow clock

/* 2010-02-12  john pfeiffer, MS windows clock v4 (updating time)

todo: wm_paint, wm_timer, strcpy instead of get_current?
*/

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

#include <windows.h>
#define  IDC_ExitButton 40001 
#define  IDC_UpdateTimeButton 40002
#define BUFFERMAX 128


LRESULT CALLBACK WindowProcedure( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

VOID APIENTRY initializeBackground( HWND hwnd )
{   HDC hdc;
    PAINTSTRUCT ps;
    hdc = BeginPaint(hwnd, &ps);
    EndPaint(hwnd, &ps);
}

void get_current_time( char current_time[ BUFFERMAX ] );

VOID APIENTRY displayText( HWND hwnd, char text[BUFFERMAX], int x, int y )
{
    HDC hdc;
    PAINTSTRUCT ps;
    wchar_t outputtext [BUFFERMAX];
    hdc = BeginPaint(hwnd, &ps);
    hdc = GetDC(hwnd);

    ExtTextOut( hdc, x, y, NULL, NULL, text , strlen( text ), NULL);

    ReleaseDC(hwnd, hdc);
    EndPaint(hwnd, &ps);
}


int WINAPI WinMain( HINSTANCE hThisInstance, HINSTANCE hPrevInstance, 
    LPSTR lpszArgument, int ncmdshow)               
{


    HWND hwnd;           /* The handle for our window */
    HWND QuitButton = NULL;
    HWND UpdateTimeButton = NULL;

    MSG messages;        /* Messages to the application  */
    WNDCLASS wc;         /* Data structure for our defined windowclass */

    wc.style = 0;
    wc.lpfnWndProc = WindowProcedure;   /* This function is called by windows */
    wc.cbClsExtra = 0;                   /* No extra bytes after the window class */
    wc.cbWndExtra = 0;                   /* structure or the window instance */
    wc.hInstance = hThisInstance;        /* handle to the owner */
    wc.hIcon = NULL;                     /* no special application icon */
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
    wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);   
    wc.lpszMenuName = NULL;                   /* No menu */
    wc.lpszClassName = "WindowsApp";          /* the name of the windows class */


    /* Register the window class, if fail quit the program with an error value */
    if( RegisterClass(&wc) ==0 ){ return -1;    }

    /* The class is registered, let's instantiate our window */
    hwnd = CreateWindowEx( 1, "WindowsApp", "Windows Clock",
       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
       NULL, NULL, hThisInstance, NULL );

    /* create button and store the handle */

    QuitButton = CreateWindow( 
        "button",                       /* class name */
        "Quit",                 /* button caption */
        WS_CHILD |WS_VISIBLE | BS_PUSHBUTTON,  /* the styles */
        0,0,                            /* the left and top coordinates */
        100,50,                         /* width and height */
        hwnd,                           /* parent window handle */
        (HMENU)IDC_ExitButton,        /* the ID of your button */
        hThisInstance,                  /* the instance of your application */
        NULL) ;                         /* unnecessary extra */


    UpdateTimeButton = CreateWindow( 
        "button",                       /* class name */
        "Update Time",                  /* button caption */
        WS_CHILD |WS_VISIBLE | BS_PUSHBUTTON,  /* the styles */
        100,0,                            /* the left and top coordinates */
        100,50,                         /* width and height */
        hwnd,                           /* parent window handle */
        (HMENU)IDC_UpdateTimeButton,        /* the ID of your button */
        hThisInstance,                  /* the instance of your application */
        NULL) ;                         /* unnecessary extra */


    ShowWindow(hwnd, ncmdshow);     /* Make the window visible on the screen */
    UpdateWindow(hwnd);             /* update with changes */

    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while( GetMessage(&messages, NULL, 0, 0) )
    {
        TranslateMessage(&messages); 
        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 current_time[BUFFERMAX];
    char temp_time[BUFFERMAX];
    PAINTSTRUCT ps;     /* a structure for a paint job (see below */
    RECT rect;     /* a struct to hold rectangle values (e.g. x,y coordinates) */
    HDC hdc;            /* handle to a DC (buffer) for the screen */

    switch (message)                  /* handle the messages */
    {
        case WM_LBUTTONDOWN:
            get_current_time( temp_time );   /* get the current time initially */

            /* good practice to zero things before using them */
            memset( temp_time , 0, sizeof( temp_time ));

            sprintf( temp_time , "%d,%d", LOWORD(lParam) , HIWORD(lParam));
            displayText( hwnd, temp_time, LOWORD(lParam) , HIWORD(lParam) );
        break;
        case WM_CHAR:
        break;
        case WM_KEYDOWN:
        break;

        case WM_COMMAND:
            switch(LOWORD(wParam))    /* find out what's been clicked */
            {
                case IDC_ExitButton:
                    PostQuitMessage(0);
                break;
                case IDC_UpdateTimeButton:

                break;
                default:
                break;
            }
        break;
        case WM_PAINT:
            initializeBackground( hwnd );
        break;
        case WM_CREATE:
            initializeBackground( hwnd );
        break;
        case WM_DESTROY:
            PostQuitMessage(0);       
        break;
        default:                   /* for messages that we don't deal with */
            return DefWindowProc(hwnd, message, wParam, lParam);
        break;
    }/* end switch (message) */

    return 0;
}/* end of WinMain */



/* Get the current time from the system and update the time string */
void get_current_time( char current_time[BUFFERMAX] )
{
    time_t tempTime;
/* initialize the variable, otherwise only returns 1970 date */
    tempTime = time(NULL);  
    strcpy(current_time, asctime(localtime(&tempTime)));
}

  • « drupal basic configuration search block
  • drupal contact form »

Published

Feb 12, 2010

Category

c

~549 words

Tags

  • c 95
  • click 3
  • follow 1
  • win 19