GetDCEx can be used to retrieve the device context for a window during processing of %WM_NCPAINT.
The documentation states that this is achieved by using
GetDCEx(hwnd, hrgn, %DCX_WINDOW | %DCX_INTERSECTRGN)
However, this call never works, because there is an undocumented flag to include which is not mentioned anywhere.
And this is how it works:
GetDCEx(hwnd, hrgn, DCX_WINDOW | DCX_INTERSECTRGN | 0x10000)
Source: Catch22.net (http://www.catch22.net/tuts/tips#UndocumentedGetDCEx)