Dear All,
how to textout the value of pointer of array?
Thank u
Printable View
Dear All,
how to textout the value of pointer of array?
Thank u
It is simple:
char *p = "hello world";
You can do that
for(i = 0; i < strlen(p); i++)
cout << p[i];
or
while(*p)
{
cout << *p;
p++;
}
With integer values you do
int mas[10];
int *p = mas;
for(i = 0; i < 10; i++)
{
cout << p[i];
}
Are you talking about the TextOut function in the CDC class?Quote:
Originally quoted by lwong
how to textout the value of pointer of array?
TDM
Yes.TDM
You need a pointer to a char array and x and y values that represents the upper left corner of where the text will be displayed.
The above would display the string 'Test string' 10 pixels from the top and 10 pixels from the left of the client area defined by the DC.Code:CClientDC dc(this);
char *Text=new char[128];
strcpy(Text,"Test string");
dc.TextOut(10,10,Text,strlen(Text));
delete []Text;
The above would display the string 'Test string' in the center of the client area in a Courier New font with a point size of 10.Code:CClientDC dc(this);
CRect WndRect;
GetClientRect(&WndRect);
char* Text=new char[128];
strcpy(Text,"Test string");
CFont NewFont;
NewFont.CreatePointFont(100,"Courier New",&DC);
CFont * OldFont=dc.SelectObject(&NewFont);
CSize TextSize=dc.GetTextExtent(Text,strlen(Text));
CPoint TextPoint;
TextPoint.cx=(WndRect.CenterPoint().x-(TextSize.cx/2));
TextPoint.cy=(WndRect.CenterPoint().y-(TextSize.cy/2));
dc.TextOut(TextPoint.cx,TextPoint.cy,Text,strlen(Text));
dc.SelectObject(OldFont);
delete []Text;
Hope that helps.
TDM
One thing that was not mentioned was what type of array you were using....float, char, int...etc. If you need to display a non-character array, tThe previous post would need to be modified to convert a non-char array to characters before TextOut will work.
TDM
TDM,
if the pointer of array is not the type of char, but is float, how to convert between them?
Say you havePHP Code:
template <class T>
std::string convert_to_string(T t, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::ostringstream oss;
oss <<std::fixed<<std::setprecision(precision)<< f << t;
return oss.str();
};
//from std::string to a number
template <class T>
bool convert_from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::istringstream iss(s);
return !(iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
};
is an example of doing the conversion.PHP Code:
double my_number = 7.9;
std::string as_string(convert_to_string(my_number, std::dec, 10));
PHP Code:
CPaintDC dc(this); // device context for painting
CRect WndRect;
GetClientRect(&WndRect);
dc.SaveDC();
double my_number = 1.22223444;
std::string as_string(convert_to_string(my_number, std::dec, 10));
CFont NewFont;
NewFont.CreatePointFont(100,"Courier New",&dc);
dc.SelectObject(&NewFont);
CSize TextSize=dc.GetTextExtent(as_string.c_str(), as_string.length());
CPoint TextPoint;
TextPoint.x=(WndRect.CenterPoint().x-(TextSize.cx/2));
TextPoint.y=(WndRect.CenterPoint().y-(TextSize.cy/2));
dc.TextOut(TextPoint.x,TextPoint.y,as_string.c_str(), as_string.length());
dc.RestoreDC(-1);
Hi,
Using the least complicated of my 2 previous examples: I assume you already have an array I'll use 'FloatArray' as the name in the example. You'll also need to update the X and Y values in the TextOut function somehow. Just for this example I'm going to keep the same Y and add 10 to the X in each iteration in the loop.
Hope that helpsCode:CClientDC dc(this);
int ArraySize=10,Xplace=10,YPlace=10;
CString Text;
for(int i=0;i<ArraySize;i++)
{
Text.Format("%f",FloatArray[i]);
dc.TextOut(XPlace+=10,YPlace,Text);
}
TDM
Putting scale labels on the tick marks of the graph? If you're doing this in OnPaint or OnDraw don't use CClientDC use the DC in those functions.
TDM
CString :rolleyes: :D
You don't like CString?Quote:
Originally quoted by souldog
CString :rolleyes: :D
TDM
I was just kidding around. However, I try to minimize my use of
MFC as much as possible.
Thanks TDM, this is what i want.
One thing to be aware of:
That's the correct way for obtaining a DC for painting from a WM_PAINT handler.Quote:
Originally posted by souldog
Code:CPaintDC dc(this);
Caution - that code uses an unprepared DC for drawing to the window's client area - it will be slower and might lead to flickering or even update problems, depending on where you call it.Quote:
Originally posted by TDM
Code:CClientDC dc(this);
Just in case you intend to copy the code as posted - watch out for that pitfall, and rather use the code as posted by souldog.