由于该项目是win32项目,且在VS2019上实现的,故现在简单一下在VS2019上如何创建与为win32项目相同的windows桌面向导程序。
(1)首先打开vs2019创建新项目
(2)然后选择Windows桌面向导程序
(3)然后点击下一步创建桌面向导程序,点击将解决方案与项目放在放在同一目录中
(4)然后点击创建,在弹出来的小窗口中将应用控制程序修改为桌面应用程序,再点击空项目,最后确定即可
这样就创建了与win32项目相当的Windows桌面想到程序。
现在我们来实现时钟
由于在实现时用ACLLIB图形库,该库中的acllib.h和acllib.c文件的程序过长,故不将这两个文件的代码放在此,我们只将我们定义的是时钟类的代码和主程序的代码放在这里,具体的代码如下,ACLLIB图形库的资源我将会上传,或者自己也可以在网上寻找。
头文件
#pragma once
#ifndef CLOCK_H_
#define CLOCK_H_
class Clock {
private:
int hour;
int mimute;
int second;
public:
Clock();
Clock(int h, int m, int s);
Clock(Clock& c);
~Clock() { }
void set(int a, int b, int c);
//void display();
void Addonesecond();
void DrawEllipseClock(int x, int y, int width, int height);
};
#endif
头文件中相关方法的定义
#include<iostream>
#include"clock.h"
#include"acllib.h"
#include<math.h>
#include<time.h>
float Rad_to_deg = 45.0 / atan(1);
Clock::Clock() {
time_t t;
struct tm tt;
t = time(NULL);//当前时间
localtime_s(&tt, &t);
hour = tt.tm_hour;
mimute = tt.tm_min;
second = tt.tm_sec;
}
Clock::Clock(int h, int m, int s) {
hour = (h + (m + s / 60) / 60) % 24;
mimute = (m + s / 60) % 60;
second = s % 60;
}
Clock::Clock(Clock& c) :hour(c.hour), mimute(c.mimute), second(c.second) {
}
/*void Clock::display() {
std::cout << "当前时间为:" << hour << "h" << mimute << "m" << second << "s\n";
}*/
void Clock::set(int a, int b, int c) {
second = c % 60;
mimute = (b + c / 60) % 60;
hour = (a + (b + c / 60) / 60) % 24;
}
void Clock::Addonesecond() {
second++;
if (second >= 60) {
second = 0;
mimute++;
if (mimute >= 60) {
mimute = 0;
hour++;
if (hour >= 12)
hour = 0;
}
}
}
void Clock::DrawEllipseClock(int x, int y, int width, int height) {
//定位圆心
int ox = (x + width) / 2;
int oy = (y + height) / 2;
int min = width < height ? width : height;//以矩形中长或宽中长度较小者来画椭圆
int hl = min / 4 - min / 7;//时针长度
int ml = min / 3 - min / 6;//份针长度
int sl = min / 2 - min / 5;//秒针长度
int i;
beginPaint();//开始绘图
clearDevice();//清屏
//绘制椭圆
setPenWidth(2);//画笔宽度为2
setPenColor(BLACK);//画笔绘制的颜色为黑色
setBrushColor(WHITE);//填充颜色为白色
ellipse(x, y, width, height);//绘制椭圆
//绘制表盘
setPenWidth(2);//画笔宽度为2
setPenColor(BLACK);//画笔绘制的颜色为黑色
int len1 = min / 2 - min / 8, len2 = len1 + 10;//len1为圆心移动到绘制点的距离,len2为所画刻度的长度
for (i = 0; i < 12; i++) {//绘制12个刻度
moveTo(ox + len1 * sin((180 - i * 30)/Rad_to_deg), oy + len1 * cos((180 - i * 30)/Rad_to_deg));//将绘制点移动到参数的位置
lineTo(ox + len2 * sin((180 - i * 30) / Rad_to_deg), oy + len2 * cos((180 - i * 30) / Rad_to_deg));//绘制刻度
}
//绘制时针
setPenWidth(8);//画笔宽度为2
setPenColor(BLACK);//画笔绘制的颜色为黑色
moveTo(ox, oy);
lineTo(ox + hl * sin((180 - hour * 30) / Rad_to_deg), oy + hl * cos((180 - hour * 30) / Rad_to_deg));
//绘制分针
setPenWidth(4);//画笔宽度为2
setPenColor(GREEN);//画笔绘制的颜色为黑色
moveTo(ox, oy);
lineTo(ox + ml * sin((180 - mimute * 6) / Rad_to_deg), oy + ml * cos((180 - mimute* 6) / Rad_to_deg));
//绘制秒针
setPenWidth(2);//画笔宽度为2
setPenColor(BLUE);//画笔绘制的颜色为黑色
moveTo(ox, oy);
lineTo(ox + sl * sin((180 - second * 6) / Rad_to_deg), oy + sl * cos((180 - second * 6) / Rad_to_deg));
endPaint();
}
主程序
#include<iostream>
#include<Windows.h>
#include"clock.h"
#include"acllib.h"
const int Widthwin = 600, Heightwin = 600;
void TimerEvent(int id);
Clock c;
int Setup() {
initWindow("Headsome Clock", DEFAULT, DEFAULT, Widthwin, Heightwin);
c.DrawEllipseClock(10, 10, Widthwin - 20, Heightwin - 20);
registerTimerEvent(TimerEvent);
startTimer(1, 1000);
return 0;
}
void TimerEvent(int id) {
if (id == 1) {
c.Addonesecond();
c.DrawEllipseClock(10, 10, Widthwin - 20, Heightwin - 20);
}
}
下面是运行结果:
注:该时钟会动,这里只截取一张图片