我的笔记本是16寸,为了让屏幕更大而隐藏了任务栏,这导致我看时间不方便。平常有一块外接屏一起使用,加上Windows小组件已经消失,特此制作一个桌面时钟。功能如下:
普通版:
gif 信息:
MD5: 4963C964AD15ADD53CD4A438C87D548A
SHA1: A6D6F1170699A7E63DA92C290406793A45B5E201
炫酷RGB版:
批处理版本身不支持窗口置顶与隐藏光标,这里需要借助两个第三方。但是一个脚本带两个第三方很难受,像NTBootAutoFix打包成自解压又没有必要,所以比较累赘,但总归实现比较简单。后续C语言的版本简单许多。
代码:
@echo off
title clockOnTop
rem 不要窗口置顶就删去call windowontop.exe clockOnTop
call windowontop.exe clockOnTop
setlocal enabledelayedexpansion
mode con cols=26 lines=4
CurS.exe /crv 0
set current=%time:~7,1%
:loop
CurS.exe /pos 6 1
echo %date:~0,4%年%date:~5,2%月%date:~8,2%日
CurS.exe /pos 9 2
echo %time:~0,8%
rem 加入颜色变化删去下面的rem
rem call :colorchange
ping 127.0.0.1 /n 1 >nul
goto loop
:colorfunction
set str=0123456789ABCDEF
set /a ca=%random%%%16
set /a cb=%random%%%16
set ca=!str:~%ca%,1!
set cb=!str:~%cb%,1!
color %ca%%cb%
goto :eof
:colorchange
set last=%current%
set current=%time:~7,1%
if %current% NEQ %last% (call :colorfunction)
goto :eof
两个第三方一个是curs.exe,隐藏光标,另一个是windowontop.exe,窗口置顶。 下载链接:直接保存开头第一个(不是炫彩RGB)gif,图种。哈希已经在上面给出。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
// 设置窗口置顶
void setWindowOnTop() {
HWND consoleWindow = FindWindow(NULL, "ClockOnTop"); // 查找标题为 ClockOnTop 的窗口
SetWindowPos(consoleWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// 设置控制台光标位置
void setCursorPosition(int x, int y) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {x, y};
SetConsoleCursorPosition(hConsole, pos);
}
// 隐藏光标
void hideCursor() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(hConsole, &cursorInfo);
}
// 获取控制台缓冲区大小
COORD getConsoleSize() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
COORD size = {csbi.dwSize.X, csbi.dwSize.Y};
return size;
}
// 更改整个控制台背景色
void changeConsoleBackground() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int bg, fg = 15; // 默认白色文字
do {
bg = rand() % 16; // 0-15 颜色
} while (bg == fg); // 确保前景色和背景色不同
int colorCode = (bg << 4) | fg; // 组合背景色和前景色
// 设置整个控制台的背景色
COORD consoleSize = getConsoleSize();
DWORD cells = consoleSize.X * consoleSize.Y;
DWORD written;
FillConsoleOutputAttribute(hConsole, colorCode, cells, (COORD){0, 0}, &written);
FillConsoleOutputCharacter(hConsole, ' ', cells, (COORD){0, 0}, &written);
SetConsoleTextAttribute(hConsole, colorCode);
}
int main() {
system("title ClockOnTop"); // 设置窗口标题
system("mode con cols=26 lines=4"); // 设置窗口大小
hideCursor(); // 隐藏光标
setWindowOnTop(); // 让窗口置顶
time_t lastUpdate = 0;
while (1) {
// 获取当前时间
time_t now = time(NULL);
if (now == lastUpdate) {
continue; // 如果秒没变,跳过循环
}
lastUpdate = now;
struct tm *tm_info = localtime(&now);
// 变换整个控制台背景色
changeConsoleBackground();
// 输出日期
setCursorPosition(6, 1);
printf("%04d年%02d月%02d日\n", tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday);
// 输出时间
setCursorPosition(9, 2);
printf("%02d:%02d:%02d\n", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
}
return 0;
}
人还是要多学习。
2025年3月26日