WS_EX_COMPOSITED problem

Hello guys,

I've got a problem with WS_EX_COMPOSITED.
The window is not redrawing after resize if I use WS_EX_COMPOSITED in the parent window.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <Windows.h>
#include <windowsx.h>

#include <GL/GL.h>
#include <GL/GLU.h>

#pragma comment(lib, "OpenGL32.Lib")
#pragma comment(lib, "glu32.lib")

static float f = 0;

HANDLE hRotateThread = NULL;

void RotateQuad()
{
    for(;;)
    {
        f += 0.1f;
        Sleep(10);
    }
}

void CALLBACK MainRenderProc()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glRotatef(f, 0, 0, 1);

    glBegin(GL_QUADS);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f(0.5f, -0.5f);
    glVertex2f(0.5f, 0.5f);
    glVertex2f(-0.5f, 0.5f);
    glEnd();
}

LRESULT CALLBACK GlWndProc(HWND hWnd,
                           UINT message,
                           WPARAM wParam,
                           LPARAM lParam)
{
    static HDC hDC = NULL;
    static HGLRC hRC = NULL;

    switch(message)
    {
    case WM_CREATE:
    {
        PIXELFORMATDESCRIPTOR pfd = {0};
        OSVERSIONINFO osvi = {0};

        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;

        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

        if(!GetVersionEx(&osvi))
        {
            return FALSE;
        }

        if(osvi.dwMajorVersion > 6 ||
           (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 0))
        {
            pfd.dwFlags |=  PFD_SUPPORT_COMPOSITION;
        }

        if(!(hDC = GetDC(hWnd)))
        {
            return FALSE;
        }

        int pixelFormat = 0;

        if(!(pixelFormat = ChoosePixelFormat(hDC, &pfd)))
        {
            return FALSE;
        }

        if(!SetPixelFormat(hDC, pixelFormat, &pfd))
        {
            return FALSE;
        }

        if(!(hRC = wglCreateContext(hDC)))
        {
            return FALSE;
        }

        if(!wglMakeCurrent(hDC, hRC))
        {
            return FALSE;
        }

        glClearColor(0, 0, 0, 1);
        glPointSize(5);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glEnable(GL_LINE_SMOOTH);
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

        hRotateThread = CreateThread(NULL, 0,
                                     (LPTHREAD_START_ROUTINE)RotateQuad,
                                     NULL, 0, NULL);
        break;
    }
    case WM_SIZE:
    {
        int width = GET_X_LPARAM(lParam);
        int height = GET_Y_LPARAM(lParam);

        if(height == 0)
        {
            height = 1;
        }

        wglMakeCurrent(hDC, hRC);

        glViewport(0, 0, width, height);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        float aspectRatio = 0;

        if(width > height)
        {
            aspectRatio = width / float(height);
            gluOrtho2D(-aspectRatio, aspectRatio, 1, -1);
        }
        else
        {
            aspectRatio = height / float(width);
            gluOrtho2D(-1, 1, aspectRatio, -aspectRatio);
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        wglMakeCurrent(NULL, NULL);
        break;
    }
    case WM_MOUSEMOVE:
        InvalidateRect(hWnd, NULL, TRUE);
        UpdateWindow(hWnd);
        break;
    case WM_PAINT:
        PAINTSTRUCT ps;
        BeginPaint(hWnd,&ps);

        wglMakeCurrent(hDC, hRC);

        MainRenderProc();

        SwapBuffers(hDC);

        wglMakeCurrent(NULL, NULL);

        EndPaint(hWnd,&ps);
        break;
    case WM_CLOSE:
        TerminateThread(hRotateThread, 0),
                        DestroyWindow(hWnd);
        break;
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return 0;
}

LRESULT CALLBACK MainWndProc(HWND hWnd,
                             UINT message,
                             WPARAM wParam,
                             LPARAM lParam)
{
    static HWND hGlWnd = NULL;

    switch(message)
    {
    case WM_CREATE:
    {
        WNDCLASSEXW wc;

        wc.cbSize = sizeof(WNDCLASSEXW);
        wc.style = CS_OWNDC;
        wc.lpfnWndProc = GlWndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = 0;
        wc.hIcon = LoadIconW(NULL, MAKEINTRESOURCEW(IDI_APPLICATION));
        wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_ARROW));
        wc.hbrBackground = NULL;
        wc.lpszMenuName = NULL;
        wc.lpszClassName = L"GLWnd1";
        wc.hIconSm = LoadIconW(NULL, MAKEINTRESOURCEW(IDI_APPLICATION));

        RegisterClassExW(&wc);

        RECT parentRect;
        GetClientRect(hWnd, &parentRect);

        hGlWnd = CreateWindowExW(0,
                                 L"GLWnd1",
                                 L"GLWnd1",
                                 WS_CHILD,
                                 0,
                                 0,
                                 parentRect.right - parentRect.left,
                                 parentRect.bottom - parentRect.top,
                                 hWnd,
                                 NULL,
                                 ((LPCREATESTRUCTW)(lParam))->hInstance,
                                 NULL);

        ShowWindow(hGlWnd, SW_SHOWNORMAL);
        break;
    }
    case WM_SIZE:
    {
        SetWindowPos(hGlWnd,
                     NULL,
                     GET_X_LPARAM(lParam) / 2,
                     0,
                     GET_X_LPARAM(lParam) / 2,
                     GET_Y_LPARAM(lParam) / 2,
                     SWP_NOZORDER |
                     SWP_NOREPOSITION);

        SetWindowPos(hGlWnd,
                     NULL,
                     0,
                     0,
                     GET_X_LPARAM(lParam),
                     GET_Y_LPARAM(lParam),
                     SWP_NOZORDER |
                     SWP_NOREPOSITION);

        InvalidateRect(hGlWnd, NULL, TRUE);
        UpdateWindow(hGlWnd);
        break;
    }
    case WM_CLOSE:
        DestroyWindow(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    WNDCLASSEXW wc;

    wc.cbSize = sizeof(WNDCLASSEXW);
    wc.style = CS_VREDRAW | CS_HREDRAW;
    wc.lpfnWndProc = MainWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = 0;
    wc.hIcon = LoadIconW(NULL, MAKEINTRESOURCEW(IDI_APPLICATION));
    wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_ARROW));
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"TestApp1";
    wc.hIconSm = LoadIconW(NULL, MAKEINTRESOURCEW(IDI_APPLICATION));

    RegisterClassExW(&wc);

    HWND hWnd = CreateWindowExW(WS_EX_COMPOSITED,
                                wc.lpszClassName,
                                L"TestApp1",
                                WS_OVERLAPPEDWINDOW |
                                WS_CLIPCHILDREN |
                                WS_CLIPSIBLINGS,
                                0,
                                0,
                                1280,
                                720,
                                NULL,
                                NULL,
                                hInstance,
                                NULL);

    ShowWindow(hWnd, SW_SHOWNORMAL);

    MSG msg = {0};

    while(msg.message != WM_QUIT)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Sleep(1);
        }
    }

    return msg.wParam;
}


It only works if I replace WS_EX_COMPOSITED with 0 or
1
2
3
4
5
6
7
8
SetWindowPos(hGlWnd,
             NULL,
             GET_X_LPARAM(lParam) / 2,
             0,
             GET_X_LPARAM(lParam) / 2,
             GET_Y_LPARAM(lParam) / 2,
             SWP_NOZORDER |
             SWP_NOREPOSITION);

with
1
2
3
4
5
6
7
8
SetWindowPos(hGlWnd,
             NULL,
             0,
             0,
             GET_X_LPARAM(lParam),
             GET_Y_LPARAM(lParam),
             SWP_NOZORDER |
             SWP_NOREPOSITION);
please help
Topic archived. No new replies allowed.