Displaying String Output in a Window using C (in WIN32 API)

Ayse picture Ayse · Mar 15, 2013 · Viewed 13.2k times · Source

I want a proper way in which I can output a character string and display it on a Window created. I had been using textout() function, but since it only paints the window, once the window is minimized and restored back, the data displayed on the window disappears. Also when the data to be displayed is exceeds the size of Window, only the data equal to window size is displayed and other data is truncated. Is there any other way to output data on a Window?

Answer

Deanna picture Deanna · Mar 15, 2013

You can put a Static or an Edit control (Label and a text box) on your window to show the data.

Call one of these during WM_CREATE:

HWND hWndExample = CreateWindow("STATIC", "Text Goes Here", WS_VISIBLE | WS_CHILD | SS_LEFT, 10,10,100,100, hWnd, NULL, hInstance, NULL);

Or

HWND hWndExample = CreateWindow("EDIT", "Text Goes Here", WS_VISIBLE | WS_CHILD | ES_LEFT, 10,10,100,100, hWnd, NULL, hInstance, NULL);

If you use an Edit then the user will also be able to scroll, and copy and paste the text.

In both cases, the text can be updated using SetWindowText():

SetWindowText(hWndExample, TEXT("Control string"));

(Courtesy of Daboyzuk)