用VC++ api函数给媳妇写了一个2010司法考试倒计时源码

来源:互联网 发布:mac app store 更新 编辑:程序博客网 时间:2024/05/01 04:38

整个源码是在系统自建hello程序的基础上修改来的。

// tesf.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <Afxwin.h>
#include <stdlib.h>


#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];        // The title bar text
CTime wnow;


// Foward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
long sifatima();

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 
 MyRegisterClass(hInstance);

 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }

 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESF);

// wnow=CTime::GetCurrentTime();
// unsigned t1=(unsigned)((60-wnow.GetSecond())*1000);
 SetTimer(NULL,1,100,NULL);


 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  
 }

 return msg.wParam;
}

 

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = NULL;
 wcex.lpszClassName = "szWindowClass";
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

 return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow("szWindowClass", szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 400, 300, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);


 //SetTimer(hWnd,NULL,t1,NULL);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR szHello[MAX_LOADSTRING],szST[MAX_LOADSTRING];
 RECT rt,rt1,rt2,rt3;
 
 int sifat=(int)sifatima();

 CString sstr;
 sstr.Format("距离司法考试还剩下%d天,请抓紧时间应考",sifat);
 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
 LoadString(hInst, IDS_STIM, szST, MAX_LOADSTRING);
 CString csr;
 
// szHello=szHello+jili;

 switch (message)
 {

  case WM_CREATE:
  SetTimer(hWnd,1,10000,NULL);
  break;
  case WM_TIMER:
   KillTimer(hWnd,1);
   InvalidateRect(hWnd,NULL,TRUE);
   UpdateWindow(hWnd);
   
   break;
  
  case WM_PAINT:
   wnow=CTime::GetCurrentTime();
   csr.Format("现在是%4d年%2d月%2d日%2d点%2d分",wnow.GetYear(),wnow.GetMonth(),wnow.GetDay(),wnow.GetHour(),wnow.GetMinute());
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...

   GetClientRect(hWnd, &rt);
   rt1=rt;
   rt2=rt;
   rt3=rt;
   rt1.top=rt.top+30;
   rt2.top=rt1.top+30;
   rt3.top=rt2.top+30;
   DrawText(hdc, csr, strlen(csr), &rt, DT_CENTER);
   DrawText(hdc, szST, strlen(szST), &rt1, DT_CENTER);
   DrawText(hdc, szHello, strlen(szHello), &rt2, DT_CENTER);
   DrawText(hdc, sstr, strlen(sstr), &rt3, DT_CENTER);
   EndPaint(hWnd, &ps);
   SetTimer(hWnd,1,1000,NULL);
   break;
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

long sifatima()
{
 CTime now=CTime::GetCurrentTime(); 
 CTime sifa(2010,9,11,9,0,0,-1);
 CTimeSpan difs;
 time_t tnow,tsifa,tdif;
 long diffl;
 tnow=now.GetTime();
 tsifa=sifa.GetTime();
 tdif=tsifa-tnow;
 difs=CTimeSpan(tdif);
 diffl=difs.GetDays();
 return diffl;
}

原创粉丝点击