winapidrawtext

how to use DrawText() to write text in a given window whose handle is known?


I want to know how to write text on a particular window starting at a given location in the window using the Windows API.

For example if the coordinates within the window where the text is to be written are (x,y) = (40,10) then what do I need to do to write a line of text to a particular window at that location in the window?


Solution

  • Suppose your window name is "hwnd" and the text which u want to write on that window at x,y coordinate is say stored in "message" where

    LPCWSTR message=L"My First Window"; then

    RECT rect;
    HDC wdc = GetWindowDC(hwnd);
    GetClientRect (bgHandle, &rect) ;
    SetTextColor(wdc, 0x00000000);
    SetBkMode(wdc,TRANSPARENT);
    rect.left=40;
    rect.top=10;
    DrawText( wdc, message, -1, &rect, DT_SINGLELINE | DT_NOCLIP  ) ;
    DeleteDC(wdc);  
    

    Thats it.. remember this is just one example.