Linguagem C - Controlando o Console pelo Modo Gráfico

Linguagem C - Controlando o Console pelo Modo Gráfico
#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"

#define LEFT  1
#define RIGHT 2
#define UP    3
#define DOWN  4

static int x = 30, y = 10;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void gotoxy(int, int);

void gotoxy(int x, int y) {
    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    static TCHAR szAppName[] = TEXT("About1");
    MSG msg;
    HWND hwnd;
    WNDCLASS wndclass;
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIcon(hInstance, szAppName);
    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) COLOR_INACTIVECAPTION;
    wndclass.lpszMenuName  = szAppName;
    wndclass.lpszClassName = szAppName;
    if (!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindow(
        szAppName, TEXT("Controla o Console"), WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 290, 230,
        NULL, NULL, hInstance, NULL
    );

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

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

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message) {
        case WM_CREATE:
            CreateWindow(
                TEXT("BUTTON"), TEXT("LEFT"), WS_VISIBLE | WS_CHILD,
                20, 80, 80, 20,
                hwnd, (HWND) LEFT, NULL, NULL
            );
            CreateWindow(
                TEXT("BUTTON"), TEXT("RIGHT"), WS_VISIBLE | WS_CHILD,
                160, 80, 80, 20,
                hwnd, (HWND) RIGHT, NULL, NULL
            );
            CreateWindow(
                TEXT("BUTTON"), TEXT("UP"), WS_VISIBLE | WS_CHILD,
                90, 40, 80, 20,
                hwnd, (HWND) UP, NULL, NULL
            );
            CreateWindow(
                TEXT("BUTTON"), TEXT("DOWN"), WS_VISIBLE | WS_CHILD,
                90, 120, 80, 20,
                hwnd, (HWND) DOWN, NULL, NULL
            );
            return 0;
        case WM_COMMAND:
            if(LOWORD(wParam)==LEFT) {
                //setlocale(LC_ALL, "ptb");
                SetConsoleTitle("Desenvolvedor APL Channel");
                system("color 1f");
                char ch;
                gotoxy(x, y);
                printf("\xdb\xdb");
                ch = 75;
                switch(ch) {
                    case 75: /**Direção: Para Esquerda*/
                        x--;
                        if(x==-1) {
                            MessageBox(NULL, "Você Errou!", "Importante", MB_OK);
                            exit(0);
                        }
                        break;
                    case 27:
                        exit(0);
                }
                system("cls");
                gotoxy(x, y);
                printf("\xdb\xdb");
            }
            if(LOWORD(wParam)==RIGHT) {
                //setlocale(LC_ALL, "ptb");
                SetConsoleTitle("Desenvolvedor APL Channel");
                system("color 1f");
                char ch;
                gotoxy(x, y);
                printf("\xdb\xdb");
                ch = 77;
                switch(ch) {
                    case 77: /**Direção: Para Direita*/
                        x++;
                        break;
                    case 27:
                        exit(0);
                }
                system("cls");
                gotoxy(x, y);
                printf("\xdb\xdb");
            }
            if(LOWORD(wParam)==UP) {
                //setlocale(LC_ALL, "ptb");
                SetConsoleTitle("Desenvolvedor APL Channel");
                system("color 1f");
                char ch;
                gotoxy(x, y);
                printf("\xdb\xdb");
                ch = 72;
                switch(ch) {
                    case 72: /**Direção: Para Direita*/
                        y--;
                        if(y==-1) {
                            MessageBox(NULL, "Você Errou!", "Importante", MB_OK);
                            exit(0);
                        }
                        break;
                    case 27:
                        exit(0);
                }
                system("cls");
                gotoxy(x, y);
                printf("\xdb\xdb");
            }
            if(LOWORD(wParam)==DOWN) {
                //setlocale(LC_ALL, "ptb");
                SetConsoleTitle("Desenvolvedor APL Channel");
                system("color 1f");
                char ch;
                gotoxy(x, y);
                printf("\xdb\xdb");
                ch = 80;
                switch(ch) {
                    case 80: /**Direção: Para Direita*/
                        y++;
                        break;
                    case 27:
                        exit(0);
                }
                system("cls");
                gotoxy(x, y);
                printf("\xdb\xdb");
            }
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

Comentários