I want to draw a line in a static control:
case WM_CREATE:
{
hgraph=CreateWindow(WC_STATIC,NULL,WS_CHILD|WS_VISIBLE|SS_CENTER,20,20,660,80,hWnd,NULL,NULL,NULL);
SendMessage(hgraph,WM_SETTEXT,NULL,(LPARAM) "My Static");
break;
}
case WM_PAINT:
{
hdc=GetDC(hgraph);
hp=CreatePen(0 ,5,RGB(0,100,0));
SelectObject(hdc,hp);
MoveToEx(hdc, 0, 0, 0);
LineTo(hdc, 100, 100);
ReleaseDC(hgraph, hdc);
}
break;
but it goes under the static control:

When drawing to any child window, you need to do your drawing within the
WM_PAINTof the child window procedure, not within theWM_PAINTof the parent window as you are doing.For system controls (e.g. statics), you need to subclass the window, which means that you need to replace the system-defined window procedure with your own. Once you have installed your own window procedure into the system control, you can catch the
WM_PAINTevent on the system control to do your painting.The complete procedure is as follows:
Define your Replacement Window Procedure for the Static Control.
We also must define a variable that we can use to store the original system Window Procedure for the control, which we must call at some point to allow the control to be drawn as normal.
Create your static control window.
Subclass your static control window (install your window procedure)
If this works correctly, then you should receive WM_PAINT messages inside your private message processing function for the static, and your drawing should occur correctly.