怎么用vs写代码显示时间和日期

怎么用vs写代码显示时间和日期,第1张

首先需要确保手机桌面的页面上有足够的空间放置日期和时间插件,如果不够的话,需要整理好图标的位置。

接着,使用双指按图示方向将屏幕向内滑动,呼出桌面设置菜单。桌面设置菜单有三个选项,点击中间的【桌面工具】。在小插件列表中可以看到【时钟】插件(该插件是时间和日期合为一体的),选择自己喜欢的插件大小,然后按住该插件。按住插件不放,然后将插件拖动至要放置的位置松手,即可在手机桌面成功添加时间和日期插件。

graphic.h是dos的画图函数库,是在TC上的,如果你要用图形函数,要用到API,你可以参考一下《windows程序设计》这本书,里面有机械时钟的例子,你如果用了VC编译器,做在windows上的程序,一般用到API,或者MFC

代码我就没有了,不过楼主想学,要想弄明白windows的消息机制,消息循环,和消息响应函数

思想我可以说一下,你可以用画笔画线,在WM_TIMER下不断重绘窗口,做成秒针走的情况,具体的实现没有意义,都是一些API函数的应用

我将数字日期显示为窗口标题,在 vc++6.0 新建--win32 application 项目, 然后新建一个C++文件 输入如下代码

#include<windows.h>

#include<math.h>

#define TWOPI (2*3.14159)

#define IDTIMER 1

#define ANGLE TWOPI/360

LRESULT CALLBACK WindowProc(

HWND hwnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

)

int WINAPI WinMain(

HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow

)

{

TCHAR szClassName[] = TEXT("analogCloc")

MSG msg

HWND hwnd

WNDCLASS wndclass

wndclass.cbClsExtra = 0

wndclass.cbWndExtra = 0

wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH)

wndclass.hCursor = NULL

wndclass.hIcon = NULL

wndclass.hInstance = hInstance

wndclass.lpfnWndProc = WindowProc

wndclass.lpszClassName = szClassName

wndclass.lpszMenuName = NULL

wndclass.style = CS_HREDRAW | CS_VREDRAW

::RegisterClass(&wndclass)

hwnd = ::CreateWindow(szClassName,TEXT("Clock"),WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,CW_USEDEFAULT,

CW_USEDEFAULT,CW_USEDEFAULT,

NULL, NULL, hInstance, NULL)

::ShowWindow(hwnd,nCmdShow)

::UpdateWindow(hwnd)

while(::GetMessage(&msg,NULL,0,0))

{

::TranslateMessage(&msg)

::DispatchMessage(&msg)

}

return msg.wParam

}

void setISOTROPIC(HDC hdc,int cxClient,int cyClient)//设置映射模式,使之成为笛卡尔坐标系的映射模式

{

::SetMapMode(hdc,MM_ISOTROPIC)

::SetWindowExtEx(hdc,1000,1000,NULL) // 逻辑单位与设备单位比1/2

::SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL)

::SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL)// 竖坐标向上为正,下为负

}

void drawClock(HDC hdc)

{

int x, y, radius

::SelectObject(hdc,::CreateSolidBrush(RGB(1,148,138)))

for(int i=0i<360i+=6)

{

x = (int)(cos(TWOPI/360*i)*900)

y = (int)(sin(TWOPI/360*i)*900)

radius = !(i%5)?40:10

Ellipse(hdc,x-radius,y-radius,x+radius,y+radius)

}

}

void drawHands(HDC hdc,SYSTEMTIME *pst,BOOL hChange)

{

int radius[3] = {500,700,850}

int angle[3]

angle[0] = pst->wHour*30+pst->wMinute/12*6

angle[1] = pst->wMinute*6

angle[2] = pst->wSecond*6

for(int i=hChange?0:2i<3i++)

{

MoveToEx(hdc,0,0,NULL)

LineTo(hdc,(int)(radius[i]*cos(ANGLE*(90-angle[i]))),

(int)(radius[i]*sin(ANGLE*(90-angle[i]))))

}

}

LRESULT CALLBACK WindowProc(

HWND hwnd,

UINT message,

WPARAM wParam,

LPARAM lParam

)

{

TCHAR time[40]

PAINTSTRUCT ps

HDC hdc

static int cxClient, cyClient

SYSTEMTIME st

static SYSTEMTIME preSt

BOOL hChange

switch(message)

{

case WM_CREATE:

::SetTimer(hwnd,IDTIMER,1000,NULL)

::GetLocalTime(&st)

wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),

st.wYear,

st.wMonth,

st.wDay,

st.wHour,

st.wMinute,

st.wSecond)

SetWindowText(hwnd,time)

preSt = st

return 0

case WM_SIZE:

cxClient = LOWORD(lParam)

cyClient = HIWORD(lParam)

return 0

case WM_TIMER:

::GetLocalTime(&st) //每次都要获取当前时间

hChange = st.wHour!=preSt.wHour||st.wMinute!=preSt.wMinute

hdc = GetDC(hwnd)

setISOTROPIC(hdc,cxClient,cyClient)

::SelectObject(hdc,::GetStockObject(WHITE_PEN))

drawHands(hdc,&preSt,hChange)

::SelectObject(hdc,::GetStockObject(BLACK_PEN))

drawHands(hdc,&st,TRUE)

ReleaseDC(hwnd,hdc)

wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),

st.wYear,

st.wMonth,

st.wDay,

st.wHour,

st.wMinute,

st.wSecond)

SetWindowText(hwnd,time)

preSt = st // 更新完毕后记录前一次的状态 return 0

case WM_KEYDOWN:

case WM_CHAR:

::DestroyWindow(hwnd)

return 0

case WM_PAINT:

hdc = ::BeginPaint(hwnd,&ps)

setISOTROPIC(hdc,cxClient,cyClient)

drawClock(hdc)

drawHands(hdc,&preSt,TRUE)

::EndPaint(hwnd,&ps)

return 0

case WM_DESTROY:

::PostQuitMessage(0)

return 0

}

return DefWindowProc(hwnd,message,wParam,lParam)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12201106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存