• 控制台画图的小程序(没有写保存功能,现在看看也懒得写了)


      1 #include <stdio.h>
      2 #include <windows.h>
      3 #include <conio.h>
      4 
      5 typedef struct        
      6 {
      7     int x;
      8     int y;
      9 }CursorPos;
     10 
     11 int Max_X = 0;                    // 画布行大小,为清屏做准备
     12 int Max_Y = 0;                    // 画布列大小,为清屏做准备
     13 
     14 void HideCursor(HANDLE hOutput);
     15 void GotoXy(HANDLE hOutput, int x, int y);
     16 int  GetMousePos(CursorPos *cr);
     17 int  GetKeyPress(HANDLE hOutput, char * choice);
     18 void ShowUsage(HANDLE hOutput);
     19 void InitScreen(HANDLE hOutput);
     20 void ReadFile(HANDLE hOutput);
     21 void SaveFile(HANDLE hOutput);
     22 
     23 int main(void)
     24 {
     25     char choice=1;
     26     FILE * fp;                        // 打开文件
     27     CursorPos cursor;                // 鼠标单击的逻辑坐标
     28     HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     29     SetConsoleTitle("控制台画图游戏");
     30     HideCursor(hOutput);
     31     ShowUsage(hOutput);
     32 
     33     if((fp=fopen("draw.txt","r"))==NULL)    // 新建文件
     34         fp=fopen("draw.txt","w");
     35     fclose(fp);
     36 
     37     while(1)
     38     {
     39         if(GetMousePos(&cursor))
     40         {
     41             GotoXy(hOutput, cursor.x,cursor.y);
     42             printf("%c",choice);
     43         }
     44         GetKeyPress(hOutput, &choice);
     45     }
     46     return 0;
     47 }
     48 int GetMousePos(CursorPos *cursor)
     49 {    
     50     HANDLE hInput  = GetStdHandle(STD_INPUT_HANDLE); 
     51     INPUT_RECORD inRec;
     52     DWORD res;
     53 
     54     ReadConsoleInput(hInput, &inRec, 1, &res);
     55     if (inRec.EventType == MOUSE_EVENT && inRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //鼠标左键
     56     {
     57         cursor->x = inRec.Event.MouseEvent.dwMousePosition.X;
     58         if(cursor->x > Max_X)
     59             Max_X = cursor->x;
     60         cursor->y = inRec.Event.MouseEvent.dwMousePosition.Y;
     61         if(cursor->y > Max_Y)
     62             Max_Y = cursor->y;
     63         
     64         return 1;
     65     }
     66     return 0;
     67 }
     68 int  GetKeyPress(HANDLE hOutput, char * choice)
     69 {
     70     HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
     71     INPUT_RECORD inRec;
     72     DWORD res;
     73     ReadConsoleInput(hInput, &inRec, 1, &res);
     74 
     75     if(inRec.EventType == KEY_EVENT)
     76     {
     77         switch(inRec.Event.KeyEvent.wVirtualKeyCode)
     78         {
     79         case VK_ESCAPE  : InitScreen(hOutput);break;
     80         case VK_CONTROL : ReadFile(hOutput);break;
     81         case VK_MENU    : SaveFile(hOutput);break;
     82         default :    * choice =inRec.Event.KeyEvent.uChar.AsciiChar;
     83         }
     84         return 1;
     85     }
     86     return 0;
     87 }
     88 
     89 void HideCursor(HANDLE hOutput)
     90 {
     91     CONSOLE_CURSOR_INFO cursor_info = {1, 0};    
     92     SetConsoleCursorInfo(hOutput, &cursor_info);
     93 }
     94 
     95 /**********移动光标至(x,y)处************/
     96 void GotoXy(HANDLE hOutput, int x, int y)
     97 {
     98     COORD CursorPosd;    
     99     CursorPosd.X = x;
    100     CursorPosd.Y = y;
    101     SetConsoleCursorPosition(hOutput, CursorPosd);        
    102 }
    103 
    104 /**************显示用法*****************/
    105 void ShowUsage(HANDLE hOutput)
    106 {
    107     printf("用法:
    ");
    108     printf("一、按住键盘按键和鼠标左键可以改变笔触类型
    ");
    109     printf("二、按下 ESC 键清空屏幕
    ");
    110     printf("三、按下 CTRL 键打开文件
    ");
    111     printf("四、按下 ALT 键保存文件(尚未实现)
    ");
    112     printf("按任意键开始画图。");
    113     Max_X = 45;
    114     Max_Y = 6;
    115     getch();
    116     InitScreen(hOutput);
    117 }
    118 
    119 /**************清屏函数*****************/
    120 void InitScreen(HANDLE hOutput)
    121 {
    122     int i,j;
    123     GotoXy(hOutput, 0, 0);
    124     for(j=0;j<=Max_Y;j++)
    125     {
    126         for(i=0;i<=Max_X;i++)
    127             printf(" ");
    128         printf("
    ");
    129     }
    130     Max_X = Max_Y = 0;
    131 }
    132 
    133 void ReadFile(HANDLE hOutput)
    134 {
    135     FILE * fp;
    136     char c;
    137     GotoXy(hOutput,0,0);
    138     fp=fopen("draw.txt","r");
    139     fscanf(fp,"%c",&c);
    140     printf("%c",c);
    141 }
    142 
    143 void SaveFile(HANDLE hOutput)
    144 {
    145     FILE * fp;
    146     char c;
    147     int i,j;
    148     GotoXy(hOutput,0,0);
    149     fp=fopen("draw.txt","w");
    150     
    151     fclose(fp);
    152 }
  • 相关阅读:
    JS定时器做物体运动
    JS做动态表格
    JS如何做2048(详细)
    改变 C/C++ 控制台程序的输出颜色和样式
    The Game Of Life – 数据结构与算法的敲门砖
    适用于 macOS 下 2K 显示器开启 HiDPI 的简便解决方案
    「踩坑记」Android API 判断权限申请结果的闪退问题
    Hello World!
    js 放大镜效果
    js 随机验证码生成及校验
  • 原文地址:https://www.cnblogs.com/doodle777/p/3154445.html
Copyright © 2020-2023  润新知