Displaying Text on MDI child windows

This is a discussion on Displaying Text on MDI child windows within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to display text on child windows. I made a handler for double clicks to display "hello world" ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    Displaying Text on MDI child windows

    I am trying to display text on child windows. I made a handler for double clicks to display "hello world" on the current window, but it doesn't work. I have tried both DrawText and TextOut functions.

    Code:
    void CMDITest1View::OnLButtonDblClk(UINT nFlags, CPoint point) 
    {
    	CDC* dc;
    	char msg[] = "Hello World";
    
    	dc = GetDC();
            dc->TextOut(0, 0, msg, -1);
    
    	return;
    }
    Thanks for any help.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    try SetWindowText() and see if that's what you're looking for. If you need a handle to a dialog resource identifier you can use GetDlgItem() to get the HWND handle.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    Your arguments in TextOut are wrong ...

    Code:
    void CMDITest1View::OnLButtonDblClk(UINT nFlags, CPoint point) 
    {
    	CDC* dc;
    	char msg[] = "Hello World";
    
    	dc = GetDC(hwnd);
            TextOut(dc, 0, 0, msg, sizeof(msg) / sizeof(msg[0]);
    
    	return;
    }
    Your GetDC is wrong !

    Your TextOut is wrong !
    -1 as argument is only valid in DrawText, not in TextOut, also you've forgot the device context as first argument !


    Greetz
    Greenhorn

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    Thank you. I got it to work with:

    Code:
    void CMDITest2View::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    	CClientDC dc(this);
    	CRect rect;
            CString string;
    
    	string = "Hello World!";
    	
    	GetClientRect(&rect);
    	dc.DrawText(string, -1, &rect, DT_LEFT);
    }

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    SetWindowText will not work as he wants. I've used it to set a windows title bar text. I haven't worked with MDI projects before, and a simple test didn't change the text.

    Since you are using MFC try this instead :

    Code:
    CDC* dc;
    CString msg = L"Hello World";
    
    dc = GetDC();
    dc->TextOut(0,0, msg);

    You found an answer before I could get mine posted.

    Remember though, if your window gets covered by another window and then you bring it back to the front, the text will be erased by the WM_PAINT message. If you want it to stay around you need to do your TextOut inside the wm_paint handler.
    Last edited by DaveH; 10-30-2008 at 12:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  3. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM
  5. MDI and Child menu's
    By novacain in forum Windows Programming
    Replies: 0
    Last Post: 11-11-2001, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21