Display string output in a window using C (in WIN32 API)

I want me to be able to output a string of characters and display it in a created window. I used the textout () function, but since it only draws the window, as soon as the window is minimized and restored back, the data displayed in the window disappears. Also, when the data to be displayed exceeds the size of the window, only data equal to the size of the window is displayed, and other data is truncated. Is there any other way to output data to a window?

+4
source share
2 answers

You can put Static or Edit (shortcut and text field) in your window to display the data.

Call one of them 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 Edit , then the user will also be able to scroll and copy and paste text.

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

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

( Kurtasi from Daboyzuk )

+10
source

TextOut should work fine, if done in WM_PAINT, it needs to be drawn every time. (including minimization and recalibration)

 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_PAINT: PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); TextOut(hdc, 10, 10, TEXT("Text Out String"),strlen("Text Out String")); EndPaint(hWnd, &ps); ReleaseDC(hWnd, hdc); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } 

You may also be interested in DrawText

 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_PAINT: PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); RECT rec; // SetRect(rect, x ,y ,width, height) SetRect(&rec,10,10,100,100); // DrawText(HDC, text, text length, drawing area, parameters "DT_XXX") DrawText(hdc, TEXT("Text Out String"),strlen("Text Out String"), &rec, DT_TOP|DT_LEFT); EndPaint(hWnd, &ps); ReleaseDC(hWnd, hdc); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } 

What will draw the text in your window in the given rectangle,


The text will be drawn by Word Wrap inside this rectangle.
If you want your whole window as a drawing area, you can use GetClientRect(hWnd, &rec); instead of SetRect(&rec,10,10,100,100);

+5
source

Source: https://habr.com/ru/post/1469462/


All Articles