读书人

隔一秒换一上背景和文字颜色

发布时间: 2012-09-18 16:21:42 作者: rapoo

隔一秒换一下背景和文字颜色
以下是我的初步构想,但是很遗憾无法实现,求解决.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int main(void)
{
clock_t time = clock() / CLOCKS_PER_SEC + 1;
clock_t now;
if ((now = clock() / CLOCKS_PER_SEC) > time && (now = clock() / CLOCKS_PER_SEC) <= time +1)
{
system("color 0D");
printf("****");
time += 1;
}
if ((now = clock() / CLOCKS_PER_SEC) > time && (now = clock() / CLOCKS_PER_SEC) <= time +1)
{
system("color 0E");
printf("****");
time += 1;
}
if ((now = clock() / CLOCKS_PER_SEC) > time && (now = clock() / CLOCKS_PER_SEC) <= time +1)
{
system("color 0F");
printf("****");
time += 1;
}
system ("pause");
return 0;
}


[解决办法]
用定时器试试
[解决办法]
你这个比定时器 复杂多了。。。

[解决办法]

C/C++ code
#include <Windows.h>#include <stdio.h>#include <conio.h>int count = 0;WORD Color = 0x89;//   http://blog.csdn.net/hongwenjun/article/details/6202127//   windows控制台彩色文本函数,今天研究了别人,摸索出使用方法了bool SetConsoleColor(WORD forceGroundColor, WORD backGroundColor);bool SetConsoleColor(WORD Color);void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);DWORD CALLBACK Thread(PVOID pvoid);int main(){    DWORD dwThreadID;    printf("use timer in workthread of console application\n");    HANDLE hThread = CreateThread(NULL, 0, Thread, NULL, 0, NULL);    getchar();    printf("秒数: %d", count);    return 0;};void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime){    SetConsoleColor(Color += 0x10); // 改变背景颜色    printf("计时%d\n", count++);}DWORD CALLBACK Thread(PVOID pvoid){    MSG msg;    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);    UINT timerid = SetTimer(NULL, 111, 1000, TimerProc);    BOOL bRet;    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {        if (bRet == -1) {            printf("Error:the thread will quit,error id is %d\n", GetLastError());            break;        } else {            TranslateMessage(&msg);            DispatchMessage(&msg);        }    }    KillTimer(NULL, timerid);    printf("thread end here\n");    return 0;}bool SetConsoleColor(WORD forceGroundColor, WORD backGroundColor){    // 获得缓冲区句柄。参数可以选择一下三种值:    //// STD_INPUT_HANDLE            标准输入的句柄    //// STD_OUTPUT_HANDLE           标准输出的句柄    //// STD_ERROR_HANDLE            标准错误的句柄    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    if (handle == 0)        return false;    //设置文本及背景颜色。    BOOL ret = SetConsoleTextAttribute(handle, forceGroundColor | backGroundColor);    return (ret == TRUE);}bool SetConsoleColor(WORD Color){    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    if (handle == 0)        return false;    BOOL ret = SetConsoleTextAttribute(handle, Color);    return (ret == TRUE);} 

读书人网 >C语言

热点推荐