Thread: Can't draw text to window

  1. #1
    Registered User
    Join Date
    Sep 2023
    Posts
    2

    Can't draw text to window

    I'm just trying to output the value of a variable to a window.

    Here is the function I run after a WM_PAINT message:

    Code:
    void OnPaint(HWND hWnd,MACHINE *machine)
    {
        RECT  rect;
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
    
        GetClientRect(hWnd, &rect);
        SetTextColor(hdc, RGB(0xFF, 0x00, 0x00));
        SetBkMode(hdc, TRANSPARENT);
        rect.left = 10;
        rect.top = 40;
        char reg[10] = { 0 };
        DrawText(hdc,( LPTSTR )(_itoa_s(machine->cpu.AddressBus, reg, 16)), -1, &rect, DT_SINGLELINE | DT_NOCLIP);
        // SelectObject(hdc, oldPen);&reg
         //DeleteObject(hPen);
        EndPaint(hWnd, &ps);
    I also do UpdateWindow(GetDesktopWindow)); in my main loop. Debug confirms the my paint routine is being run as it should.

    Blank window is all I get

    EDIT: Just realized UpdateWindow is being called, but after the initial window draw, WM_PAINT doesn't seem to be being sent. How do I force a redraw of the client area?
    Last edited by MoosTone; 09-02-2023 at 07:00 PM. Reason: New info as per edit

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    How many manual pages have you read?

    > _itoa_s(machine->cpu.AddressBus, reg, 16)
    _itoa_s, _itow_s functions | Microsoft Learn
    Some points.
    1. It returns errno_t, which makes NO sense to cast to a LPTSTR (this isn't the same as itoa).
    2. Most newer compilers default to UNICODE mode for calling the Windows API, so even trying to pass in a char array will get you junk.

    Code:
        GetClientRect(hWnd, &rect);
        SetTextColor(hdc, RGB(0xFF, 0x00, 0x00));
        SetBkMode(hdc, TRANSPARENT);
        DrawText....
    All of these return values - some more useful than others (like success/fail).
    It's worth checking all these things (at least in the debugger) when faced with "it doesn't work".

    > DrawText(hdc,( LPTSTR )(_itoa_s(machine->cpu.AddressBus, reg, 16)), -1, &rect, DT_SINGLELINE | DT_NOCLIP);#
    Speaking of which, try something basic like this.
    Code:
    DrawText(hdc,_T("Hello world"), -1, &rect, DT_SINGLELINE | DT_NOCLIP);
    If it works, your use of itoa_s is broken.
    If it doesn't, you need to dig deeper.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impossible to draw in an XCB window using Cairo
    By levenstein in forum Linux Programming
    Replies: 5
    Last Post: 07-02-2016, 09:31 AM
  2. C++ - win32: how can i draw correctly a transparent text?
    By joaquim in forum Game Programming
    Replies: 0
    Last Post: 07-21-2015, 10:15 AM
  3. Draw an hyperlink on the window
    By Niara in forum Windows Programming
    Replies: 4
    Last Post: 06-25-2007, 01:08 PM
  4. Window 1 causing text on window 2
    By Thantos in forum Windows Programming
    Replies: 4
    Last Post: 08-17-2003, 11:29 PM
  5. Direct Draw and Text
    By Mecnels in forum Game Programming
    Replies: 1
    Last Post: 03-14-2003, 08:12 AM

Tags for this Thread