Creating static control with WS_EX_COMPOSITED fails

421 views Asked by At

I'm writing a Windows program in C, and I have a static control which contains a graph that you can click and drag the mouse on to select a range. However, the selection range flickers while you're dragging the mouse around. I learned from Googling this is a common issue, and the solution is to use the WS_EX_COMPOSITED style (overriding WM_ERASEBKGND was also suggested, but it doesn't solve it in my case). The problem is that when I activate this style, it causes CreateWindowEx to fail. Here is the line:

CreateWindowEx(WS_EX_COMPOSITED, WC_STATIC, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, graphXPos, graphYPos, GRAPH_WIDTH, GRAPH_HEIGHT, mainWindowHandle, NULL, NULL, NULL);

This worked fine when the window was created like this:

CreateWindow(WC_STATIC, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, graphXPos, graphYPos, GRAPH_WIDTH, GRAPH_HEIGHT, mainWindowHandle, NULL, NULL, NULL);

The error code CreateWindowEx fails with is 0x57 which means "this parameter is incorrect". The documentation says that WS_EX_COMPOSITED doesn't work with window classes that use the styles CS_OWNDC or CS_CLASSDC, so I used GetClassInfo to check if static controls use these styles, and they don't (they only use CS_DBLCLKS and CS_PARENTDC).

I don't know if this matters, but the window that this static control is a child of has the styles: WS_OVERLAPPED, WS_MINIMIZEBOX, WS_SYSMENU, WS_VISIBLE, WS_CLIPSIBLINGS. The window class it belongs to has only the CS_DBLCLKS style.

1

There are 1 answers

0
Vlad Feinstein On

Flicker is repeated painting of the same area with alternating contrast colors.

The solution to it is double-buffering. You paint everything onto off-screen memory DC, and BitBlt it at once.

More complicated alternative is to create a semi-transparent window on top of your graph and draw selection on it.