11

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?

Carson
  • 6,105
  • 2
  • 37
  • 45

2 Answers2

14

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.

Abhineet
  • 6,459
  • 10
  • 35
  • 53
4

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.

ScottyMacDev
  • 163
  • 1
  • 8