Advertisement
FoxTuGa

Paint.dos

May 17th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <Windows.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <dos.h>
  7.  
  8. int Main_Menu(void);
  9. void PrintScreen(void);
  10. void PrintMenu_right(char option, struct tagScreen_Cursor cursor_s);
  11. struct tagScreen_Cursor ScreenPaint(struct tagScreen_Cursor cursor_s, char option);
  12. int CursorYconvertCOLOUR(char option);
  13. int CursorYconvertTYPE(char option);
  14. int ReadConsoleL(char *c);
  15. void ConvertNumtoType(char *c);
  16. void PrintInstructions(void);
  17. void PrintLoadingBar(void);
  18. void Pausa_S(void);
  19. void New_Paint(void);
  20. struct tagS_Cursor *NewNode(void);
  21. void InserirNovoNo(struct tagS_Cursor **phead, struct tagS_Cursor *pnew);
  22. void PrintCredits(void);
  23. void DrawBox(int ini_x, int ini_y, int quant_x, int quant_y);
  24. void Add_ext(char *sname);
  25. int PrintWrite_to_binary(struct tagS_Cursor **phead, char *string);
  26. void ImEx_portPrint(struct tagS_Cursor **phead, char type[]);
  27. int PrintRead_from_binary(struct tagS_Cursor **phead, char *string);
  28. int Search_and_Del_Cursor(struct tagS_Cursor **phead, COORD cursor);
  29. void PrintColours(void);
  30. void PrintTypes(void);
  31. void ResetScreen(struct tagScreen_Cursor cursor_s);
  32. void ResetStruct(struct tagS_Cursor **phead);
  33.  
  34. struct tagScreen_Cursor {
  35.     COORD pos;
  36.     char colour;
  37.     char type;
  38. };
  39. struct tagS_Cursor {
  40.     COORD pos;
  41.     char colour;
  42.     char type;
  43.     struct tagS_Cursor *pnext;
  44.     struct tagS_Cursor *pprev;
  45. };
  46.  
  47.  
  48. HANDLE hConsole;
  49. COORD cursor_default;
  50. CONSOLE_CURSOR_INFO cursor_i;
  51.  
  52. int main() {
  53.     char c;
  54.     int control;
  55.     struct tagScreen_Cursor cursor_s;
  56.     struct tagS_Cursor *phead, *New;    //TODO: Gravar em ficheiros e ler de ficheiros
  57.     FILE *pINI;
  58.     COORD cursor;
  59.  
  60.     pINI = fopen("paintdos.ini", "a+");
  61.  
  62.     phead = NULL;
  63.     cursor_default.X = 4;
  64.     cursor_default.Y = 5;
  65.     cursor_s.pos = cursor_default;
  66.     cursor_s.colour = cursor_s.type = 0;
  67.  
  68.     PrintLoadingBar();
  69.     PrintScreen();
  70.     PrintMenu_right('\0', cursor_s);
  71.     PrintInstructions();
  72.  
  73.     while(TRUE) {
  74.         control=ReadConsoleL(&c);
  75.         if( c == 'p' )
  76.             New_Paint();
  77.         else    if( control == 0 ) {
  78.             cursor_s = ScreenPaint(cursor_s, c);
  79.             if( cursor_s.type != 0 && cursor_s.colour != 0 && c == 32) {
  80.                 New = NewNode();
  81.                 New->colour = cursor_s.colour;
  82.                 New->pos = cursor_s.pos;
  83.                 New->type = cursor_s.type;
  84.  
  85.                 InserirNovoNo(&phead, New);
  86.             }
  87.             if( c == 'h' )
  88.                 Search_and_Del_Cursor(&phead, cursor_s.pos);
  89.         }
  90.         else    if( control == 1 || control == 2) {
  91.             PrintMenu_right(c, cursor_s);
  92.             if( control == 2 )
  93.                 cursor_s.colour = c;
  94.             else
  95.                 cursor_s.type = c;
  96.        
  97.         }
  98.         else    if( control == 5 ) {
  99.  
  100.             system("cls");
  101.             PrintCredits();
  102.             break;
  103.         }
  104.         else    if( control == 10 )  {
  105.             ImEx_portPrint(&phead, "Export", cursor_s);
  106.             ResetStruct(&phead);
  107.         }
  108.         else    if( control == 11 ) {
  109.             ResetStruct(&phead);
  110.             ImEx_portPrint(&phead, "Import", cursor_s);
  111.         }
  112.     }
  113.  
  114.     fclose(pINI);
  115.     return 0;
  116. }
  117.  
  118. void PrintScreen() {
  119.     int idx;
  120.     COORD cursor, cursor2;
  121.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  122.  
  123.     /* Primeira linha HORIZONTAL */
  124.     cursor.X = 2;
  125.     cursor.Y = 2;
  126.     SetConsoleCursorPosition(hConsole, cursor);
  127.     for(idx=0; idx < 73; idx++) {
  128.         putchar(220);
  129.     }
  130.     /* Primeira linha HORIZONTAL */
  131.  
  132.     /* Texto Bem-Vindo */
  133.     cursor.X = 27;
  134.     cursor.Y = 3;
  135.     SetConsoleCursorPosition(hConsole, cursor);
  136.     SetConsoleTextAttribute(hConsole, BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED | FOREGROUND_BLUE);
  137.     printf(" Bem-Vindo ao Paint DOS! ");
  138.     /* Texto Bem-Vindo */
  139.  
  140.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  141.     /* PRIMEIRA linha HORIZONTAL */
  142.     cursor.X = 2;
  143.     cursor.Y = 4;
  144.     SetConsoleCursorPosition(hConsole, cursor);
  145.     for(idx=0; idx < 73; idx++) {
  146.         putchar(219);
  147.     }
  148.     /* Primeira linha HORIZONTAL */
  149.  
  150.     /* Ultima linha HORIZONTAL */
  151.     cursor.Y = 22;
  152.     cursor.X = 2;
  153.     SetConsoleCursorPosition(hConsole, cursor);
  154.     for(idx=0; idx < 73; idx++ ) {
  155.         putchar(220);
  156.     }
  157.     /* Ultima linha HORIZONTAL */
  158.  
  159.     /* Linhas VERTICAIS PRINCIPAIS */
  160.     cursor.Y = cursor2.Y = 3;
  161.     cursor.X = 2;
  162.     cursor2.X = 74;
  163.     for(; cursor.Y < 23 && cursor2.Y < 23; cursor.Y++, cursor2.Y++) {
  164.         SetConsoleCursorPosition(hConsole, cursor);
  165.         putchar(219);
  166.         SetConsoleCursorPosition(hConsole, cursor2);
  167.         putchar(219);
  168.     }
  169.     /* Linhas VERTICAIS PRINCIPAIS */
  170.  
  171.     /* Linha VERTICAL INTERIOR */
  172.     cursor.Y = 5;
  173.     cursor.X = 60;
  174.     for(; cursor.Y < 23; cursor.Y++) {
  175.         SetConsoleCursorPosition(hConsole, cursor);
  176.         putchar(219);
  177.     }
  178.     /* Linha VERTICAL INTERIOR */
  179.  
  180.     cursor.X = 2;
  181.     cursor.Y = 24;
  182.     SetConsoleCursorPosition(hConsole, cursor);
  183.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  184. }
  185. void PrintMenu_right(char option, struct tagScreen_Cursor cursor_s) {
  186.     int idx, block = '\0';
  187.     static char last_c, last_w;
  188.     COORD cursor;
  189.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  190.  
  191.     cursor_i.bVisible = FALSE;
  192.     SetConsoleCursorInfo(hConsole, &cursor_i);
  193.  
  194.     cursor.X = 63;
  195.     /* Escolher CORES */
  196.     if(last_c == 0)
  197.         PrintColours();
  198.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
  199.  
  200.     if( option == 'q' || option == 'w' || option == 'e' ) {
  201.         if( last_c != option && last_c != 0 ) {
  202.             cursor.Y = CursorYconvertCOLOUR(last_c);
  203.             cursor.X--;
  204.             cursor.Y--;
  205.             SetConsoleCursorPosition(hConsole, cursor);
  206.             for(idx=0; idx < 4; idx++)
  207.                 putchar(' ');
  208.             cursor.Y++;
  209.             SetConsoleCursorPosition(hConsole, cursor);
  210.             putchar(' ');
  211.             cursor.Y++;
  212.             SetConsoleCursorPosition(hConsole, cursor);
  213.             for(idx=0; idx < 4; idx++)
  214.                 putchar(' ');
  215.             cursor.Y--;
  216.             cursor.X += 3;
  217.             SetConsoleCursorPosition(hConsole, cursor);
  218.             putchar(' ');
  219.  
  220.             cursor.X = 63;
  221.         }
  222.         cursor.Y = CursorYconvertCOLOUR(option);
  223.         cursor.X--;
  224.         cursor.Y--;
  225.         SetConsoleCursorPosition(hConsole, cursor);
  226.         for(idx=0; idx < 4; idx++)
  227.             putchar('+');
  228.         cursor.Y++;
  229.         SetConsoleCursorPosition(hConsole, cursor);
  230.         putchar('+');
  231.         cursor.Y++;
  232.         SetConsoleCursorPosition(hConsole, cursor);
  233.         for(idx=0; idx < 4; idx++)
  234.             putchar('+');
  235.         cursor.Y--;
  236.         cursor.X += 3;
  237.         SetConsoleCursorPosition(hConsole, cursor);
  238.         putchar('+');
  239.  
  240.         last_c = option;
  241.     }
  242.     /* Escolher CORES */
  243.  
  244.     cursor.X = 69;
  245.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
  246.     /* Escolher OBJECT */
  247.     if( last_w == 0 )
  248.         PrintTypes();
  249.  
  250.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);
  251.     if( option == 'a' || option == 's' || option == 'd' || option == 'f' ) {
  252.  
  253.         if( last_w != option && last_w != 0) {
  254.             cursor.Y = CursorYconvertTYPE(last_w);
  255.             cursor.X--;
  256.             cursor.Y--;
  257.             SetConsoleCursorPosition(hConsole, cursor);
  258.             for(idx=0; idx < 3; idx++)
  259.                 putchar(' ');
  260.             cursor.Y++;
  261.             SetConsoleCursorPosition(hConsole, cursor);
  262.             putchar(' ');
  263.             cursor.Y++;
  264.             SetConsoleCursorPosition(hConsole, cursor);
  265.             for(idx=0; idx < 3; idx++)
  266.                 putchar(' ');
  267.             cursor.Y--;
  268.             cursor.X += 2;
  269.             SetConsoleCursorPosition(hConsole, cursor);
  270.             putchar(' ');
  271.  
  272.             cursor.X = 69;
  273.         }
  274.         cursor.Y = CursorYconvertTYPE(option);
  275.         cursor.X--;
  276.         cursor.Y--;
  277.         SetConsoleCursorPosition(hConsole, cursor);
  278.         for(idx=0; idx < 3; idx++)
  279.             putchar('+');
  280.         cursor.Y++;
  281.         SetConsoleCursorPosition(hConsole, cursor);
  282.         putchar('+');
  283.         cursor.Y++;
  284.         SetConsoleCursorPosition(hConsole, cursor);
  285.         for(idx=0; idx < 3; idx++)
  286.             putchar('+');
  287.         cursor.Y--;
  288.         cursor.X += 2;
  289.         SetConsoleCursorPosition(hConsole, cursor);
  290.         putchar('+');
  291.  
  292.         last_w = option;
  293.     }
  294.     /* Escolher OBJECT */
  295.  
  296.     SetConsoleCursorPosition(hConsole, cursor_s.pos);
  297.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  298. }
  299. int CursorYconvertCOLOUR(char option) {
  300.     char cursorY = 0;
  301.     if(option == 'q')   // GREEN
  302.         cursorY = 7;
  303.     else    if( option == 'w' ) // RED
  304.         cursorY = 11;
  305.     else    if( option == 'e')  // BLUE
  306.         cursorY = 15;
  307.     return cursorY;
  308. }
  309. int CursorYconvertTYPE(char option) {
  310.     char cursorY = 0;
  311.     if(option == 'a')  
  312.         cursorY = 7;
  313.     else    if( option == 's' )
  314.         cursorY = 10;
  315.     else    if( option == 'd') 
  316.         cursorY = 13;
  317.     else    if( option == 'f' )
  318.         cursorY = 16;
  319.     return cursorY;
  320. }
  321. int ReadConsoleL(char *c){
  322.     char ch;
  323.     *c = getch();
  324.     ch = *c;
  325.     switch(ch) {
  326.     case 'q':
  327.     case 'w':
  328.     case 'e': return 2;
  329.     case 'a':
  330.     case 's':
  331.     case 'd':
  332.     case 'f': return 1;
  333.     case 'h':
  334.     case 'j':
  335.     case 'i':
  336.     case 'p':
  337.     case 'k':
  338.     case 'l':
  339.     case 32:  return 0;
  340.     case 'y': return 5;
  341.     case 'z': return 10;
  342.     case 'x': return 11;
  343.     default: return -1;
  344.     }
  345. }
  346. struct tagScreen_Cursor ScreenPaint(struct tagScreen_Cursor cursor_s, char option) {
  347.     if(cursor_s.colour != 0 && cursor_s.type != 0) {
  348.         if( option == 'j' && cursor_s.pos.X > 4)
  349.             cursor_s.pos.X--;
  350.         else    if( option == 'k' && cursor_s.pos.Y < 21)
  351.             cursor_s.pos.Y++;
  352.         else    if( option == 'i' && cursor_s.pos.Y > 5 )
  353.             cursor_s.pos.Y--;
  354.         else    if( option == 'l' && cursor_s.pos.X < 58)
  355.             cursor_s.pos.X++;
  356.         else    if(option == 'h' )
  357.             putchar(' ');
  358.         else    if( option == 32 ) {
  359.             if( cursor_s.colour == 'q' )
  360.                 SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
  361.             else    if( cursor_s.colour == 'w' )
  362.                 SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
  363.             else    if( cursor_s.colour == 'e' )
  364.                 SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
  365.             ConvertNumtoType(&cursor_s.type);
  366.             printf("%c", cursor_s.type);
  367.             SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED| FOREGROUND_GREEN);
  368.         }
  369.         SetConsoleCursorPosition(hConsole, cursor_s.pos);
  370.     }
  371.  
  372.     return cursor_s;
  373. }
  374. void ConvertNumtoType(char *c) {
  375.     if(*c == 'a')  
  376.         *c = 219;
  377.     else    if( *c == 's' )
  378.         *c = 220;
  379.     else    if( *c == 'd') 
  380.         *c = 223;
  381.     else    if( *c == 'f' )
  382.         *c = 254;
  383. }
  384. void PrintInstructions(void) {
  385.     COORD cursor;
  386.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  387.  
  388.     cursor.X = 1;
  389.     cursor.Y = 24;
  390.     SetConsoleCursorPosition(hConsole, cursor);
  391.     printf("Cores: q, w, e; | Blocos: a, s, d, f; | Pintar: Espaco; | Apagar: h; | Sair: y");
  392.  
  393.     cursor.X = 1;
  394.     cursor.Y = 1;
  395.     SetConsoleCursorPosition(hConsole, cursor);
  396.     printf("Criado por: ");
  397.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
  398.     printf("Leandro Soares");
  399.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  400.     printf(", TGPSI10/01");
  401.  
  402.     cursor.X = 50;
  403.     cursor.Y = 1;
  404.     SetConsoleCursorPosition(hConsole, cursor);
  405.     printf("Move: j, k, i, l; | Novo: p;");
  406. }
  407. void PrintLoadingBar(void) {
  408.     COORD cursor, loading, loading_txt;
  409.     int idx, idx2;
  410.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  411.  
  412.     PrintInstructions();
  413.  
  414.     loading_txt.X = 52;
  415.     loading_txt.Y = 10;
  416.     loading.X = 20;
  417.     loading.Y = cursor.Y = 10;
  418.     SetConsoleCursorPosition(hConsole, loading_txt);
  419.     printf("Loading");
  420.     loading_txt.X += 7;
  421.     for(idx=0, cursor.X = 24; idx <= 25; idx++, cursor.X++) {
  422.         SetConsoleCursorPosition(hConsole, loading);
  423.         printf("%d%% ", idx*4);
  424.         Sleep(200);
  425.         SetConsoleCursorPosition(hConsole, cursor);
  426.         putchar(219);
  427.  
  428.         SetConsoleCursorPosition(hConsole, loading_txt);
  429.         putchar('.');
  430.         loading_txt.X++;
  431.         if(idx % 3 == 0 && idx != 0) {
  432.             for(idx2=0; idx2 < 3; idx2++) {
  433.                 loading_txt.X--;
  434.                 SetConsoleCursorPosition(hConsole, loading_txt);
  435.                 putch(' ');
  436.             }
  437.         }
  438.  
  439.     }
  440.  
  441.     cursor.X = 22;
  442.     cursor.Y = 15;
  443.     SetConsoleCursorPosition(hConsole, cursor);
  444.     Pausa_S();
  445.     system("cls");
  446. }
  447. void Pausa_S(void) {
  448.     printf("Clique numa tecla para continuar...");
  449.     fflush(stdin);
  450.     getch();
  451. }
  452. void New_Paint(void) {
  453.     COORD cursor;
  454.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  455.  
  456.     cursor.Y = 5;
  457.  
  458.     while(cursor.Y != 21) {
  459.         cursor.X = 4;
  460.         while(cursor.X != 58) {
  461.             SetConsoleCursorPosition(hConsole, cursor);
  462.             putch(' ');
  463.             cursor.X++;
  464.         }
  465.         cursor.Y++;
  466.     }
  467. }
  468. struct tagS_Cursor *NewNode(void) {
  469.     struct tagS_Cursor *New;
  470.  
  471.     New = (struct tagS_Cursor *) malloc(sizeof(struct tagS_Cursor));
  472.     New->colour = 0;
  473.     New->pnext = NULL;
  474.     New->pos.X = 0;
  475.     New->pos.Y = 0;
  476.     New->type = 0;
  477.     New->pprev = NULL;
  478.  
  479.     return New;
  480. }
  481. void InserirNovoNo(struct tagS_Cursor **phead, struct tagS_Cursor *pnew) {
  482.     if( *phead != NULL ) {
  483.         pnew->pnext = *phead;
  484.         (*phead)->pprev = pnew;
  485.     }
  486.     *phead = pnew;
  487. }
  488. void PrintCredits(void) {
  489.     COORD cursor, cursor2;
  490.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  491.  
  492.     DrawBox(10,11,55,4);
  493.  
  494.     cursor.X = 31;
  495.     cursor.Y = 13;
  496.     SetConsoleCursorPosition(hConsole, cursor);
  497.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
  498.     printf("Volte Sempre!");
  499.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  500.     getch();
  501.  
  502.     cursor.X = 1;
  503.     cursor.Y = 20;
  504.     SetConsoleCursorPosition(hConsole, cursor);
  505. }
  506. void DrawBox(int ini_x, int ini_y, int quant_x, int quant_y) {
  507.     COORD cursor, cursor2;
  508.  
  509.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  510.  
  511.     cursor.X = ini_x;
  512.     cursor2.X = ini_x;
  513.     cursor.Y = ini_y;
  514.     cursor2.Y = ini_y + quant_y;
  515.     for(; cursor.X < ini_x + quant_x && cursor2.X < ini_x + quant_x ; cursor.X++, cursor2.X++) {
  516.         SetConsoleCursorPosition(hConsole, cursor);
  517.         putchar(219);
  518.         SetConsoleCursorPosition(hConsole, cursor2);
  519.         putchar(219);
  520.     }
  521.  
  522.     cursor.X = ini_x;
  523.     cursor.Y = ini_y;
  524.     for(; cursor.Y <= ini_y + quant_y ; cursor.Y++) {
  525.         cursor.X = ini_x;
  526.         SetConsoleCursorPosition(hConsole, cursor);
  527.         putchar(219);
  528.  
  529.         cursor.X = ini_x + quant_x;
  530.         SetConsoleCursorPosition(hConsole, cursor);
  531.         putchar(219);
  532.     }
  533.  
  534.     cursor.X = ini_x;
  535.     cursor.Y = ini_y;
  536.     SetConsoleCursorPosition(hConsole, cursor);
  537. }
  538. void Add_ext(char *sname) {
  539.     while( *sname != '\0' ) sname++;
  540.     *(sname) = '.';
  541.     *(sname+1) = 'p';
  542.     *(sname+2) = 'd';
  543.     *(sname+3) = 'o';
  544.     *(sname+4) = 's';
  545.     *(sname+5) = '\0';
  546. }
  547. int PrintWrite_to_binary(struct tagS_Cursor **phead, char name[]) {
  548.     struct tagS_Cursor *paux;
  549.     int rtrn;
  550.     FILE *foutput;
  551.  
  552.     foutput = fopen(name, "wb");
  553.     paux = *phead;
  554.  
  555.     if( paux != NULL ) {
  556.         while( paux != NULL ) {
  557.             fwrite(&paux, sizeof(struct tagS_Cursor), 1, foutput);
  558.  
  559.            
  560.             paux = paux->pnext;
  561.         }
  562.         fwrite("\n", sizeof("\n"), 1, foutput);
  563.         rtrn = 1;
  564.     }
  565.     else
  566.         rtrn = 0;
  567.  
  568.     fclose(foutput);
  569.     return rtrn;
  570. }
  571. void ImEx_portPrint(struct tagS_Cursor **phead, char type[], struct tagScreen_Cursor cursor_s) {
  572.     char name[50];
  573.     COORD cursor, cursor2;
  574.  
  575.     name[0] = '\0';
  576.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  577.     system("cls");
  578.  
  579.     DrawBox(10,11,55, 4);
  580.     PrintInstructions();
  581.  
  582.     cursor.X = 15;
  583.     cursor.Y = 9;
  584.     SetConsoleCursorPosition(hConsole, cursor);
  585.     SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED);
  586.     if( !strcmp(type, "Import") )
  587.         printf("Ler");
  588.     else    if( !strcmp(type, "Export") )
  589.         printf("Gravar");
  590.     printf(" Ficheiro Paint (.pdos)");
  591.  
  592.     cursor.X = 20;
  593.     cursor.Y = 13;
  594.     SetConsoleCursorPosition(hConsole, cursor);
  595.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
  596.     printf("Ficheiro: ");
  597.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  598.     gets(name);
  599.     Add_ext(name);
  600.  
  601.     if( !strcmp(type, "Import") )
  602.         PrintRead_from_binary(&(*phead), name);
  603.     else    if( !strcmp(type, "Export") )
  604.         PrintWrite_to_binary(&(*phead), name);
  605.  
  606.     DrawBox(10,11,55, 4);
  607.     PrintInstructions();
  608.  
  609.     cursor.X = 15;
  610.     cursor.Y = 9;
  611.     SetConsoleCursorPosition(hConsole, cursor);
  612.     SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED);
  613.     printf("Gravar Ficheiro Paint (.pdos)");
  614.  
  615.     cursor.X = 25;
  616.     cursor.Y = 13;
  617.     SetConsoleCursorPosition(hConsole, cursor);
  618.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
  619.     if( !strcmp(type, "Import") )
  620.         printf("Ficheiro Lido!");
  621.     else    if( !strcmp(type, "Export") )
  622.         printf("Ficheiro Criado!");
  623.  
  624.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  625.  
  626.     cursor.X = 5;
  627.     cursor.Y = 20;
  628.     SetConsoleCursorPosition(hConsole, cursor);
  629.     Pausa_S();
  630.  
  631.     ResetScreen(cursor_s);
  632. }
  633. int PrintRead_from_binary(struct tagS_Cursor **phead, char *string) {
  634.     FILE *finput;
  635.     struct tagS_Cursor *paux, aux;
  636.     char type = '\0';
  637.  
  638.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  639.     paux = NULL;
  640.     finput = fopen(string, "r+b");
  641.  
  642.     while( fread(&aux, sizeof(struct tagS_Cursor), 1, finput) != EOF ) {
  643.         paux = &aux;
  644.         InserirNovoNo(&(*phead), paux);
  645.         SetConsoleCursorPosition(hConsole, aux.pos);
  646.  
  647.         switch(aux.colour) {
  648.         case 7: SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); break;
  649.         case 11: SetConsoleTextAttribute(hConsole, FOREGROUND_RED); break;
  650.         case 15: SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); break;
  651.         }
  652.  
  653.         switch(aux.type) {
  654.         case 7: type = 219; break;
  655.         case 10: type = 220; break;
  656.         case 13: type = 223; break;
  657.         case 16: type = 254; break;
  658.         }
  659.         putchar(type);
  660.     }
  661.  
  662.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_GREEN | FOREGROUND_RED);
  663.     aux.pos.X = 4;
  664.     aux.pos.Y = 5;
  665.     SetConsoleCursorPosition(hConsole, aux.pos);
  666.  
  667.     fclose(finput);
  668. }
  669. int Search_and_Del_Cursor(struct tagS_Cursor **phead, COORD cursor) {
  670.     struct tagS_Cursor *paux, *paux2;
  671.     int control = 0;;
  672.     paux = *phead;
  673.  
  674.     while( paux != NULL ) {
  675.         if( paux->pos.X == cursor.X && paux->pos.Y == cursor.Y ) {
  676.             control = 1;
  677.  
  678.             if( paux->pprev == NULL ) {
  679.                 paux->pnext->pprev = NULL;
  680.                 *phead = paux->pnext;
  681.             }
  682.             if( paux->pnext == NULL )
  683.                 paux->pprev->pnext = NULL;
  684.             if( paux->pnext != NULL && paux->pprev != NULL ) {
  685.                 paux2 = paux->pnext;
  686.                 paux->pprev = paux2;
  687.                 paux2->pprev = paux;
  688.             }
  689.  
  690.             free(paux);
  691.             break;
  692.         }
  693.  
  694.         paux = paux->pnext;
  695.     }
  696.  
  697.     return control;
  698. }
  699. void PrintColours(void) {
  700.     COORD cursor;
  701.  
  702.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  703.  
  704.     cursor.X = 63;
  705.  
  706.     cursor.Y = 7;
  707.     SetConsoleCursorPosition(hConsole, cursor);
  708.     SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN );
  709.     putchar(219);
  710.     putchar(219);
  711.  
  712.     cursor.Y = 11;
  713.     SetConsoleCursorPosition(hConsole, cursor);
  714.     SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
  715.     putchar(219);
  716.     putchar(219);
  717.  
  718.     cursor.Y = 15;
  719.     SetConsoleCursorPosition(hConsole, cursor);
  720.     SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE );
  721.     putchar(219);
  722.     putchar(219);
  723.  
  724.     SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
  725. }
  726. void PrintTypes(void) {
  727.     COORD cursor;
  728.  
  729.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  730.  
  731.     cursor.X = 69;
  732.  
  733.     cursor.Y = 7;
  734.     SetConsoleCursorPosition(hConsole, cursor);
  735.     putchar(219);
  736.  
  737.     cursor.Y = 10;
  738.     SetConsoleCursorPosition(hConsole, cursor);
  739.     putchar(220);
  740.  
  741.     cursor.Y = 13;
  742.     SetConsoleCursorPosition(hConsole, cursor);
  743.     putchar(223);
  744.  
  745.     cursor.Y = 16;
  746.     SetConsoleCursorPosition(hConsole, cursor);
  747.     putchar(254);
  748. }
  749. void ResetScreen(struct tagScreen_Cursor cursor_s) {
  750.     COORD cursor;
  751.    
  752.     system("cls");
  753.     PrintScreen();
  754.     PrintMenu_right('\0', cursor_s);
  755.     PrintInstructions();
  756.     PrintColours();
  757.     PrintTypes();
  758.  
  759.     cursor.Y = 5;
  760.     cursor.X = 4;
  761.     SetConsoleCursorPosition(hConsole, cursor);
  762. }
  763. void ResetStruct(struct tagS_Cursor **phead) {
  764.     while( *phead != NULL ) {
  765.         Search_and_Del_Cursor(&phead, (*phead)->pos );
  766.         *phead = (*phead)->pnext;
  767.     }
  768. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement