急!!C++编程,高手进!!用Vc++6.0编,模拟时钟转动的程序,要求如下

急!!C++编程,高手进!!用Vc++6.0编,模拟时钟转动的程序,要求如下,第1张

我将数字日期显示升帆为窗口标题,在 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)

}

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

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

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

程序代码如下:

#include<iostream>

#include<windows.h>

using namespace std

class Clock{

public:

Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){

}

void displayTime()

private:

short h

short m

short s

}void Clock::displayTime(){

while(true){

cout<<h<<':'<<m<<':'<<s<<" "

Sleep(1000)

cout<<'\r'

if(!(s=++s%60))

if(!(m=++m%60))

h=++h%24

}

} int main()

{

Clock A(23,59,55)

A.displayTime()

return 0

}

扩展资料:

定义一个日期类,包括年、月、日三个成员变量,显示日期的方法如下数乱:裂毕余肆滚

publicclassDemo{

publicstaticvoidmain(String[]args){

Datedate1=newDate(1994,5,22)

date1.showInfo()

Datedate2=newDate()

date2.year=1995

date2.month=6

date2.day=29

date2.showInfo()

}

}

//日期类:

publicclassDate{

intyear

intmonth

intday

//构造方法

publicDate(intyear,intmonth,intday){

this.year=year

this.month=month

this.day=day

}

publicDate(){

}

publicvoidshowInfo(){

System.out.println(year+"年"+month+"月"+day+"日")

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存