CSDN博客

img aixihuan

Win32 API GetLocalTime C version

发表于2004/10/11 21:06:00  3443人阅读

分类: 学习历程

#include <windows.h>
#include <stdlib.h>  /* We are using malloc so this is needed */


LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "GetLocalTimeApp";

/* Our function prototypes */
/*-----------------------------------------------------------*/
int GetDayofweek(char *Dayofweek);
int GetLocalTimeHHMMSS(char *LocalTimeHHMMSS);
int GetLocalDateDMY(char *LocalDateDMY);
int GetLocalDateMDY(char *LocalDateMDY);
int GetLocalDateYMD(char *LocalDateYMD);
/*-----------------------------------------------------------*/

/* Some variables for use in testing the functions */
/*-----------------------------------------------------------*/
char *LocalDateDMY;
char *LocalDateMDY;
char *LocalDateYMD;
char *LocalTimeHHMMSS;
char *Dayofweek;
/*-----------------------------------------------------------*/


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;              
    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) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
           0,                  
           szClassName,        
           "Using GetLocalTime Win32 API Version 1.0",      
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,      
           CW_USEDEFAULT,      
           350,                
           100,                
           HWND_DESKTOP,       
           NULL,               
           hThisInstance,      
           NULL                
           );

    ShowWindow (hwnd, nFunsterStil);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                
    {
        case WM_CREATE:
        /* Our tests begin here. */
        /*-----------------------------------------------------------*/
         LocalDateDMY = malloc(11);
         if ( GetLocalDateDMY(LocalDateDMY) > 0 )
          MessageBox(NULL, LocalDateDMY, "Date in DMY format", MB_OK);
         free(LocalDateDMY);
        
         LocalDateMDY = malloc(11);
         if ( GetLocalDateMDY(LocalDateMDY) > 0 )
          MessageBox(NULL, LocalDateMDY, "Date in MDY format", MB_OK);
         free(LocalDateMDY);
        
         LocalDateYMD = malloc(11);
         if ( GetLocalDateYMD(LocalDateYMD) > 0 )
          MessageBox(NULL, LocalDateYMD, "Date in YMD format", MB_OK);
         free(LocalDateYMD);
        
         LocalTimeHHMMSS = malloc(9);
         if ( GetLocalTimeHHMMSS(LocalTimeHHMMSS) > 0 )
          MessageBox(NULL, LocalTimeHHMMSS, "Time in 24 Hour format", MB_OK);
         free(LocalTimeHHMMSS);

         Dayofweek = malloc(2);
         if ( GetDayofweek(Dayofweek) > 0 )
          MessageBox(NULL, Dayofweek, "The week day is", MB_OK);
         free(Dayofweek);
         /* End of our tests */
         /*-----------------------------------------------------------*/
         return 0;
            break;
        case WM_DESTROY:
            PostQuitMessage (0);    
            break;
        default:                    
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

/* Our functions are defined here */
/*-----------------------------------------------------------*/
int GetDayofweek(char *Dayofweek)
{
SYSTEMTIME lpSystemTime;
char *lpDayofweekFmt = "%d";
char *bDayofweek;

    GetLocalTime(&lpSystemTime);
    
    if ( (bDayofweek = malloc(2)) != NULL) {
     wsprintf(bDayofweek, lpDayofweekFmt, lpSystemTime.wDayOfWeek);
        lstrcpy(Dayofweek, bDayofweek);
        free(bDayofweek);
    }
    else
     return -1;

}

/*-----------------------------------------------------------*/
int GetLocalTimeHHMMSS(char *LocalTimeHHMMSS)
{
SYSTEMTIME lpSystemTime;
char *lpTimeFmt = "%02d:%02d:%02d";
char *bTime;

    GetLocalTime(&lpSystemTime);
    if ( (bTime = malloc(9)) != NULL ) {
     wsprintf(bTime, lpTimeFmt,
                        lpSystemTime.wHour,
                        lpSystemTime.wMinute,
                        lpSystemTime.wSecond);
        lstrcpy(LocalTimeHHMMSS, bTime);
        free(bTime);
    }
    else
     return -1;   
}

/*-----------------------------------------------------------*/
int GetLocalDateDMY(char *LocalDateDMY)
{
SYSTEMTIME lpSystemTime;
char *lpDateFmt = "%02d/%02d/%02d";
char *bDateDMY;

    GetLocalTime(&lpSystemTime);
    if ( (bDateDMY = malloc(11)) != NULL ) {
     wsprintf(bDateDMY, lpDateFmt,
                           lpSystemTime.wDay,
                           lpSystemTime.wMonth,
                           lpSystemTime.wYear);
        lstrcpy(LocalDateDMY, bDateDMY);
        free(bDateDMY);
    }
    else
     return -1;        
}
/*-----------------------------------------------------------*/
int GetLocalDateMDY(char *LocalDateMDY)
{
SYSTEMTIME lpSystemTime;
char *lpDateFmt = "%02d/%02d/%02d";
char *bDateMDY;

    GetLocalTime(&lpSystemTime);
    if ( (bDateMDY = malloc(11)) != NULL) {
     wsprintf(bDateMDY, lpDateFmt,
                           lpSystemTime.wMonth,
                           lpSystemTime.wDay,
                           lpSystemTime.wYear);
        lstrcpy(LocalDateMDY, bDateMDY);
        free(bDateMDY);
     }
     else
      return -1;  
}

/*-----------------------------------------------------------*/
int GetLocalDateYMD(char *LocalDateYMD)
{
SYSTEMTIME lpSystemTime;
char *lpDateFmt = "%02d/%02d/%02d";
char *bDateYMD;

    GetLocalTime(&lpSystemTime);
    if ( (bDateYMD = malloc(11)) != NULL) {
     wsprintf(bDateYMD, lpDateFmt,
                           lpSystemTime.wYear,
                           lpSystemTime.wMonth,
                           lpSystemTime.wDay);
        lstrcpy(LocalDateYMD, bDateYMD);
        free(bDateYMD);
    }
    else
     return -1;   

}
/*-----------------------------------------------------------*/
/* end of file */

阅读全文
0 0

相关文章推荐

img
取 消
img