当前位置:网站首页>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;
}
后续私信我哦~
边栏推荐
猜你喜欢
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
MySQL外键(详解)
U-Net详解:为什么它适合做医学图像分割?(基于tf-Kersa复现代码)
matlab封闭曲线拟合 (针对一些列离散点)
数据库文档生成工具V1.0
基于爬行动物搜索RSA优化LSTM的时间序列预测
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols
nacos 返回 403 unknown user 太他么坑了 源码解析
如何用matlab做高精度计算?【第二辑】
随机推荐
数组的一些方法
IDEA中创建编写JSP
SENet detailed explanation and Keras reproduction code
目标检测中的IoU、GIoU、DIoU与CIoU
科研绘图图表类型种类繁多,本文告诉你如何选择!
MySQL基础(DDL、DML、DQL)
天鹰优化的半监督拉普拉斯深度核极限学习机用于分类
MMDeploy部署实战系列【第四章】:onnx,tensorrt模型推理
数据库:整理四个实用的SQLServer脚本函数
Centos通过Docker搭建MySQL的PXC集群
DenseNet详解及Keras复现代码
微软电脑管家2.0公测版体验
SegNet——论文笔记
idea使用@Autowired注解爆红原因及解决方法
npm包发布与迭代
MySQL面试题大全(陆续更新)
狗都能看懂的Vision Transformer的讲解和代码实现
this关键字,构造函数
unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
Database document generation tool V1.0