⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p_41.cpp

📁 显示数字钟表时间 有小时分钟和秒钟的精确显示
💻 CPP
字号:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);
BOOL Show_Time(struct tm * newtime);
char szWinName[]="WinCLK"; //name of window class
char str[80]="";
int maxX,maxY;
int hour,min,sec;

HBRUSH hbrush;
HBITMAP hbit,hbit1;
HBITMAP mybmp;
HDC memDC,memDC1;
POINT point;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdline,int nShowCmd)
{
	HWND hwnd;
	MSG msg;
	WNDCLASS wcl;

	//Define a window class
	wcl.hInstance = hInstance;
	wcl.lpszClassName = szWinName;
	wcl.lpfnWndProc = WindowFunc;
	
	wcl.style = 0;
	wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wcl.hCursor =LoadCursor(NULL,IDC_ARROW);

	wcl.lpszMenuName = NULL;

	wcl.cbClsExtra=0;
	wcl.cbWndExtra=0;

	wcl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

	if(!RegisterClass(&wcl)) return 0;

	hwnd = CreateWindow(szWinName,"Win Clock",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		HWND_DESKTOP,
		NULL,hInstance,NULL);

	int nWinMode = 1;
	ShowWindow(hwnd,nWinMode);
    //设置时钟,第二个参数为时钟序号,
	//第三个参数的单位为毫秒
	SetTimer(hwnd,1,1000,NULL);
	UpdateWindow(hwnd);
	
	mybmp=LoadBitmap(hInstance,"MYBMP");
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	//关闭时钟,第二个参数为时钟序号
    KillTimer(hwnd,1);
	return msg.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT paintstruct;
	struct tm * newtime;
	time_t t;
	
	switch (message)
	{
		case WM_CREATE:
			maxX=GetSystemMetrics(SM_CXFULLSCREEN);
			maxY=GetSystemMetrics(SM_CYFULLSCREEN);
			hdc=GetDC(hwnd);
			memDC=CreateCompatibleDC(hdc);
			hbit=CreateCompatibleBitmap(hdc,maxX,maxY);
			SelectObject(memDC,hbit);
			hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
			SelectObject(memDC,hbrush);
			PatBlt(memDC,0,0,maxX,maxY,PATCOPY);
		
			memDC1=CreateCompatibleDC(hdc);
			ReleaseDC(hwnd,hdc);
			break;
		case WM_SIZE:
			point.x=LOWORD(lParam);
			point.y=HIWORD(lParam);
			break;
		case WM_PAINT:
			hdc=BeginPaint(hwnd,&paintstruct);
			PatBlt(memDC,0,0,maxX,maxY,PATCOPY);
			Show_Time(newtime);
			BitBlt(hdc,0,0,maxX,maxY,memDC,0,0,SRCCOPY);
			EndPaint(hwnd,&paintstruct);
			break;
		case WM_TIMER:
			t = time(NULL);
			newtime=localtime(&t);
			hour=newtime->tm_hour;
			min=newtime->tm_min;
			sec=newtime->tm_sec;
			InvalidateRect(hwnd,NULL,0);
			break;
		case WM_DESTROY:
			DeleteDC(memDC);
			DeleteDC(memDC1);
			DeleteObject(hbit);
			DeleteObject(hbit1);
			DeleteObject(mybmp);
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hwnd,message,wParam,lParam);
	}
	return 0;
}


BOOL Show_Time(struct tm * newtime)
{
	
	int x,y,x1;
	int number[8];
	int i;
	y=point.y/2-27;
	x=point.x/2-32*4;
	
	number[0]=hour/10;
	number[1]=hour-hour/10*10;
	number[2]=min/10;
	number[3]=min-min/10*10;
	number[4]=sec/10;
	number[5]=sec-sec/10*10;

	SelectObject(memDC1,mybmp);
	for(i=0;i<=5;i++)
	{
		BitBlt(memDC,x,y,32,54,memDC1,number[i]*32,0,SRCAND);
		x=x+32;
		if(i==1|i==3)
		{	
			BitBlt(memDC,x,y,32,54,memDC1,10*32,0,SRCAND);
			x=x+32;
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -