Thread: Using TextOut() to display a number

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    14

    Question Using TextOut() to display a number

    So, I want to use the TextOut function to display an integer in my window.

    Here`s a piece of my code :

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    	int anumber = 100;
    	switch (message)
    	{
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    		switch (wmId)
    		{
    		case IDM_ABOUT:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		TextOut(hdc, 20, 20, L"Here is a number : " + anumber, 22);
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    I posted only this much because it would have been a waste of room the post the whole code.
    Can anybody help ?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> TextOut(hdc, 20, 20, L"Here is a number : " + anumber, 22);

    char* + int == char*

    Hint: use ssprintf, et al.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    14
    Don`t really know what you mean, but isn`t sprintf a function in C (I should have specified that my app is a Win32 C++ GUI-based one.) ? And ... what do you mean by "char* + int == char*" ?
    Last edited by Fujy; 04-02-2010 at 06:53 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to use sprintf (if you use C) or std::stringstream (if you prefer C++) to convert integer to string

    after that you use your current function to output text string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    14
    Thanks, works now.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Just as a note....

    Any variable declared in the callback has very limited scope (lifetime). Each message that is processed means a new 'copy' of the variable.

    If you want the variable to retain its value for the life of the app (not just for one individual message), use 'static' variables in the callback.

    EDIT: I advise you to use _snprintf() or similar versions. sprintf() may/will crash your app if the resultant string is equal to or exceeds the lenght of the char array.

    Code:
    char szBuf[3]={0};
    int    iNum=123;
    
    sprintf(sBuf, "%d", iNum);// this may crash your app
    
    _snprintf(sBuf, 2, "%d" , iNum); //will only print '12' but will not crash app
    Last edited by novacain; 04-06-2010 at 11:11 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM