C语言跨年小程序
主要是利用SetConsoleCurosrPosition实现
话不多说直接上代码
#include
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
#include
#include
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,“winmm.lib”)
//#pragma comment( linker,"/subsystem:“windows” /entry:“mainCRTStartup”" ) 隐藏控制台
using namespace std;
void NewWindow();
int n = 1;
HANDLE hOutput;
COORD currentFontCoord,currentFontTime;
IMAGE img;
int ScreenWidth, ScreenHeight;
int Interval = 100;
LRESULT CALLBACK Melter(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
switch (Msg)
{
case WM_CREATE:{
HDC Desktop = GetDC(HWND_DESKTOP);
HDC Window = GetDC(hWnd);
BitBlt(Window, 0, 0, ScreenWidth, ScreenHeight, Desktop, 0, 0, SRCCOPY);
ReleaseDC(hWnd, Window);
ReleaseDC(HWND_DESKTOP, Desktop);
SetTimer(hWnd, 0, Interval, 0);
ShowWindow(hWnd, SW_SHOW);
break;
}
case WM_PAINT:{
ValidateRect(hWnd, 0);
break;
}
case WM_TIMER:{
HDC Window = GetDC(hWnd);
int X = (rand() % ScreenWidth) - (150 / 2), Y = (rand() % 15), Width = (rand() % 150);
BitBlt(Window, X, Y, Width, ScreenHeight, Window, X, 0, SRCCOPY);
//int X = (rand() % ScreenWidth, Window, X, 0, SRCCOPY);
ReleaseDC(hWnd, Window);
break;
}
case WM_DESTROY:{
KillTimer(hWnd, 0);
PostQuitMessage(0);
break;
}
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
void NewF11_Wind(){
HWND hwnd = GetForegroundWindow();
int cx = GetSystemMetrics(SM_CXSCREEN); /* 屏幕宽度 像素 /
int cy = GetSystemMetrics(SM_CYSCREEN); / 屏幕高度 像素 /
LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE); / 获取窗口信息 /
/ 设置窗口信息 最大化 取消标题栏及边框 */
SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
SetWindowPos(hwnd, HWND_TOP, 0, 0, cx+17, cy, 0);
}
void PlayNewMusic(){
mciSendString(“play images\10.wmv”, 0, 0, 0);
Sleep(10000);
}
void NewSizeFont(){
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFO consoleCurrentFont;
GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont);
currentFontCoord = GetConsoleFontSize(hOutput, consoleCurrentFont.nFont);
CONSOLE_FONT_INFOEX ConsoleCurrentFontEx;
ConsoleCurrentFontEx.cbSize = sizeof(CONSOLE_FONT_INFOEX);
ConsoleCurrentFontEx.dwFontSize.X = 64;
ConsoleCurrentFontEx.dwFontSize.Y = 128;
SetCurrentConsoleFontEx(hOutput, FALSE, &ConsoleCurrentFontEx);
time_t timep;
struct tm *p;
time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
p = localtime(&timep);//用localtime将秒数转化为struct tm结构体
//thread thread1(NewFont);
p->tm_year = 2019;
p->tm_mon = 12;
p->tm_mday = 31;
p->tm_hour = 23;
p->tm_min = 59;
p->tm_sec = 51;
if (n == 1){
for (int i = 9; i>=0; i--){
currentFontCoord.X = 15;
currentFontCoord.Y = 7;
SetConsoleCursorPosition(hOutput, currentFontCoord);
if (i != 0){
printf("%d", i);
NewFont(i);
currentFontCoord.X = 5;
currentFontCoord.Y = 1;
SetConsoleCursorPosition(hOutput, currentFontCoord);
printf("%d/%d/%d %02d:%02d:%02d\n", p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec ++);
if (i == 4){
mciSendString("pause images\\66.mp3", 0, 0, 0);
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)PlayNewMusic, NULL, NULL, NULL);
}
}
Sleep(1000);
}
printf(" ");
currentFontCoord.X = 7;
currentFontCoord.Y = 1;
SetConsoleCursorPosition(hOutput, currentFontCoord);
p->tm_year = 2020;
p->tm_mon = 1;
p->tm_mday = 1;
p->tm_hour = 1;
p->tm_min = 1;
p->tm_sec = 1;
printf("%d/%d/%d %02d:%02d:%02d\n", p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec++);
currentFontCoord.X = 15;
currentFontCoord.Y = 6;
SetConsoleCursorPosition(hOutput, currentFontCoord);
printf("2020你好");
currentFontCoord.X = 15;
currentFontCoord.Y = 7;
SetConsoleCursorPosition(hOutput, currentFontCoord);
printf("2019再见");
n = 0;
}
}
void NewWindow(){
HWND hwnd = initgraph(0,0);
loadimage(&img, “images\233.jpg”, 0, 0);
putimage(0, 0,&img);
//system(“ipconfig”);
//HWND hwnd1 = system(“start”);
//while (true)1620,720
MoveWindow(hwnd,2000,2000, 0, 0, true); //彈出控制臺
}
int main(HINSTANCE Inst,HINSTANCE Prev,LPSTR Cmd,int showcmd){
mciSendString(“play images\66.mp3”, NULL, NULL, NULL);
NewF11_Wind();
NewSizeFont();
Sleep(4000);
mciSendString(“play images\66.mp3”, NULL, NULL, NULL);
NewWindow();
Sleep(4000);
ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
WNDCLASS wndClass = { 0, Melter, 0, 0, Inst, 0, LoadCursor(0, IDC_ARROW), 0, 0, “ScreenMelter” };
if (RegisterClass(&wndClass)){
HWND hWnd = CreateWindowExA(WS_EX_TOPMOST, “ScreenMelter”, 0, WS_POPUP, 0, 0, ScreenWidth, ScreenHeight, HWND_DESKTOP, 0, Inst, 0);
if (hWnd){
srand(GetTickCount());
MSG Msg = { 0 };
while (Msg.message != WM_QUIT){
if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
}
getchar();
return 0;
}