当前位置:网站首页>C语言实现-华为太空人手表
C语言实现-华为太空人手表
2022-08-04 05:49:00 【cpp编程】
- 项目介绍
价值6000万的太空人手表:

- 项目准备
- VS/VC++的任意版本 (VC6, 2010, 2012, 2013, 2015,2017,2019)
- easyx图形库
- 项目实现
- 创建项目
创建空项目
- 实现项目架构
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
void init() {
}
void drawClockBG() {
}
void drawTime() {
}
int main()
{
init();
drawClockBG(); //绘制表盘
while (1) {
// to do 绘制太空人
drawTime(); //绘制时间
Sleep(30); // 帧等待
}
closegraph();
return 0;
}- 创建窗口
void init() {
// 创建窗口
initgraph(
GetSystemMetrics(SM_CXSCREEN), //桌面宽度
GetSystemMetrics(SM_CYSCREEN));//桌面高度
}类似的全透明窗口:360助手。。。
void setTransparentWindow() {
// 去掉窗口的标题栏
HWND hwnd = GetHWnd(); //获取当前窗口句柄
SetWindowLong( //设置窗口属性说
hwnd,
GWL_STYLE, //设定一个新的窗口风格。
//GetWindowLong 获取指定串口的属性
GetWindowLong(hwnd, GWL_STYLE) - WS_CAPTION);//WS_CAPTION带标题栏的窗口风格
// 设置窗口的位置和大小,并设置为顶层窗口
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);
//设置透明
// 把当前窗口设置为分层窗口
// GWL_EXSTYLE 窗口的扩展样式, EX:扩展
// WS_EX_LAYERED 分层窗口
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
// 设置分层窗口的透明度
SetLayeredWindowAttributes(
hwnd,
RGB(0, 0, 0), //指定需要透明的背景颜色值
0, //设置透明度,0表示完全透明,255表示不透明
LWA_COLORKEY); // 透明方式:窗体中的所有颜色为参数2的地方将变为透明
}
void init() {
// 创建窗口
initgraph(
GetSystemMetrics(SM_CXSCREEN), //桌面宽度
GetSystemMetrics(SM_CYSCREEN));//桌面高度
setTransparentWindow();
}测试效果。
3.3 加载图片资源
拷贝图片资源到项目目录中:
![]()
加载图片
IMAGE imgs[71];
IMAGE sz[11];
IMAGE sz2[10];
void loadImages() {
// 预加载太空人图片
char imgName[64];
for (int i = 0; i < 71; i++) {
sprintf_s(imgName, sizeof(imgName), "img/human_%04d_图层 %d.jpg", i, 71 - i);
loadimage(&imgs[i], imgName, 105, 105, true);
}
// 预加载数字图片
for (int i = 0; i < 10; i++) {
sprintf_s(imgName, sizeof(imgName), "img/%d.jpg", i);
loadimage(&sz[i], imgName, 40, 70, true);
loadimage(&sz2[i], imgName, 20, 35, true);
}
loadimage(&sz[10], "img/rect.jpg", 10, 10, true);
}
void init() {
// 创建窗口
initgraph(
GetSystemMetrics(SM_CXSCREEN), //桌面宽度
GetSystemMetrics(SM_CYSCREEN));//桌面高度
setTransparentWindow();
loadImages(); //加载图片资源
}设置新的坐标体系

void init() {
// 创建窗口
initgraph(
GetSystemMetrics(SM_CXSCREEN), //桌面宽度
GetSystemMetrics(SM_CYSCREEN));//桌面高度
setTransparentWindow();
loadImages(); //加载图片资源
// 设置坐标原点
setorigin(
GetSystemMetrics(SM_CXSCREEN) - 200,
200);
}绘制表盘
void drawClockBG() {
// 绘制表盘
IMAGE bg;
loadimage(&bg, "img/bg3.jpg", 400, 400, true);
putimage(-200, -200, &bg);
}测试,发现有毛刺。
解决毛刺:
void drawClockBG() {
// 绘制表盘
IMAGE bg;
loadimage(&bg, "img/bg3.jpg", 400, 400, true);
putimage(-200, -200, &bg);
// 画一个圆环,去除毛刺
setlinecolor(BLACK); // 设置线条颜色为黑色
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 8); // 设置线条样式为宽度 10 的实线,端点是平的
circle(0, 0, 202);
}绘制太空飞船
确定坐标位置

绘制太空人
int main()
{
init();
drawClockBG(); //绘制表盘
int k = 0;
while (1) {
// to do 绘制太空人
putimage(-50, -15, &imgs[k]);
k = (k + 1) % 71;
drawTime(); //绘制时间
Sleep(30); // 帧等待
}
closegraph();
return 0;
}绘制时间
导入工具库函数

#include "date_tool.h"
void drawTime() {
static int last_hour = -1, last_minute = -1, last_second = -1;
int hour, minute, second;
getTime(&hour, &minute, &second);
int h1 = hour / 10;
int h2 = hour % 10;
if (last_hour != hour) putimage(-130, -90, &sz[h1]); //-130, -90
if (last_hour != hour) putimage(-80, -90, &sz[h2]); //-80, -90
static bool first = true;
if (first) putimage(-30, -75, &sz[10]);//-30,-75
if (first) putimage(-30, -45, &sz[10]);//-30,-45
int m1 = minute / 10;
int m2 = minute % 10;
if (last_minute != minute) putimage(-10, -90, &sz[m1]);//-10, -90
if (last_minute != minute) putimage(40, -90, &sz[m2]);//40,-90
if (first || last_second / 10 != second / 10) putimage(90, -55, &sz2[second / 10]); //90,-55
if (first || last_second % 10 != second % 10) putimage(120, -55, &sz2[second % 10]); //120,-55
first = false;
last_hour = hour;
last_minute = minute;
last_second = second;
}绘制日期
void drawDate() {
char str[16] = "";
int year, month, day;
getDate(&year, &month, &day, str);
setbkcolor(RGB(223, 230, 240));
settextcolor(RGB(15, 15, 15)); //设置字体颜色
settextstyle(25, 0, "微软雅黑"); //设置字体样式
outtextxy(70, 0, str); //在指定位置输出文本
getWeek(year, month, day, str);
outtextxy(65, 30, str);
sprintf(str, "%d-%d", month, day);
outtextxy(110, 30, str);
}
void drawTime() {
static int last_hour = -1, last_minute = -1, last_second = -1;
int hour, minute, second;
getTime(&hour, &minute, &second);
int h1 = hour / 10;
int h2 = hour % 10;
if (last_hour != hour) putimage(-130, -90, &sz[h1]); //-130, -90
if (last_hour != hour) putimage(-80, -90, &sz[h2]); //-80, -90
static bool first = true;
if (first) putimage(-30, -75, &sz[10]);//-30,-75
if (first) putimage(-30, -45, &sz[10]);//-30,-45
int m1 = minute / 10;
int m2 = minute % 10;
if (last_minute != minute) putimage(-10, -90, &sz[m1]);//-10, -90
if (last_minute != minute) putimage(40, -90, &sz[m2]);//40,-90
if (first || last_second / 10 != second / 10) putimage(90, -55, &sz2[second / 10]); //90,-55
if (first || last_second % 10 != second % 10) putimage(120, -55, &sz2[second % 10]); //120,-55
if (first || (hour == 0 && minute == 0 && second < 3)) {
drawDate();
}
first = false;
last_hour = hour;
last_minute = minute;
last_second = second;
}后续私信我哦~
边栏推荐
猜你喜欢
随机推荐
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
如何用matlab做高精度计算?【第一辑】
YOLOv3详解:从零开始搭建YOLOv3网络
狗都能看懂的CenterNet讲解及代码复现
指定区域内随机填充圆之matlab实现
用matlab打造的摩斯电码加解码器音频版,支持包括中文在内的任意字符
Centos通过Docker搭建MySQL的PXC集群
目标检测中的IoU、GIoU、DIoU与CIoU
代码小变化带来的大不同
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
JVM工具之 JPS
数据库文档生成工具V1.0
设置el-table自动向下滑动(不多解释,直接代码实现)
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM
Unable to preventDefault inside passive event listener due to target being treated as passive. See
idea使用@Autowired注解爆红原因及解决方法
SENet detailed explanation and Keras reproduction code
2DCNN, 1DCNN, BP, SVM fault diagnosis and result visualization of matlab
E-R图总结规范
MySQL外键(详解)









