CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Posts
    218

    pointer of array show out problem

    Dear All,
    how to textout the value of pointer of array?
    Thank u

  2. #2
    Join Date
    Feb 2004
    Posts
    21
    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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

  4. #4
    Join Date
    Feb 2004
    Posts
    218
    Yes.TDM

  5. #5
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

  6. #6
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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

  7. #7
    Join Date
    Feb 2004
    Posts
    218
    TDM,
    if the pointer of array is not the type of char, but is float, how to convert between them?

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    PHP Code:
    template <class T>
    std::string convert_to_string(T tstd::ios_base & (*f)(std::ios_base&), const  std::streamsize precision)
    {
        
    std::ostringstream oss;
        
    oss <<std::fixed<<std::setprecision(precision)<< << t;
        return 
    oss.str();
    };

    //from std::string to a number
    template <class T>
    bool convert_from_string(&t, const std::string &sstd::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_numberstd::dec10)); 
    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_numberstd::dec10));

    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."

  9. #9
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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

  10. #10
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  11. #11
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

  12. #12
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Originally quoted by souldog
    CString
    You don't like CString?

    TDM

  13. #13
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  14. #14
    Join Date
    Feb 2004
    Posts
    218
    Thanks TDM, this is what i want.

  15. #15
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    One thing to be aware of:

    Originally posted by souldog
    Code:
    CPaintDC dc(this);
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured