Neste exemplo foi utilizado a função SetConsoleCursorPosition

#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.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 hI, HINSTANCE hPI, PSTR szCL, int iCS) {
system("title SetConsoleTitle");
setlocale(LC_ALL, "ptb");
static TCHAR szAppName[] = TEXT("SetConsoleTitle");
MSG msg;
HWND hwnd;
HBITMAP imagem;
HBRUSH hBrush;
imagem = LoadBitmap(hI, IMAGEM);
hBrush = CreatePatternBrush(imagem);
DeleteObject(imagem);
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hI;
wndclass.hIcon = LoadIcon(hI, IDI_ICON);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = hBrush;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT ("Error!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(
szAppName, TEXT("gotoxy(x, y);"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 290, 230,
NULL, NULL, hI, NULL
);
ShowWindow(hwnd, iCS);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lP) {
switch(msg) {
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) {
SetConsoleTitle("LEFT");
system("color 3e");
char ch;
gotoxy(x, y);
printf("LEFT");
ch = 75;
switch(ch) {
case 75:
x--;
Beep(567, 800);
if(x==-1) {
MessageBox(NULL, "Error!", "Important", MB_OK);
exit(0);
}
break;
case 27:
exit(0);
}
system("cls");
gotoxy(x, y);
printf("LEFT");
}
if(LOWORD(wParam)==RIGHT) {
SetConsoleTitle("RIGHT");
system("color 3e");
char ch;
gotoxy(x, y);
printf("RIGHT");
ch = 77;
switch(ch) {
case 77:
x++;
Beep(700, 532);
break;
case 27:
exit(0);
}
system("cls");
gotoxy(x, y);
printf("RIGHT");
}
if(LOWORD(wParam)==UP) {
SetConsoleTitle("UP");
system("color 3e");
char ch;
gotoxy(x, y);
printf("UP");
ch = 72;
switch(ch) {
case 72:
y--;
Beep(700, 700);
if(y==-1) {
MessageBox(NULL, "Error!", "Important", MB_OK);
exit(0);
}
break;
case 27:
exit(0);
}
system("cls");
gotoxy(x, y);
printf("UP");
}
if(LOWORD(wParam)==DOWN) {
SetConsoleTitle("DOWN");
system("color 3e");
char ch;
gotoxy(x, y);
printf("DOWN");
ch = 80;
switch(ch) {
case 80:
y++;
Beep(600, 532);
break;
case 27:
exit(0);
}
system("cls");
gotoxy(x, y);
printf("DOWN");
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lP);
}
Neste exemplo foi utilizado a função SetConsoleCursorPosition
/********************Arquivo: resource.h********************/
#define IDI_ICON 1
#define IMAGEM 2
Neste exemplo foi utilizado a função SetConsoleCursorPosition
/********************Arquivo: resource.rc********************/
#include "afxres.h"
#include "resource.h"
IDI_ICON ICON "project1.ico"
IMAGEM BITMAP DISCARDABLE "image.bmp"
Comentários
Postar um comentário