-
March 4th, 2004, 08:56 PM
#1
pointer of array show out problem
Dear All,
how to textout the value of pointer of array?
Thank u
-
March 4th, 2004, 09:00 PM
#2
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];
}
Last edited by defiler_z; March 4th, 2004 at 09:07 PM.
-
March 5th, 2004, 12:25 AM
#3
Originally quoted by lwong
how to textout the value of pointer of array?
Are you talking about the TextOut function in the CDC class?
TDM
-
March 5th, 2004, 01:46 AM
#4
-
March 5th, 2004, 02:22 AM
#5
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.
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' 10 pixels from the top and 10 pixels from the left of the client area defined by the DC.
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;
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.
Hope that helps.
TDM
Last edited by TDM; March 5th, 2004 at 02:25 AM.
-
March 5th, 2004, 02:37 AM
#6
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
-
March 5th, 2004, 03:17 AM
#7
TDM,
if the pointer of array is not the type of char, but is float, how to convert between them?
-
March 5th, 2004, 03:24 AM
#8
PHP 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();
};
Say you have
PHP Code:
double my_number = 7.9;
std::string as_string(convert_to_string(my_number, std::dec, 10));
is an example of doing the conversion.
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);
Last edited by souldog; March 5th, 2004 at 03:44 AM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 5th, 2004, 03:44 AM
#9
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.
Code:
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);
}
Hope that helps
TDM
-
March 5th, 2004, 03:54 AM
#10
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
Last edited by TDM; March 5th, 2004 at 04:05 AM.
-
March 5th, 2004, 03:55 AM
#11
CString
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 5th, 2004, 03:56 AM
#12
Originally quoted by souldog
CString
You don't like CString?
TDM
-
March 5th, 2004, 04:04 AM
#13
I was just kidding around. However, I try to minimize my use of
MFC as much as possible.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 5th, 2004, 04:44 AM
#14
Thanks TDM, this is what i want.
-
March 5th, 2004, 03:24 PM
#15
One thing to be aware of:
Originally posted by souldog
That's the correct way for obtaining a DC for painting from a WM_PAINT handler.
Originally posted by TDM
Code:
CClientDC 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.
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.
Last edited by gstercken; March 5th, 2004 at 04:53 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|