I am creating a game with the Win32 API and tried to draw a button by myself with this following code:
HWND button = CreateWindow("BUTTON", "", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 100, 100, 250, 40, hwnd, NULL, instance, NULL);
HDC dc = GetWindowDC(button);
TextOut(dc, 10, 10, "test", 5);
ReleaseDC(button, dc);
I want to clarify for everyone that I have tried for many situations and various functions, like FillRect, MoveTo, DrawText ... The point is it works in WM_CTLCOLORBTN, where I get the HDC of the button with wParam. I don't understand why it doesn't work. Maybe the GetDC function is buggy? Which function should I use? I specify that I tried with other functions like GetDC or BeginPaint/EndPaint. What I need to do??
CodePudding user response:
When using BS_OWNERDRAW you need to handle (and perform drawing) WM_DRAWITEM in the parent window. This is explicitly stated in the Button Styles documentation:
BS_OWNERDRAWCreates an owner-drawn button. The owner window receives aWM_DRAWITEMmessage when a visual aspect of the button has changed. Do not combine theBS_OWNERDRAWstyle with any other button styles.
The lParam of the WM_DRAWITEM message is a pointer to a DRAWITEMSTRUCT which contains, among other things, the HDC you must use for drawing.
hDCA handle to a device context; this device context must be used when performing drawing operations on the control.
