you need to do this in response to a paint msg or double buffer, which requires a persistent DC.
Some issues...(done without a complier so may not be 100% correct)
Code:
If you create a GDI object you must clear it (release if you used a 'Get' or delete if you used a 'create')
HDC dev = GetDC(hwnd);//here we 'get' a GDI object, allocating it memory (a copy) which must be 'release'd
//create new GDI
HPEN = Pen = CreatePen(...);//here we 'create' a new GDI object which must be 'delete'd
//select objects into into DC for use, catching the default GDI object 'pushed' out
HPEN *hOriginal = (HPEN*)SelectObject(dev , Pen);
//use HDC
//return to the same state original
SelectObject(dev , hOriginal );//put any original objects back in, pushing out ones we created
//clean up GDI objects
ReleaseDC(dev);
DeleteObject(Pen);
Calling for a paint
InvalidateRect() generates a WM_PAINT msg for the area in the rect.
Follow InvalidateRect() with UpdateWindow()
UpdateWindow() will make the paint msg non-queued (posted immediately to the windows callback, not sent to the OS msg queue, then app queue, then to the window, being ignored and concatinated along the way).
in the buttons handler just call for a paint and set up the string to draw
Code:
//in teh buttons BN_CLICKED handler
GetClientRect(hWnd, &ClientRect);
InvalidateRect(hWnd, ClientRect);
UpdateWindow(hWnd);
So if painting speed is not required.
Code:
Case WM_PAINT:
PAINTSTRUCT PS = {0};
rect ClientRect;
GetClientRect(hWnd, &ClientRect);
///fill in the paint details
BeginPaint(hWnd, &PS);can only be done in WM_PAINT and must have a EndPaint() call
//paint client area white to clear last text
FillRect(PS.hdc, &ClientRect, (HBRUSH)GetStockObject(WHITE_BRUSH));//easy as we dont have to clean up a stock object
//set text background
SetBkMode(PS.hdc, TRANSPARENT);
//adjust rect and get string
//paint text
DrawText(PS.hdc, poem.c_str(), poem.length(), &rec2, DT_CENTER);
//clean up
EndPaint(hWnd, &PS);//this will clean up the PS.hdc
To avoid flicker you will also probably want to return false from WM_ERASEBKGND msgs. (or is that true, been a few years and no IDE to check....)
Search for code on double buffering, I have answered that one many times in the last 10 years