#include <windows.h>这是完整代码。。
#define ID_TIMER 1 //定义定时器的ID号为1
#include "resource.h"
#include <math.h>
//下面是数字时钟的一些必要函数
void DisplayDigit(HDC hdc,int iNumber)
{
//10行7列的BOOL型变量
static BOOL fSevenSegment[10][7]={
1,1,1,0,1,1,1,//0
0,0,1,0,0,1,0,//1
1,0,1,1,1,0,1,//2
1,0,1,1,0,1,1,//3
0,1,1,1,0,1,0,//4
1,1,0,1,0,1,1,//5
1,1,0,1,1,1,1,//6
1,0,1,0,0,1,0,//7
1,1,1,1,1,1,1,//8
1,1,1,1,0,1,1 //9
};
//定义一个7行6列的点数组,一共有42个点
static POINT ptSegment[7][6]={
7,6, 11,2, 31,2, 35,6, 31,10, 11,10, //0
6,7, 10,11, 10,31, 6,35, 2,31, 2,11, //1
36,7, 40,11, 40,31, 36,35, 32,31, 32,11, //2
7,36, 11,32, 31,32, 35,36, 31,40, 11,40, //3
6,37, 10,41, 10,61, 6,65, 2,61, 2,41, //4
36,37, 40,41, 40,61, 36,65, 32,61, 32,41, //5
7,66, 11,62, 31,62, 35,66, 31,70, 11,70 //6
};
int iSeg;
for(iSeg=0;iSeg<7;iSeg++)
{
if(fSevenSegment[iNumber][iSeg])
Polygon(hdc,ptSegment[iSeg],6);
}
}
void DisplayTwoDigits(HDC hdc, int iNumber,BOOL fSuppress)
{
if(!fSuppress||(iNumber/10!=0))
DisplayDigit(hdc,iNumber/10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
DisplayDigit(hdc,iNumber%10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
}
//
void DisplayColon(HDC hdc)
{
//2行4列的一个点数组
POINT ptColon[2][4]={
2,21, 6,17, 10,21, 6,25,
2,51, 6,47, 10,51, 6,55
};
Polygon(hdc,ptColon[0],4);
Polygon(hdc,ptColon[1],4);
OffsetWindowOrgEx(hdc,-12,0,NULL);
}
//
void DisplayTime1(HDC hdc,BOOL f24Hour,BOOL fSuppress)
{
SYSTEMTIME st;
GetLocalTime(&st);
if(f24Hour)
DisplayTwoDigits(hdc,st.wHour,fSuppress);
else
DisplayTwoDigits(hdc,(st.wHour%=12)?st.wHour:12,fSuppress);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wMinute,FALSE);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wSecond,FALSE);
}
LRESULT __stdcall WndProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
char szWindowClass[] = "Clock";
// 注册窗口类
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_MAIN);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); // 此域可以是一个系统定义的颜色值
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
::RegisterClassEx(&wcex);
// 创建并显示主窗口
HWND hWnd = ::CreateWindowEx(NULL, szWindowClass, "时钟",
WS_POPUP|WS_SYSMENU|WS_SIZEBOX, 200, 200, 400, 600, NULL, NULL, hInstance, NULL);
::ShowWindow(hWnd, nShowCmd);
::UpdateWindow(hWnd);
// 进入消息循环
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return 1;
}
//////////////////////////////////////////////////////////////////////////////////////
// 消息处理代码
#define IDT_CLOCK 1
const int IDM_HELP = 100;
const int IDM_TOPMOST = 101;
// 实现函数
void SetIsotropic(HDC hdc, int cxClient, int cyClient);
void DrawClockFace(HDC hdc);
void DrawHand(HDC hdc, int nLength, int nWidth, int nDegrees, COLORREF clrColor);
// 上一次Windows通知时的时间
static int s_nPreHour; // 小时
static int s_nPreMinute; // 分钟
static int s_nPreSecond; // 秒
// 窗口客户区的大小
static int s_cxClient;
static int s_cyClient;
// 是否位于最顶层
static BOOL s_bTopMost;
LRESULT __stdcall WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//数字时钟的变量
static BOOL f24Hour,fSuppress;
static HBRUSH hBrushRed;
static int cxClient,cyClient;
HDC hdc;
PAINTSTRUCT ps;
TCHAR szBuffer[2];
switch(nMsg)
{
case WM_CREATE:
{
//这是做数字时钟用的
hBrushRed=CreateSolidBrush(RGB(255,0,0));//定义一个实心的红色画刷
SetTimer(hWnd,ID_TIMER,1000,NULL);//为句柄为hwnd的窗口设置一个定时器
// 设置时间
SYSTEMTIME time;
::GetLocalTime(&time);
s_nPreHour = time.wHour%12;
s_nPreMinute = time.wMinute;
s_nPreSecond = time.wSecond;
// 创建定时器
::SetTimer(hWnd, IDT_CLOCK, 1000, NULL);
return 0;
}
//做数字时钟用的
case WM_SETTINGCHANGE:
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITIME,szBuffer,2);
f24Hour=(szBuffer[0]=='1');
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITLZERO,szBuffer,2);
fSuppress=(szBuffer[0]=='0');
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case WM_SIZE:
{
s_cxClient = LOWORD(lParam);
s_cyClient = HIWORD(lParam);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(hWnd, &ps);
// 设置坐标系
SetIsotropic(hdc, s_cxClient, s_cyClient);
// 绘制时钟外观
DrawClockFace(hdc);
// 绘制指针
// 经过1个小时时针走30度(360/12),经过1分钟时针走0.5度(30/60)
DrawHand(hdc, 200, 8, s_nPreHour*30 + s_nPreMinute/2, RGB(0, 0, 0));
// 经过1分钟分针走6度(360/60)
DrawHand(hdc, 400, 6, s_nPreMinute*6, RGB(0, 0, 0));
// 经过1秒钟秒针走6度(360/60)
DrawHand(hdc, 400, 1, s_nPreSecond*6, RGB(0, 0, 0));
SetMapMode(hdc,MM_ISOTROPIC);//设置映射模式
SetWindowExtEx(hdc,600,100,NULL);
SetViewportExtEx(hdc,cxClient/3,cyClient/3,NULL);
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,hBrushRed);
DisplayTime1(hdc,f24Hour,fSuppress);
::EndPaint(hWnd, &ps);
return 0;
}
case WM_TIMER:
{
// 如果窗口处于最小化状态就什么也不做
if(::IsIconic(hWnd)) // IsIconic函数用来判断窗口是否处于最小化状态
return 0;
// 取得系统时间
SYSTEMTIME time;
::GetLocalTime(&time);
// 建立坐标系
HDC hdc = ::GetDC(hWnd);
SetIsotropic(hdc, s_cxClient, s_cyClient);
// 以COLOR_3DFACE为背景色就可以擦除指针了(因为窗口的背景色也是COLOR_3DFACE)
COLORREF crfColor = ::GetSysColor(COLOR_3DFACE);
// 如果分钟改变的话就擦除时针和分针
if(time.wMinute != s_nPreMinute)
{
// 擦除时针和分针
DrawHand(hdc, 200, 8, s_nPreHour*30 + s_nPreMinute/2, crfColor);
DrawHand(hdc, 400, 6, s_nPreMinute*6, crfColor);
s_nPreHour = time.wHour;
s_nPreMinute = time.wMinute;
}
// 如果秒改变的话就擦除秒针,然后重画所有指针
if(time.wSecond != s_nPreSecond)
{
// 擦除秒针
DrawHand(hdc, 400, 1, s_nPreSecond*6, crfColor);
// 重画所有指针
DrawHand(hdc, 400, 1, time.wSecond*6, RGB(0, 0, 0));
DrawHand(hdc, 200, 8, time.wHour*30 + time.wMinute/2, RGB(0, 0, 0));
DrawHand(hdc, 400, 6, time.wMinute*6, RGB(0, 0, 0));
s_nPreSecond = time.wSecond;
}
//定时器事件,做数字时钟用的
InvalidateRect(hWnd,NULL,TRUE);
return 0;
}
case WM_CONTEXTMENU:
POINT pt; pt.x = LOWORD(lParam); pt.y = HIWORD(lParam);
{
// 取得系统菜单的句柄
HMENU hSysMenu = ::GetSystemMenu(hWnd, FALSE);
// 弹出系统菜单
int nID = ::TrackPopupMenu(hSysMenu,TPM_LEFTALIGN|TPM_RETURNCMD,
pt.x, pt.y, 0, hWnd, NULL);
if(nID > 0)
::SendMessage(hWnd, WM_SYSCOMMAND, nID, 0);
return 0;
}
case WM_CLOSE:
{
::KillTimer(hWnd, IDT_CLOCK);
::DestroyWindow(hWnd);
return 0;
}
case WM_DESTROY:
{
KillTimer(hWnd,ID_TIMER);
DeleteObject(hBrushRed);
::PostQuitMessage(0);
return 0;
}
case WM_NCHITTEST:
{
UINT nHitTest;
nHitTest = ::DefWindowProc(hWnd, WM_NCHITTEST, wParam, lParam);
if (nHitTest == HTCLIENT &&
::GetAsyncKeyState(MK_LBUTTON) < 0) // 如果鼠标左键按下,GetAsyncKeyState函数的返回值小于0
nHitTest = HTCAPTION;
return nHitTest;
}
case WM_SYSCOMMAND:
int nID = wParam;
{
if(nID == IDM_HELP)
{
::MessageBox(hWnd, "一个时钟的例子", "时钟", 0);
}
else if(nID == IDM_TOPMOST)
{
HMENU hSysMenu = ::GetSystemMenu(hWnd, FALSE);
if(s_bTopMost)
{
::CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_UNCHECKED);
::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);
s_bTopMost = FALSE;
}
else
{
::CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_CHECKED);
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);
s_bTopMost = TRUE;
}
}
return ::DefWindowProc(hWnd, WM_SYSCOMMAND, nID, 0);
}
}
return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}
void SetIsotropic(HDC hdc, int cx, int cy)
{
::SetMapMode(hdc, MM_ISOTROPIC);
::SetWindowExtEx(hdc, 1000, 1000, NULL);
::SetViewportExtEx(hdc, cx, -cy, NULL);
::SetViewportOrgEx(hdc, cx/2, cy/2, NULL);
}
// 绘制时钟的外观
void DrawClockFace(HDC hdc)
{
const int SQUARESIZE = 20;
static POINT pt[] =
{
0, 450, // 12点
225, 390, // 1点
390, 225, // 2点
450, 0, // 3点
390, -225, //... 下面的坐标是上面的点的对称点(以X轴、Y轴或原点对称)
225, -390,
0, -450,
-225, -390,
-390, -225,
-450, 0,
-390, 225,
-225, 390
};
// 选择一个黑色的画刷
::SelectObject(hdc, ::GetStockObject(BLACK_BRUSH));
// 画12个圆
for(int i=0; i<12; i++)
{
::Ellipse(hdc, pt[i].x - SQUARESIZE, pt[i].y + SQUARESIZE,
pt[i].x + SQUARESIZE, pt[i].y - SQUARESIZE);
}
}
// 指针的长度、宽度、相对于0点偏移的角度、颜色分别由参数nLength、nWidth、nDegrees、clrColor指定
void DrawHand(HDC hdc, int nLength, int nWidth, int nDegrees, COLORREF clrColor)
{
// 将角度nDegrees转化成弧度 . 2*3.1415926/360 == 0.0174533
double nRadians = (double)nDegrees*0.0174533;
// 计算坐标
POINT pt[2];
pt[0].x = (int)(nLength*sin(nRadians));
pt[0].y = (int)(nLength*cos(nRadians));
pt[1].x = -pt[0].x/5;
pt[1].y = -pt[0].y/5;
// 创建画笔,并选如DC结构中
HPEN hPen = ::CreatePen(PS_SOLID, nWidth, clrColor);
HPEN hOldPen = (HPEN)::SelectObject(hdc, hPen);
// 画线
::MoveToEx(hdc, pt[0].x, pt[0].y, NULL);
::LineTo(hdc, pt[1].x, pt[1].y);
::SelectObject(hdc, hOldPen);
::DeleteObject(hPen);
}
1 个解决方案
#1
我是把下面的代码,融入进去的,这个数字是正过来的。。但是融入我的代码里就出现问题了
#include <windows.h>
#define ID_TIMER 1 //定义定时器的ID号为1
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
//WinMain函数
int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
PSTR lpCmdLine, // command line
int iCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("DigClock");
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,
TEXT("Clock"),
WS_OVERLAPPEDWINDOW,
490,
300,
300,
200,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//
void DisplayDigit(HDC hdc,int iNumber)
{
//10行7列的BOOL型变量
static BOOL fSevenSegment[10][7]={
1,1,1,0,1,1,1,//0
0,0,1,0,0,1,0,//1
1,0,1,1,1,0,1,//2
1,0,1,1,0,1,1,//3
0,1,1,1,0,1,0,//4
1,1,0,1,0,1,1,//5
1,1,0,1,1,1,1,//6
1,0,1,0,0,1,0,//7
1,1,1,1,1,1,1,//8
1,1,1,1,0,1,1 //9
};
//定义一个7行6列的点数组,一共有42个点
static POINT ptSegment[7][6]={
7,6, 11,2, 31,2, 35,6, 31,10, 11,10, //0
6,7, 10,11, 10,31, 6,35, 2,31, 2,11, //1
36,7, 40,11, 40,31, 36,35, 32,31, 32,11, //2
7,36, 11,32, 31,32, 35,36, 31,40, 11,40, //3
6,37, 10,41, 10,61, 6,65, 2,61, 2,41, //4
36,37, 40,41, 40,61, 36,65, 32,61, 32,41, //5
7,66, 11,62, 31,62, 35,66, 31,70, 11,70 //6
};
int iSeg;
for(iSeg=0;iSeg<7;iSeg++)
{
if(fSevenSegment[iNumber][iSeg])
Polygon(hdc,ptSegment[iSeg],6);
}
}
//
void DisplayTwoDigits(HDC hdc, int iNumber,BOOL fSuppress)
{
if(!fSuppress||(iNumber/10!=0))
DisplayDigit(hdc,iNumber/10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
DisplayDigit(hdc,iNumber%10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
}
//
void DisplayColon(HDC hdc)
{
//2行4列的一个点数组
POINT ptColon[2][4]={
2,21, 6,17, 10,21, 6,25,
2,51, 6,47, 10,51, 6,55
};
Polygon(hdc,ptColon[0],4);
Polygon(hdc,ptColon[1],4);
OffsetWindowOrgEx(hdc,-12,0,NULL);
}
//
void DisplayTime(HDC hdc,BOOL f24Hour,BOOL fSuppress)
{
SYSTEMTIME st;
GetLocalTime(&st);
if(f24Hour)
DisplayTwoDigits(hdc,st.wHour,fSuppress);
else
DisplayTwoDigits(hdc,(st.wHour%=12)?st.wHour:12,fSuppress);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wMinute,FALSE);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wSecond,FALSE);
}
//窗口的消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
static BOOL f24Hour,fSuppress;
static HBRUSH hBrushRed;
static int cxClient,cyClient;
HDC hdc;
PAINTSTRUCT ps;
TCHAR szBuffer[2];
switch(message)
{
case WM_CREATE:
hBrushRed=CreateSolidBrush(RGB(255,0,0));//定义一个实心的红色画刷
SetTimer(hwnd,ID_TIMER,1000,NULL);//为句柄为hwnd的窗口设置一个定时器
case WM_SETTINGCHANGE:
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITIME,szBuffer,2);
f24Hour=(szBuffer[0]=='1');
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITLZERO,szBuffer,2);
fSuppress=(szBuffer[0]=='0');
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
//定时器事件
case WM_TIMER:
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode(hdc,MM_ISOTROPIC);//设置映射模式
SetWindowExtEx(hdc,276,72,NULL);
SetViewportExtEx(hdc,cxClient/2,cyClient/2,NULL);
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,hBrushRed);
DisplayTime(hdc,f24Hour,fSuppress);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd,ID_TIMER);
DeleteObject(hBrushRed);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
#1
我是把下面的代码,融入进去的,这个数字是正过来的。。但是融入我的代码里就出现问题了
#include <windows.h>
#define ID_TIMER 1 //定义定时器的ID号为1
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
//WinMain函数
int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
PSTR lpCmdLine, // command line
int iCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("DigClock");
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,
TEXT("Clock"),
WS_OVERLAPPEDWINDOW,
490,
300,
300,
200,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//
void DisplayDigit(HDC hdc,int iNumber)
{
//10行7列的BOOL型变量
static BOOL fSevenSegment[10][7]={
1,1,1,0,1,1,1,//0
0,0,1,0,0,1,0,//1
1,0,1,1,1,0,1,//2
1,0,1,1,0,1,1,//3
0,1,1,1,0,1,0,//4
1,1,0,1,0,1,1,//5
1,1,0,1,1,1,1,//6
1,0,1,0,0,1,0,//7
1,1,1,1,1,1,1,//8
1,1,1,1,0,1,1 //9
};
//定义一个7行6列的点数组,一共有42个点
static POINT ptSegment[7][6]={
7,6, 11,2, 31,2, 35,6, 31,10, 11,10, //0
6,7, 10,11, 10,31, 6,35, 2,31, 2,11, //1
36,7, 40,11, 40,31, 36,35, 32,31, 32,11, //2
7,36, 11,32, 31,32, 35,36, 31,40, 11,40, //3
6,37, 10,41, 10,61, 6,65, 2,61, 2,41, //4
36,37, 40,41, 40,61, 36,65, 32,61, 32,41, //5
7,66, 11,62, 31,62, 35,66, 31,70, 11,70 //6
};
int iSeg;
for(iSeg=0;iSeg<7;iSeg++)
{
if(fSevenSegment[iNumber][iSeg])
Polygon(hdc,ptSegment[iSeg],6);
}
}
//
void DisplayTwoDigits(HDC hdc, int iNumber,BOOL fSuppress)
{
if(!fSuppress||(iNumber/10!=0))
DisplayDigit(hdc,iNumber/10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
DisplayDigit(hdc,iNumber%10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
}
//
void DisplayColon(HDC hdc)
{
//2行4列的一个点数组
POINT ptColon[2][4]={
2,21, 6,17, 10,21, 6,25,
2,51, 6,47, 10,51, 6,55
};
Polygon(hdc,ptColon[0],4);
Polygon(hdc,ptColon[1],4);
OffsetWindowOrgEx(hdc,-12,0,NULL);
}
//
void DisplayTime(HDC hdc,BOOL f24Hour,BOOL fSuppress)
{
SYSTEMTIME st;
GetLocalTime(&st);
if(f24Hour)
DisplayTwoDigits(hdc,st.wHour,fSuppress);
else
DisplayTwoDigits(hdc,(st.wHour%=12)?st.wHour:12,fSuppress);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wMinute,FALSE);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wSecond,FALSE);
}
//窗口的消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
static BOOL f24Hour,fSuppress;
static HBRUSH hBrushRed;
static int cxClient,cyClient;
HDC hdc;
PAINTSTRUCT ps;
TCHAR szBuffer[2];
switch(message)
{
case WM_CREATE:
hBrushRed=CreateSolidBrush(RGB(255,0,0));//定义一个实心的红色画刷
SetTimer(hwnd,ID_TIMER,1000,NULL);//为句柄为hwnd的窗口设置一个定时器
case WM_SETTINGCHANGE:
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITIME,szBuffer,2);
f24Hour=(szBuffer[0]=='1');
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITLZERO,szBuffer,2);
fSuppress=(szBuffer[0]=='0');
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
//定时器事件
case WM_TIMER:
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetMapMode(hdc,MM_ISOTROPIC);//设置映射模式
SetWindowExtEx(hdc,276,72,NULL);
SetViewportExtEx(hdc,cxClient/2,cyClient/2,NULL);
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,hBrushRed);
DisplayTime(hdc,f24Hour,fSuppress);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd,ID_TIMER);
DeleteObject(hBrushRed);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}