用vc++6.0C语言编写模拟时钟,大神,求助

用vc++6.0C语言编写模拟时钟,大神,求助,第1张

可以参考一下下面的代码:

#include<stdio.h>

#include <graphics.h>

#include <conio.h>

#include <math.h>

#define PI 3.14159

#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300

#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240

#define d(a,b,c) X(a,b,c)Y(a,b,c)line(300,240,x,y)

void Draw(int hour, int minute, int second)

{

double a_hour, a_min, a_sec// 时、分、秒针的弧度值

int x_hour, y_hour, x_min, y_min, x_sec, y_sec// 时、分、秒针的末端位置

// 计算时、分、秒针的弧度值

a_sec = second * 2 * PI / 60

a_min = minute * 2 * PI / 60 + a_sec / 60

a_hour= hour * 2 * PI / 12 + a_min / 12

// 计算时、分、秒针的末端位置

x_sec = 320 + (int)(120 * sin(a_sec))

y_sec = 240 - (int)(120 * cos(a_sec))

x_min = 320 + (int)(100 * sin(a_min))

y_min = 240 - (int)(100 * cos(a_min))

x_hour= 320 + (int)(70 * sin(a_hour))

y_hour= 240 - (int)(70 * cos(a_hour))

// 画时针

setlinestyle(PS_SOLID, NULL, 10)

setcolor(WHITE)

line(320, 240, x_hour, y_hour)

// 画分针

setlinestyle(PS_SOLID, NULL, 6)

setcolor(LIGHTGRAY)

line(320, 240, x_min, y_min)

// 画秒针

setlinestyle(PS_SOLID, NULL, 2)

setcolor(RED)

line(320, 240, x_sec, y_sec)

}

void main()

{

int i,l,x1,x2,y1,y2

initgraph(640, 480)// 初始化 640 x 480 的绘图窗口 // 绘制一个简单的表盘

circle(320, 240, 2)

circle(320, 240, 160)

outtextxy(296, 300, "Quan")// 设置 XOR 绘图模式

for(i=0i<60i++) /*划钟点上的短线*/

{

if(i%5==0)

l=15

else

l=5

x1=160*sin(i*6*PI/180)+320

y1=160*cos(i*6*PI/180)+240

x2=(160-l)*sin(i*6*PI/180)+320

y2=(160-l)*cos(i*6*PI/180)+240

line(x1,y1,x2,y2)

}

setwritemode(R2_XORPEN)// 设置 XOR 绘图模式

// 绘制表针

SYSTEMTIME ti// 定义变量保存当前时间

while(!kbhit()) // 按任意键退出钟表程序

{

GetLocalTime(&ti)

Draw(ti.wHour, ti.wMinute, ti.wSecond)// 画表针

Sleep(1000)// 延时 1 秒

Draw(ti.wHour, ti.wMinute, ti.wSecond)// 擦表针(擦表针和

}

closegraph()

}

我将数字日期显示为窗口标题,在 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/11324591.html

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

发表评论

登录后才能评论

评论列表(0条)

保存