论坛: 编程破解 标题: 怎么用C写一个时钟啊?大字符的显示问题,请进!(问题已解决) 复制本贴地址    
作者: radom [f_h]    论坛用户   登录
就是在屏幕中间写一个大大的: HH:MM:SS 的,像电子手表上显示一样的。这怎么写呀?
屏幕刷新有点问题。不太好看。。。
另外: 标准C里面也有一个清屏幕的字符。今天忘了抄下来。。直接用在
printf("***")里的。。忘了是怎么写的。。过些时,再用那个字符试试;




[此贴被 radom(f_h) 在 10月13日21时44分 编辑过]

地主 发表时间: 06-09-17 21:51

回复: jhkdiy [jhkdiy]   版主   登录
不知道你的编程环境,用什么语言,是基于DOS还是Windows的。在《Windows程序设计》第五版里已有一个详细的例子,在定时器那一章中,现在我直接贴出源代码吧,你可以用VC6新建一个Win32工程,然后将下面的代码粘贴上去后进行编译即可:

代码:

程序8-3  DIGCLOCK
       
DIGCLOCK.C
       
/*----------------------------------------------------------------------------
       
  DIGCLOCK.C --        Digital Clock
       
                                          (c) Charles Petzold, 1998
       
----------------------------------------------------------------------------*/
       
#include <windows.h>
       
#define ID_TIMER    1
       
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
       
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
       
                                                        PSTR szCmdLine, int iCmdShow)
       
{
       
  static TCHAR szAppName[] = TEXT ("DigClock") ;
       
          HWND                                hwnd ;
       
          MSG                                  msg ;
       
          WNDCLASS                      wndclass ;
       

          wndclass.style                                      = CS_HREDRAW | CS_VREDRAW ;
       
          wndclass.lpfnWndProc                                = WndProc ;
       
          wndclass.cbClsExtra                                  = 0 ;
       
          wndclass.cbWndExtra                                  = 0 ;
       
        wndclass.hInstance                                  = hInstance ;
       
          wndclass.hIcon                                      = LoadIcon (NULL, IDI_APPLICATION) ;
       
          wndclass.hCursor                                    = LoadCursor (NULL, IDC_ARROW) ;
       
          wndclass.hbrBackground                      = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
       
          wndclass.lpszMenuName                        = NULL ;
       
          wndclass.lpszClassName                      = szAppName ;
       

          if (!RegisterClass (&wndclass))
       
          {
       
                  MessageBox (  NULL, TEXT ("Program requires Windows NT!"),
       
                                                                        szAppName, MB_ICONERROR) ;
       
                  return 0 ;
       
          }
       

          hwnd = CreateWindow (szAppName, TEXT ("Digital Clock"),
       
                      WS_OVERLAPPEDWINDOW,
       
                      CW_USEDEFAULT, CW_USEDEFAULT,
       
                      CW_USEDEFAULT, CW_USEDEFAULT,
       
                      NULL, NULL, hInstance, NULL) ;
       

          ShowWindow (hwnd, iCmdShow) ;
       
          UpdateWindow (hwnd) ;
       

          while (GetMessage (&msg, NULL, 0, 0))
       
                  {
       
                          TranslateMessage (&msg) ;
       
                          DispatchMessage (&msg) ;
       
                }
       
          return msg.wParam ;
       
          }
       

void DisplayDigit (HDC hdc, int iNumber)
       
{
       
          static BOOL  fSevenSegment [10][7] = {
       
                        1, 1,    1,    0,    1,    1,    1,            // 0
       
                        0, 0,    1,    0,    0,    1,    0,            // 1
       
                        1, 0,    1,    1,    1,    0,    1,            // 2
       
                        1, 0,    1,    1,    0,    1,    1,            // 3
       
                        0, 1,    1,    1,    0,    1,    0,            // 4
       
                        1, 1,    0,    1,    0,    1,    1,            // 5
       
                      1, 1,    0,    1,    1,    1,    1,            // 6
       
                        1, 0,    1,    0,    0,    1,    0,          // 7
       
                        1, 1,    1,    1,    1,    1,    1,            // 8
       
                        1, 1,    1,    1,    0,    1,    1 } ;        // 9
       
          static POINT ptSegment [7][6] = {
       
                                  7,  6,  11,  2,  31,  2, 35,  6,  31, 10, 11, 10,
       
                  6,  7,  10, 11, 10, 31, 6,  35, 2,  31, 2,  11,
       
                  36, 7,  40, 11, 40, 31, 36,  35, 32, 31, 32, 11,
       
                  7 , 36, 11, 32, 31, 32, 35,  36, 31, 40, 11, 40,
       
                  6 , 37, 10, 41, 10, 61, 6,  65, 2,  61, 2,  41,
       
                  36, 37, 40, 41, 40, 61, 36,  65, 32, 61, 32, 41,
       
                  7 , 66, 11, 62, 31, 62, 35,  66, 31, 70, 11, 70 } ;
       
    int          iSeg ;
       
          for (iSeg = 0 ; iSeg < 7 ; iSeg++)
       
                  if (fSevenSegment [iNumber][iSeg])
       
                                        Polygon (hdc, ptSegment [iSeg], 6) ;
       
}
       

void DisplayTwoDigits (HDC hdc, int iNumber, BOOL fSuppress)
       
{
       
          if (!fSuppress || (iNumber / 10 != 0))
       
                DisplayDigit (hdc, iNumber / 10) ;
       
          OffsetWindowOrgEx (hdc, -42, 0, NULL) ;
       
          DisplayDigit (hdc, iNumber % 10) ;
       
          OffsetWindowOrgEx (hdc, -42, 0, NULL) ;
       
}
       

void DisplayColon (HDC hdc)
       
{
       
          POINT ptColon [2][4] = {      2,    21,    6,    17,    10,    21,    6,    25,
       
                              2,51,    6,    47,    10,    51,    6,    55 } ;
       

    Polygon (hdc, ptColon [0], 4) ;
       
    Polygon (hdc, ptColon [1], 4) ;
       

    OffsetWindowOrgEx (hdc, -12, 0, NULL) ;
       
}
       

void DisplayTime (HDC hdc, BOOL f24Hour, BOOL fSuppress)
       
{
       
    SYSTEMTIME st ;
       
    GetLocalTime (&st) ;
       
    if (f24Hour)
       
            DisplayTwoDigits (hdc, st.wHour, fSuppress) ;
       
    else
       
    DisplayTwoDigits (hdc, (st.wHour %= 12) ? st.wHour : 12, fSuppress) ;
       
    DisplayColon (hdc) ;
       
    DisplayTwoDigits (hdc, st.wMinute, FALSE) ;
       
    DisplayColon (hdc) ;
       
    DisplayTwoDigits (hdc, st.wSecond, FALSE) ;
       
}
       
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
       
{
       
    static BOOL          f24Hour, fSuppress ;
       
    static HBRUSH        hBrushRed ;
       
    static int                    cxClient, cyClient ;
       
    HDC                                        hdc ;
       
    PAINTSTRUCT  ps ;
       
    TCHAR                                szBuffer [2] ;
       

    switch (message)
       
          {
       
          case  WM_CREATE:
       
                  hBrushRed = CreateSolidBrush (RGB (255, 0, 0)) ;
       
                SetTimer (hwnd, ID_TIMER, 1000, NULL) ;// fall through
       

    case  WM_SETTINGCHANGE:
       
            GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, 2) ;
       
            f24Hour = (szBuffer[0] == '1') ;
       

            GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITLZERO, szBuffer, 2) ;
       
            fSuppress = (szBuffer[0] == '0') ;
       

            InvalidateRect (hwnd, NULL, TRUE) ;
       
            return 0 ;
       

    case  WM_SIZE:
       
            cxClient = LOWORD (lParam) ;
       
                  cyClient = HIWORD (lParam) ;
       
                  return 0 ;
       

          case  WM_TIMER:
       
                  InvalidateRect (hwnd, NULL, TRUE) ;
       
                return 0 ;
       

  case  WM_PAINT:
       
                  hdc = BeginPaint (hwnd, &ps) ;
       

                  SetMapMode (hdc, MM_ISOTROPIC) ;
       
                  SetWindowExtEx (hdc, 276, 72, NULL) ;
       
                  SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
       

                  SetWindowOrgEx (hdc, 138, 36, NULL) ;
       
                  SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
       
                  SelectObject (hdc, GetStockObject (NULL_PEN)) ;
       
                  SelectObject (hdc, hBrushRed) ;
       

                  DisplayTime (hdc, f24Hour, fSuppress) ;
       

                EndPaint (hwnd, &ps) ;
       
                return 0 ;
       

          case  WM_DESTROY:
       
                  KillTimer (hwnd, ID_TIMER) ;
       
                  DeleteObject (hBrushRed) ;
       
                  PostQuitMessage (0) ;
       
                  return 0 ;
       
          }
       
          return DefWindowProc (hwnd, message, wParam, lParam) ;
       
}




B1层 发表时间: 06-09-18 13:49

回复: radom [f_h]   论坛用户   登录
我想一个TC环境的,VC的,在DOS不能运行。VC环境我也有,但我想要一个TC的。。谢谢啊。。

B2层 发表时间: 06-09-18 17:28

回复: kert_t8 [kert_t8]   论坛用户   登录
大大的

关键在这个大大的,其他都好办,会不会需要单独一个字库,普通的方法肯定不可能在命令行下面打大大的字。要不就画出来,用图形的函数,不过我不懂

B3层 发表时间: 06-09-18 18:38

回复: jhkdiy [jhkdiy]   版主   登录
我的情况和月之御者一样,我猜想也要使用dos下的图形自己画。或许286能提供具体的解决办法。

B4层 发表时间: 06-09-18 19:51

回复: radom [f_h]   论坛用户   登录
可以先不用大大的。普通的也行。。谢谢呵!

B5层 发表时间: 06-09-18 19:55

回复: sniper167 [sniper167]   论坛用户   登录
TC下有时间函数,可以取得系统时间
啥名字偶忘记了

B6层 发表时间: 06-09-19 16:37

回复: sniper167 [sniper167]   论坛用户   登录
http://sniper167.bokee.com/4445514.html
这个程序里面用到了那个函数,楼主可以参考

B7层 发表时间: 06-09-19 16:41

回复: sniper167 [sniper167]   论坛用户   登录
大大的???

自己整11个数组来存放 大大的 0-9 以及 :

要显示的时候就调相应的数组

大家看看这个方法杂样  似乎太麻烦了  数组不太容易实现喃

大家多指点

B8层 发表时间: 06-09-19 17:02

回复: 286 [unique]   版主   登录
本来我十年前曾写过一个,就是全屏幕不停地显示象电子表的时间的,但现在找不到了。回头找到把程序和原代码给你吧。

B9层 发表时间: 06-09-20 12:44

回复: kert_t8 [kert_t8]   论坛用户   登录
跟控制光标是不是有关系?

你得到时间,用ctime()得到一个字符串sprintf到一个char[]里面去,然后找出来hh:mm:ss这一部分,然后打印出来。有一个ascii符是退格符,可以打印八遍使光标退回去,然后....

这个是我臆想出来的,不行不能怪我

闪人.....

B10层 发表时间: 06-09-20 17:36

回复: radom [f_h]   论坛用户   登录
286哥, 我就是想要电子钟那样的,找到代码了吗?

我楼上的,我是一菜鸟,那能做控制鼠标那高级的东西呀.连窗口都不会啊.

B11层 发表时间: 06-09-23 09:37

回复: kert_t8 [kert_t8]   论坛用户   登录
我也是个菜鸟,我也不会控制鼠标,我说的是控制光标,否则的话就一直刷屏,什么也看不清楚

B12层 发表时间: 06-09-25 13:22

回复: kert_t8 [kert_t8]   论坛用户   登录
#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main ()
{
        while (1) {
                time_t sec  = time(NULL);
                struct tm t = *localtime(&sec);

                printf("\x1b[2J");      /* clear screen and home cursor */
                printf("\x1b[31;40m");  /* red foreground, black background */
                printf("\x1b[11;29H");  /* moves cursor to line 11, column 29 */
                printf("+-----^--^-----+\n");
                printf("\x1b[12;29H");
                printf("|\t%02d:%02d:%02d:  |\n", t.tm_hour, t.tm_min, t.tm_sec);
                printf("\x1b[13;29H");
                printf("+-------V------+\n");
                sleep(1);
        }

        return 0;
}
/* reference: ANSI Escape Sequences
* compile:  gcc mytime.c -o mytime
* usage:    ./mytime
*/



以上为转贴,无意看到的

B13层 发表时间: 06-10-03 22:28

回复: radom [f_h]   论坛用户   登录
先谢了,稍后登录再试.
呵呵..linux下试了一下,可以的..呵呵...这程序写得挺不好懂的...
printf 还可以这样找颜色.我晕..只记得cprintf可以打印��色....
printf 还可以移动光标?我晕..只记得goto,moveto可能移动光标..
这个 sleep(1)就是1暂停1秒的意思...?汗.
只能在 linux 下编译...呆会儿试试 win下的.嘿.
代码:

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>

int main ()
{
      while(1){

    time_t sec = time(NULL);
                struct tm t = *localtime(&sec);
    clrscr();
    window(33,12,48,13);
    cprintf("%02d:%02d:%02d\n", t.tm_hour,t.tm_min,t.tm_sec);
    sleep(1);

    }
return 0;
}



[此贴被 radom(f_h) 在 10月04日17时10分 编辑过]


[此贴被 radom(f_h) 在 10月04日18时52分 编辑过]

B14层 发表时间: 06-10-04 11:24

回复: NetFog [q70213526]   版主   登录
帮顶..

B15层 发表时间: 06-10-04 18:51

回复: radom [f_h]   论坛用户   登录
代码:


#include "stdio.h"
#include "conio.h"
#include "time.h"
#include "graphics.h"
void clk();
void wind();

main()
{
    int gd=DETECT,gm;
    registerbgidriver(EGAVGA_driver);
    initgraph(&gd,&gm,"");
    while(!kbhit())
        {
wind();
          clk();
sleep(1);
        }
    closegraph();
    exit();  /*郁闷,不用这个函数,调试或者运行的时候,有时会出现"CPU遇到无效指令..."
              非要加上此函数才不会出错..可看程序时,却不知道哪儿有问题.功能有限!*/
}

/* shizhong */

void clk()
    {
                time_t sec;
struct tm foo;
char hr[3],mi[3],se[2];
sec=time(NULL);
foo=*localtime(&sec);
sprintf(se,"%d",foo.tm_sec);
sprintf(mi,"%d:",foo.tm_min);
sprintf(hr,"%d:",foo.tm_hour);
settextstyle(0,0,3);
setcolor(WHITE);
outtextxy(120,110,hr);
outtextxy(190,110,mi);
outtextxy(260,110,se);
      }
/* chuangkou */
void wind()
{
    char cper[50];
    setbkcolor(BLUE);
    cleardevice();
    setviewport(100,100,540,380,1);
    setfillstyle(1,2);
    setcolor(YELLOW);
    setlinestyle(0,0,3);
    rectangle(0,0,439,279);
    floodfill(50,50,14);
    settextstyle(1,0,1);
    setcolor(BLACK);
    sprintf(cper,"%s","Maked by FairyNull");
    outtextxy(250,250,cper);

}



注:  此程序在TC环境下能显示大字符,可是当在CMD[即退出TC]的时候,运行这个程序时,却不 能    显示大字符,这是为什么呀?怎么解决?"


再注:以上直接运行已可以显示大字符。但还是不知道为什么有的还是不行。但以上程序已可以。




[此贴被 radom(f_h) 在 10月13日21时27分 编辑过]

B16层 发表时间: 06-10-10 20:46

回复: radom [f_h]   论坛用户   登录
代码:


17.14  怎样通过ANSI驱动程序来清屏?
    这种操作可以通过"<esc>[2J”来完成,下面的程序演示了这一点:
# include <stdio. h>
main ( )
{
      printf( " %c[2JNice to have an empty screen. \n" , 27 ) ;
      return ( 0 );

    17.15  怎样通过ANSI驱动程序来存储光标位置?
    这种操作可以通过"<esc>[s"来完成,下面的程序演示了这一点:
    #include<stdio.h>
    main()
    {
        printf( "Cursor position is %c[s \n" , 27 );
        printf ( "Interrupted ! \n" ) ;
        printf( "%c[uSAVED! !\n" , 27 );
        return( 0 );
    }

    17.16  怎样通过ANSI驱动程序来恢复光标位置?
    这种操作可以通过“<esc>[u”来完成,请参见17.15中的例子。

    17.17  怎样通过ANSI驱动程序来改变屏幕颜色?
    完成这项任务的方法是先改变当前文本的背景色,然后清屏。下面的程序就是一个例子:
    # include <stdio. h>
    int main ( )
    {
        printf( " %0c[43;32m%c[2JOhh, pretty colors!\n", 27 , 27 ) ;
        return( 0 );
    }

    17.18  怎样通过ANSI驱动程序来写带有颜色的文本?
    文本的颜色是可以改变的文本属性之一。文本的属性可以通过“(esc>[<attr>m”来改变。在ANSI字符序列中,文本的属性是用数字来表示的。你可以用一条命令来设置多种属性,各种属性之间用分号分隔开,例如“<esc>[<attr>;<attr>m"。下面的程序演示了这一点:
    # include <stdio. h>
    main ( )
    {
          printf("%c[32;44mPsychedelic, man.\n" , 27 );
        return( 0 );
    }
    以下列出了ANSI驱动程序所支持的属性,你的显示器可能不支持其中的某些选项:
        1―High Intensity(高强度)
        2一Low Intensity(低强度)
        3一Italic(斜体)
        4一Underline(下划线)
        5一Blinking(闪烁)
        6一Fast Blingking(快闪)
        7一Reverse(反转)
        8一Invisible(不可见)
    前景色:
        30一Black(黑)
        31一Red(红)
        32一Green(绿)
        33一Yellow(黄)
        34一Blue(蓝)
        35一Magenta(洋红)
        36一Cyan(青蓝)
        37一White(白)
    背景色:
        40―Black(黑)
        41一Red(红)
        42一Green(绿)
        43一Yellow(黄)
        44一Blue(蓝)
        45一Magenta(洋红)
        46一Cyan(青蓝)
        47一White(白)

    17.19怎样通过ANSI驱动程序来移动光标?
    移动光标有两种方式:相对移动和绝对移动。相对移动是指相对于当前光标位置的移动,例如“将光标上移两格”。绝对移动是指相对于屏幕左上角的移动,例如“将光标移到第10行第5列”。
    相对移动可按以下方式进行:
    “<esc>[#a”,其中#表示上移的格数
    “<esc>[#b”,其中#表示下移的格数
    “<esc>[#c”,其中#表示右移的格数
    “<esc>[#d”,其中#表示左移的格数
    将光标移到绝对位置的方法是:
    “<esc>[<row>;<col>H”,其中row和col分别表示目标位置所在的行数和列数。




好东西。呵呵。。各位试试啦。

ESC 的16进制表示方法为 \x1b


[此贴被 radom(f_h) 在 10月14日12时30分 编辑过]

B17层 发表时间: 06-10-14 11:09

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号