在c ++中,改变光标位置而不使用窗口句柄。 Qbasic模仿慢 [英] in c++, changing cursor location without using the windows handle. Qbasic imitation is slow
问题描述
有时我需要在屏幕的某个位置写东西(例如:第10列和第20行)。我在网上搜索,发现它是使用Windows处理程序是使用windows.h。
是使用手柄快速但有点复杂,所以我写了一个类只使用printf(字符串),并以适合屏幕的方式更改字符串,每个printf命令填充整个80x24控制台屏幕。该类模仿QBasic的CLS,LOCATE x,y和PRINT命令。
问题:有一个更简单的方法来到达屏幕上的任何位置,并放置一个字符或点像我这样的慢类?
我的QBASIC类非常慢,我每秒只能使用几次。
VC ++ 10.0 Windows XP
感谢您的时间。
:
// print_test.cpp:定义控制台应用程序的入口点。
//
#includestdafx.h
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
class QBASIC
{
public:
QBASIC()
{
for(int i = 0; i <(80 * 23); i ++)
{
default_fill [i] ='';
current_fill [i] ='';
}
default_fill [80 * 23] = NULL;
current_fill [80 * 23] = NULL;
row = 0;
column = 0;
window_number = 0;
}
void locate(int x,int y)
{
row = y;
column = x;
return;
}
void print(char * text)
{
int length = strlen(text);
for(int i = 0; i {
current_fill [row * 80 + column + i] = text [i]
}
row ++;
column = 0;
printf(%s,current_fill);
return;
}
void cls()
{
for(int i = 0; i <(80 * 24); i ++)
{
current_fill [ i] = default_fill [i];
}
column = 0;
row = 0;
printf(%s,current_fill);
}
void window(int x1,int y1,int x2,int y2,char * text,int id)
{
// 178 wall code
window_buffer [window_number] = new unsigned char [1000];
window_number ++;
for(int i = x1; i <(x2 + 1); i ++)
{
current_fill [i + 80 * y1] = 178;
current_fill [i + 80 * y2] = 178;
}
for(int j = y1; j <(y2 + 1); j ++)
{
current_fill [x1 + 80 * j]
current_fill [x2 + 80 * j] = 178;
}
int length = strlen(text); int temp_row = 0; int temp_column = 0;
for(int i = 0; i {
if(current_fill [(x1 + 1 + temp_column)+(y1 + 1 + temp_row)* 80 + != 178)
{
current_fill [(x1 + 1 + temp_column)+(y1 + 1 + temp_row)* 80 + i] = text [i]
}
else
{
temp_row ++;
temp_column = -i;
current_fill [(x1 + 1 + temp_column)+(y1 + 1 + temp_row)* 80 + i] = text [i]
}
}
printf(%s,current_fill);
return;
}
private:
unsigned char default_fill [80 * 23 + 1000];
unsigned char current_fill [80 * 23 + 1000];
int row,column;
unsigned char * window_buffer [10]; //最大窗口数= 10
int window_number;
};
int main()
{
QBASIC * mimic = new QBASIC();
mimic-> cls();
mimic-> locate(25,10);
mimic-> print(x < - 这里是第26列和第11行);
mimic-> locate(5,4);
mimic-> print(x < - 这里是第6列和第5行);
mimic-> locate(0,0);
mimic-> print(x< - here is origin);
mimic-> print(x < - 这里是后续打印);
mimic-> print(x< - here is another);
int window_id_1 = 0,window_id_2 = 1;
mimic-> window(20,5,28,9,this is a window,window_id_1);
mimic-> window(10,18,70,22,this is another window from 10,18 to 70,22,window_id_2);
delete mimic;
getchar(); //等待在退出之前看到屏幕。按enter键继续
return 0;
}
我认为没有解决方案。
void LOCATE (int x,int y)
{
COORD newPosition = {x,y};
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h,newPosition);
}
Sometimes i need to write things on a location of screen (ex: 10th column and 20th row). I searched on the net and found it is done with using windows handler which is using windows.h.
Yes, using handles are fast but somewhat complex so i wrote a class that uses only printf(string) and changes the string in a way that it fits to screen and every printf command fills the entire 80x24 console screen. The class imitates QBasic's CLS , LOCATE x,y and PRINT commands.
Question: Is there a simpler way to reach any location in screen and put a char or a dot(drawing) without using windows handle or a slow class like mine?
My QBASIC class is so slow that i can use it only several times per second. VC++ 10.0 windows XP
Thanks for your time.
Here is my class and some examples:
// print_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class QBASIC
{
public:
QBASIC()
{
for(int i=0;i<(80*23);i++)
{
default_fill[i]=' ';
current_fill[i]=' ';
}
default_fill[80*23]=NULL;
current_fill[80*23]=NULL;
row=0;
column=0;
window_number=0;
}
void locate(int x,int y)
{
row=y;
column=x;
return;
}
void print(char* text)
{
int length=strlen(text);
for(int i=0;i<length;i++)
{
current_fill[row*80+column+i]=text[i];
}
row++;
column=0;
printf("%s",current_fill);
return;
}
void cls()
{
for(int i=0;i<(80*24);i++)
{
current_fill[i]=default_fill[i];
}
column=0;
row=0;
printf("%s",current_fill);
}
void window(int x1,int y1,int x2,int y2,char*text,int id)
{
//178 wall code
window_buffer[window_number]=new unsigned char[1000];
window_number++;
for(int i=x1;i<(x2+1);i++)
{
current_fill[i+80*y1]=178;
current_fill[i+80*y2]=178;
}
for(int j=y1;j<(y2+1);j++)
{
current_fill[x1+80*j]=178;
current_fill[x2+80*j]=178;
}
int length=strlen(text);int temp_row=0;int temp_column=0;
for(int i=0;i<length;i++)
{
if(current_fill[(x1+1+temp_column)+(y1+1+temp_row)*80+i]!=178)
{
current_fill[(x1+1+temp_column)+(y1+1+temp_row)*80+i]=text[i];
}
else
{
temp_row++;
temp_column=-i;
current_fill[(x1+1+temp_column)+(y1+1+temp_row)*80+i]=text[i];
}
}
printf("%s",current_fill);
return;
}
private:
unsigned char default_fill[80*23+1000];
unsigned char current_fill[80*23+1000];
int row,column;
unsigned char *window_buffer[10];//max windows number=10
int window_number;
};
int main()
{
QBASIC *mimic=new QBASIC();
mimic->cls();
mimic->locate(25,10);
mimic->print("x <--here is 26th column and 11th row");
mimic->locate(5,4);
mimic->print("x <--here is 6th column and 5th row");
mimic->locate(0,0);
mimic->print("x <--here is origin");
mimic->print("x <--here is sequantial print after");
mimic->print("x <--here is another");
int window_id_1=0,window_id_2=1;
mimic->window(20,5,28,9,"this is a window",window_id_1);
mimic->window(10,18,70,22,"this is another window from 10,18 to 70,22",window_id_2);
delete mimic;
getchar();// waiting to see the screen before exiting. press enter to continue
return 0;
}
i think theres no solution.
Ok, I will continue using handles again.
void LOCATE(int x, int y)
{
COORD newPosition = { x, y };
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, newPosition);
}
这篇关于在c ++中,改变光标位置而不使用窗口句柄。 Qbasic模仿慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!