从字符串C ++创建控制台标题

时间:2014-10-31 19:52:55

标签: c++ visual-studio-2010

我正在尝试创建一个设置Console标题的类。 我的目的是摆脱"系统(标题"某事")"。 相反,我想使用SetConsoleTitle。 我的问题是我可以在标题中设置我知道的字符串,但我不能使用我的字符串变量。这是我的代码:

int title(string Title){
TCHAR szOldTitle[MAX_PATH];
TCHAR szNewTitle[MAX_PATH];
GetConsoleTitle(szOldTitle, MAX_PATH) 
StringCchPrintf(szNewTitle, MAX_PATH, TEXT(Title), szOldTitle);
SetConsoleTitle(szNewTitle);}

任何人都知道如何将我的字符串Title设置为我命令行的标题?

1 个答案:

答案 0 :(得分:0)

我终于想到了自己:D 感谢大家的帮助。 对于想要设置控制台标题的每个人,就像我打算测试此代码一样。适合我。比系统快5000倍("标题文字");



bool Title(int buffersizex){
	color(1, 1, 1, 1);
	COORD top = { 0, 0 };
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(console, top);
	int flr = 177;
	int ln = 179;
	char filler = flr;
	char line = ln;
	int csitze = buffersizex;
	string text = "Buble for sorting Arrays";
	int lenght = csitze - text.length() + 2;
	double free = lenght / 2;
	int print = free;
	int add = csitze - (print * 2 + text.length() + 2);
	for (int z = 0; z < print; z++){ cout << filler; }
	cout << line << text << line;
	for (int z = 0; z < (print + add); z++){ cout << filler; }

	return true;
}
&#13;
&#13;
&#13; 我希望这有助于每个人。 如果你不相信我,请运行:

&#13;
&#13;
void TitleSpeedtest(int Checknums){
	int starttim;
	int endttim;
	double runnum = Checknums;
	starttim = clock();
	for (int n = 0; n <runnum; n++){
		title(to_string(clock()));		//run my function runnum times
	}
	endttim = clock();
	double complete = endttim - starttim;
	double single = complete / runnum;
	cout << "My function needs for changing the title:	" << single << " millis" << endl;
	int starttim2;
	int endttim2;
	starttim2 = clock();
	for (int n = 0; n <runnum; n++){
		system("title %time%");			//run the system version runnum times
	}
	endttim2 = clock();
	double complete2 = endttim2 - starttim2;
	double single2 = complete2 / runnum;
	cout << "The system Function needes for the same:	" << single2 << " millis" << endl;
	double result = complete2 / complete;
	cout << "In other Words, the system() function takes:	" << result << " times longer than mine"<<endl;
	stringstream titl;
	titl << "ONLY " << result << "TIMES";
	title(titl.str());
	sleep(5000);
	system("pause");
}
&#13;
&#13;
&#13;