zoukankan      html  css  js  c++  java
  • 控制台画图的小程序(没有写保存功能,现在看看也懒得写了)

      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 }
  • 相关阅读:
    微信小程序---自定义三级联动
    jQuery分页插件
    微信小程序点击图片放大
    微信小程序----日期时间选择器(自定义精确到分秒或时段)
    PHP文件上传error的错误类型
    获取url传的参数转变为对象的方法
    mysql忘记密码时,重新修改密码
    表单注册及自定义validate手机验证码验证实例
    登陆jq表单验证及jqcookie记住密码实例
    iframe高度自适应,自适应子页面高度
  • 原文地址:https://www.cnblogs.com/doodle777/p/3154445.html
Copyright © 2011-2022 走看看