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

I am hoping this a more complete answer...

void OnPaint(HWND hWnd)
{
    RECT  rect;
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);

    GetClientRect(hWnd, &rect);
    SetTextColor(hdc, RGB(0xFF, 0x00, 0x00));
    SetBkMode(hdc, TRANSPARENT);
    rect.left = 40;
    rect.top = 10;
    DrawText(hdc, L"Hello World!", -1, &rect, DT_SINGLELINE | DT_NOCLIP);
    SelectObject(hdc, oldPen);
    DeleteObject(hPen);
    EndPaint(hWnd, &ps);
}

This would then be called from the WM_PAINT message in the WndProc.


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.

Tags:

Winapi