PDA

View Full Version : تغییر رنگ یه تیکه از متن تو cmd



Amir-Shabani
پنج شنبه 05 اسفند 1389, 09:49 صبح
سلام بر همه دوستان.

من تازه سی ++ رو شروع کردم و برای یه کاری به مشکل برخورد کردم.

زمانی که از system("color") استفاده کینم کل رنگ متن عوض میشه در حالی که من فقط میخوام یه تیکه از متن تغییر رنگ بده !!!! مثلا میخوام یه متن عین زیر بنویسم !!!! حالا باید چه کنم؟

Amir Shabani is a Student of CPP


با سپاس

kitten
پنج شنبه 05 اسفند 1389, 11:45 صبح
سلام دوست عزیز تا انجایی که من می دونم این کار انجام نمی شه
این کار را فقط در کامپایلر های قدیمی که سر فایل graphic دارند میشه انجام داد مثل Tc

drstrike
پنج شنبه 05 اسفند 1389, 14:43 عصر
زمانی که از system("color") استفاده کینم کل رنگ متن عوض میشه در حالی که من فقط میخوام یه تیکه از متن تغییر رنگ بده !!!! مثلا میخوام یه متن عین زیر بنویسم !!!! حالا باید چه کنم؟
Amir Shabani is a Student of CPP



#include <conio.h>
textcolor(4); cprintf("Amir Shabani");
textcolor(15); cprintf(" is a student of");
textcolor(2); cprintf("CPP");

Amir-Shabani
پنج شنبه 05 اسفند 1389, 15:47 عصر
با سپاس .

ولی جواب نداد!!! فکر کنم اینی که نوشتید برای سی هستش !!!! من ++ میخوام . باز هم سپاس !!!!!

mehdi.mousavi
پنج شنبه 05 اسفند 1389, 18:08 عصر
سلام.
بدین شکل عمل کنید:


int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if(!hConsoleOutput || hConsoleOutput == INVALID_HANDLE_VALUE)
return -1;

SetConsoleTextAttribute(hConsoleOutput, FOREGROUND_RED);
cout << "RED";

SetConsoleTextAttribute(hConsoleOutput, FOREGROUND_GREEN);
cout << "GREEN";

return 0;
}


موفق باشید.

پاورقی: کد در Visual Studio 2010 نوشته شده، اما باید بتونید اونو در نسخه های دیگه VS نیز Compile و اجرا کنید.

r00tkit
پنج شنبه 05 اسفند 1389, 20:55 عصر
استاد چرا TCHAR رو با _ نوشتید ؟ و همین طور tmain با وجود _ دیگه برنامه کار نمی کنه http://msdn.microsoft.com/en-us/library/cc842072.aspx

mehdi.mousavi
پنج شنبه 05 اسفند 1389, 21:19 عصر
استاد چرا TCHAR رو با _ نوشتید ؟ و همین طور tmain با وجود _ دیگه برنامه کار نمی کنه http://msdn.microsoft.com/en-us/library/cc842072.aspx

سلام.
وقتی _UNICODE رو define کرده باشید، بطور خودکار _tmain و _TCHAR به ترتیب به wmain و wchar_t
map میشن... (اینکه UNICODE و _UNICODE با هم فرقی دارن یا خیر، حقیقتش نمیدونم، اما من همواره هر دو رو در کدم تعریف میکردم).
در حال حاضر هم در VS2010 وقتی یه پروژه ایجاد می کنید، کد زیر ایجاد میشه و هر دو preprocessor نام برده، بطور خودکار تعریف میشن:



int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


موفق باشید.

drstrike
پنج شنبه 05 اسفند 1389, 22:14 عصر
با سپاس .

ولی جواب نداد!!! فکر کنم اینی که نوشتید برای سی هستش !!!! من ++ میخوام . باز هم سپاس !!!!!
:متعجب: جواب نداد!! نفرمائید دوست عزیز. ضمیمه رو تست کنید.
در ضمن کدش هم تو سی جوا میده هم سی++

Amir-Shabani
جمعه 06 اسفند 1389, 09:48 صبح
:متعجب: جواب نداد!! نفرمائید دوست عزیز. ضمیمه رو تست کنید.
در ضمن کدش هم تو سی جوا میده هم سی++

همینی رو که ضمیمه کردید اجرا کردم ولی یه ارور میگیره :

textcolor': identifier not found

الان داشتم تو یه سایت انگلیسی سرچ میکردم که یه بنده خدا هم با ارور من مواجه شده بود


can any one tell me how can i change text color in c++. i hav tried
everything
textcolor(7)
setcolor(7)
but everytime i type this compilor gives error
textcolor/setcolor is undefined variable. i hav include
conio.h

drstrike
جمعه 06 اسفند 1389, 10:54 صبح
همینی رو که ضمیمه کردید اجرا کردم ولی یه ارور میگیره :

ببخشید یادم رفت بگم که تو Borland C++ 3.1 یا 5.2 تست کنید. فک کنم شما با VS اجرا کردید، درسته؟

Amir-Shabani
جمعه 06 اسفند 1389, 13:41 عصر
با سپاس از همه!!!! بله من واسه vs میخواستم. یه کد خوب پیدا کردم و اون چیزی رو که میخواستم تونستم بسازم.

اینم اون چیزی که میخواستم :


// Flag and Nation.cpp : Defines the entry point for the console application.
// http://Asre-Digital.com

#include "stdafx.h"
#include <iostream>

#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin



int main()

{


HANDLE hConsole;

int nation;

int red=192,blue=16,green=32,black=0,white=240,yellow= 224,normal=15;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Iran=1 , Germany=2, France=3, Russia=4, Austria=5\n";
cout<<"Please choose 1..5"
cin>> nation ;
if (nation==1)
{
SetConsoleTextAttribute(hConsole, green);
cout<<" \n";
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}

if (nation==2)
{
SetConsoleTextAttribute(hConsole, black);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, yellow);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==3)
{
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==4)
{
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==5)
{
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
system("pause");
return 0;

}

m.soleimani
جمعه 06 اسفند 1389, 16:44 عصر
#include "stdafx.h"
#include <iostream>

#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin



int main()

{


HANDLE hConsole;

int nation;

int red=192,blue=16,green=32,black=0,white=240,yellow= 224,normal=15;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Iran=1 , Germany=2, France=3, Russia=4, Austria=5\n";
cout<<"Please choose 1..5"
cin>> nation ;
if (nation==1)
{
SetConsoleTextAttribute(hConsole, green);
cout<<" \n";
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}

if (nation==2)
{
SetConsoleTextAttribute(hConsole, black);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, yellow);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==3)
{
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" ";
SetConsoleTextAttribute(hConsole, white);
cout<<" ";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==4)
{
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, blue);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
if (nation==5)
{
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, white);
cout<<" \n";
SetConsoleTextAttribute(hConsole, red);
cout<<" \n";
SetConsoleTextAttribute(hConsole, normal);
}
system("pause");
return 0;

}

برای قرار دادن کد از تگ کد استفاده کنید موفق باشید./

moonDark
چهارشنبه 22 آذر 1391, 12:19 عصر
از این تابع میتونید استفاده کنید دیگه لازم نیست برای هربار تغییر رنگ دستور کاملش رو بنویسید
void setColor(int color) {
HANDLE hnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hnd, color);
}