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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS uxtheme.dll
 * FILE:            dll/win32/uxtheme/themehooks.c
 * PURPOSE:         uxtheme user api hook functions
 * PROGRAMMER:      Giannis Adamopoulos
 */
 
#include "uxthemep.h"

USERAPIHOOK g_user32ApiHook;
BYTE gabDWPmessages[UAHOWP_MAX_SIZE];
BYTE gabMSGPmessages[UAHOWP_MAX_SIZE];
BYTE gabDLGPmessages[UAHOWP_MAX_SIZE];
BOOL g_bThemeHooksActive = FALSE;

PWND_DATA ThemeGetWndData(HWND hWnd)
{
    PWND_DATA pwndData;

    pwndData = (PWND_DATA)GetPropW(hWnd, (LPCWSTR)MAKEINTATOM(atWndContext));
    if(pwndData == NULL)
    {
        pwndData = HeapAlloc(GetProcessHeap(), 
                            HEAP_ZERO_MEMORY, 
                            sizeof(WND_DATA));
        if(pwndData == NULL)
        {
            return NULL;
        }
        
        SetPropW( hWnd, (LPCWSTR)MAKEINTATOM(atWndContext), pwndData);
    }

    return pwndData;
}

void ThemeDestroyWndData(HWND hWnd)
{
    PWND_DATA pwndData;
    DWORD ProcessId;

    /*Do not destroy WND_DATA of a window that belong to another process */
    GetWindowThreadProcessId(hWnd, &ProcessId);
    if(ProcessId != GetCurrentProcessId())
    {
        return;
    }

    pwndData = (PWND_DATA)GetPropW(hWnd, (LPCWSTR)MAKEINTATOM(atWndContext));
    if(pwndData == NULL)
    {
        return;
    }

    if(pwndData->HasThemeRgn)
    {
        g_user32ApiHook.SetWindowRgn(hWnd, 0, TRUE);
    }

    if (pwndData->hTabBackgroundBrush != NULL)
    {
        CloseThemeData(GetWindowTheme(hWnd));

        DeleteObject(pwndData->hTabBackgroundBrush);
    }

    if (pwndData->hTabBackgroundBmp != NULL)
    {
        DeleteObject(pwndData->hTabBackgroundBmp);
    }

    if (pwndData->hthemeWindow)
    {
        CloseThemeData(pwndData->hthemeWindow);
    }

    if (pwndData->hthemeScrollbar)
    {
        CloseThemeData(pwndData->hthemeScrollbar);
    }

    HeapFree(GetProcessHeap(), 0, pwndData);

    SetPropW( hWnd, (LPCWSTR)MAKEINTATOM(atWndContext), NULL);
}

HTHEME GetNCCaptionTheme(HWND hWnd, DWORD style)
{
    PWND_DATA pwndData;

    /* We only get the theme for the window class if the window has a caption */
    if((style & WS_CAPTION) != WS_CAPTION)
        return NULL;

    /* Get theme data for this window */
    pwndData = ThemeGetWndData(hWnd);
    if (pwndData == NULL)
        return NULL;

    if (!(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
    {
        if (pwndData->hthemeWindow)
        {
            CloseThemeData(pwndData->hthemeWindow);
            pwndData->hthemeWindow = NULL;
        }
        return NULL;
    }

    /* If the theme data was not cached, open it now */
    if (!pwndData->hthemeWindow)
        pwndData->hthemeWindow = OpenThemeDataEx(hWnd, L"WINDOW", OTD_NONCLIENT);

    return pwndData->hthemeWindow;
}

HTHEME GetNCScrollbarTheme(HWND hWnd, DWORD style)
{
    PWND_DATA pwndData;

    /* We only get the theme for the scrollbar class if the window has a scrollbar */
    if((style & (WS_HSCROLL|WS_VSCROLL)) == 0)
        return NULL;

    /* Get theme data for this window */
    pwndData = ThemeGetWndData(hWnd);
    if (pwndData == NULL)
        return NULL;

    if (!(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
    {
        if (pwndData->hthemeScrollbar)
        {
            CloseThemeData(pwndData->hthemeScrollbar);
            pwndData->hthemeScrollbar = NULL;
        }
        return NULL;
    }

    /* If the theme data was not cached, open it now */
    if (!pwndData->hthemeScrollbar)
        pwndData->hthemeScrollbar = OpenThemeDataEx(hWnd, L"SCROLLBAR", OTD_NONCLIENT);

    return pwndData->hthemeScrollbar;
}

static BOOL CALLBACK ThemeCleanupChildWndContext (HWND hWnd, LPARAM msg)
{
    ThemeDestroyWndData(hWnd);
    return TRUE;
}

static BOOL CALLBACK ThemeCleanupWndContext(HWND hWnd, LPARAM msg)
{
    if (hWnd == NULL)
    {
        EnumWindows (ThemeCleanupWndContext, 0);
    }
    else
    {
        ThemeDestroyWndData(hWnd);
        EnumChildWindows (hWnd, ThemeCleanupChildWndContext, 0);
    }

    return TRUE;
}

void SetThemeRegion(HWND hWnd)
{
    HTHEME hTheme;
    RECT rcWindow;
    HRGN hrgn, hrgn1;
    int CaptionHeight, iPart;
    WINDOWINFO wi;

    TRACE("SetThemeRegion %d\n", hWnd);

    wi.cbSize = sizeof(wi);
    GetWindowInfo(hWnd, &wi);

    /* Get the caption part id */
    if (wi.dwStyle & WS_MINIMIZE)
        iPart = WP_MINCAPTION;
    else if (wi.dwExStyle & WS_EX_TOOLWINDOW)
        iPart = WP_SMALLCAPTION;
    else if (wi.dwStyle & WS_MAXIMIZE)
        iPart = WP_MAXCAPTION;
    else
        iPart = WP_CAPTION;

    CaptionHeight = wi.cyWindowBorders;
    CaptionHeight += GetSystemMetrics(wi.dwExStyle & WS_EX_TOOLWINDOW ? SM_CYSMCAPTION : SM_CYCAPTION );

    GetWindowRect(hWnd, &rcWindow);
    rcWindow.right -= rcWindow.left;
    rcWindow.bottom = CaptionHeight;
    rcWindow.top = 0;
    rcWindow.left = 0;

    hTheme = GetNCCaptionTheme(hWnd, wi.dwStyle);
    GetThemeBackgroundRegion(hTheme, 0, iPart, FS_ACTIVE, &rcWindow, &hrgn);

    GetWindowRect(hWnd, &rcWindow);
    rcWindow.right -= rcWindow.left;
    rcWindow.bottom -= rcWindow.top;
    rcWindow.top = CaptionHeight;
    rcWindow.left = 0;
    hrgn1 = CreateRectRgnIndirect(&rcWindow);

    CombineRgn(hrgn, hrgn, hrgn1, RGN_OR );

    DeleteObject(hrgn1);

    g_user32ApiHook.SetWindowRgn(hWnd, hrgn, TRUE);
}

int OnPostWinPosChanged(HWND hWnd, WINDOWPOS* pWinPos)
{
    PWND_DATA pwndData;
    DWORD style;

    /* We only proceed to change the window shape if it has a caption */
    style = GetWindowLongW(hWnd, GWL_STYLE);
    if((style & WS_CAPTION)!=WS_CAPTION)
        return 0;

    /* Get theme data for this window */
    pwndData = ThemeGetWndData(hWnd);
    if (pwndData == NULL)
        return 0;

    /* Do not change the region of the window if its size wasn't changed */
    if ((pWinPos->flags & SWP_NOSIZE) != 0 && pwndData->DirtyThemeRegion == FALSE)
        return 0;

    /* We don't touch the shape of the window if the application sets it on its own */
    if (pwndData->HasAppDefinedRgn != FALSE)
        return 0;

    /* Calling SetWindowRgn will call SetWindowPos again so we need to avoid this recursion */
    if (pwndData->UpdatingRgn != FALSE)
        return 0;

    if(!IsAppThemed() || !(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
    {
        if(pwndData->HasThemeRgn)
        {
            pwndData->HasThemeRgn = FALSE;
            g_user32ApiHook.SetWindowRgn(hWnd, 0, TRUE);
        }
        return 0;
    }

    pwndData->DirtyThemeRegion = FALSE;
    pwndData->HasThemeRgn = TRUE;
    pwndData->UpdatingRgn = TRUE;
    SetThemeRegion(hWnd);
    pwndData->UpdatingRgn = FALSE;

     return 0;
 }

/**********************************************************************
 *      Hook Functions
 */

static LRESULT CALLBACK
ThemeDefWindowProcW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    PWND_DATA pwndData;

    pwndData = (PWND_DATA)GetPropW(hWnd, (LPCWSTR)MAKEINTATOM(atWndContext));

    if(!IsAppThemed() || 
       !(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT) ||
       (pwndData && pwndData->HasAppDefinedRgn))
    {
        return g_user32ApiHook.DefWindowProcW(hWnd, 
                                            Msg, 
                                            wParam, 
                                            lParam);
    }

    return ThemeWndProc(hWnd, 
                        Msg, 
                        wParam, 
                        lParam, 
                        g_user32ApiHook.DefWindowProcW);
}

static LRESULT CALLBACK
ThemeDefWindowProcA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    PWND_DATA pwndData;

    pwndData = (PWND_DATA)GetPropW(hWnd, (LPCWSTR)MAKEINTATOM(atWndContext));

    if(!IsAppThemed() || 
       !(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT) ||
       (pwndData && pwndData->HasAppDefinedRgn))
    {
        return g_user32ApiHook.DefWindowProcA(hWnd, 
                                            Msg, 
                                            wParam, 
                                            lParam);
    }

    return ThemeWndProc(hWnd, 
                        Msg, 
                        wParam, 
                        lParam, 
                        g_user32ApiHook.DefWindowProcA);
}

static LRESULT CALLBACK
ThemePreWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ret,PDWORD unknown)
{
    switch(Msg)
    {
        case WM_CREATE:
        case WM_STYLECHANGED:
        case WM_SIZE:
        case WM_WINDOWPOSCHANGED:
        {
            if(IsAppThemed() && (GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
                ThemeCalculateCaptionButtonsPos(hWnd, NULL);
            break;
        }
        case WM_THEMECHANGED:
        {
            PWND_DATA pwndData = ThemeGetWndData(hWnd);

            if (GetAncestor(hWnd, GA_PARENT) == GetDesktopWindow())
                UXTHEME_LoadTheme(TRUE);

            if (pwndData == NULL)
                return 0;

            if (pwndData->hTabBackgroundBrush != NULL)
            {
                DeleteObject(pwndData->hTabBackgroundBrush);
                pwndData->hTabBackgroundBrush = NULL;
            }

            if (pwndData->hTabBackgroundBmp != NULL)
            {
                DeleteObject(pwndData->hTabBackgroundBmp);
                pwndData->hTabBackgroundBmp = NULL;
            }

            if (pwndData->hthemeWindow)
            {
                CloseThemeData(pwndData->hthemeWindow);
                pwndData->hthemeWindow = NULL;
            }

            if (pwndData->hthemeScrollbar)
            {
                CloseThemeData(pwndData->hthemeScrollbar);
                pwndData->hthemeScrollbar = NULL;
            }

            if(IsAppThemed() && (GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
                ThemeCalculateCaptionButtonsPos(hWnd, NULL);

            pwndData->DirtyThemeRegion = TRUE;
            break;
        }
        case WM_NCCREATE:
        {
            PWND_DATA pwndData = ThemeGetWndData(hWnd);
            if (pwndData == NULL)
                return 0;
            pwndData->DirtyThemeRegion = TRUE;
        }
    }

    return 0;
}


static LRESULT CALLBACK
ThemePostWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ret,PDWORD unknown)
{
    switch(Msg)
    {
        case WM_WINDOWPOSCHANGED:
        {
            return OnPostWinPosChanged(hWnd, (WINDOWPOS*)lParam);
        }
        case WM_NCDESTROY:
        {
            ThemeDestroyWndData(hWnd);
            return 0;
        }
    }

    return 0;
}

HRESULT GetDiaogTextureBrush(HTHEME theme, HWND hwnd, HDC hdc, HBRUSH* result, BOOL changeOrigin)
{
    PWND_DATA pwndData;

    pwndData = ThemeGetWndData(hwnd);
    if (pwndData == NULL)
        return E_FAIL;

    if (pwndData->hTabBackgroundBrush == NULL)
    {
        HBITMAP hbmp;
        RECT dummy, bmpRect;
        BOOL hasImageAlpha;
        HRESULT hr;

        hr = UXTHEME_LoadImage(theme, 0, TABP_BODY, 0, &dummy, FALSE, &hbmp, &bmpRect, &hasImageAlpha);<--- Uninitialized variable: dummy
        if (FAILED(hr))
            return hr;

        if (changeOrigin)
        {
            /* Unfortunately SetBrushOrgEx doesn't work at all */
            RECT rcWindow, rcParent;
            UINT y;
            HDC hdcPattern, hdcHackPattern;
            HBITMAP hbmpOld1, hbmpold2, hbmpHack;

            GetWindowRect(hwnd, &rcWindow);
            GetWindowRect(GetParent(hwnd), &rcParent);
            y = (rcWindow.top - rcParent.top) % bmpRect.bottom;

            hdcPattern = CreateCompatibleDC(hdc);
            hbmpOld1 = (HBITMAP)SelectObject(hdcPattern, hbmp);

            hdcHackPattern = CreateCompatibleDC(hdc);
            hbmpHack = CreateCompatibleBitmap(hdc, bmpRect.right, bmpRect.bottom);
            hbmpold2 = (HBITMAP)SelectObject(hdcHackPattern, hbmpHack);

            BitBlt(hdcHackPattern, 0, 0, bmpRect.right, bmpRect.bottom - y, hdcPattern, 0, y, SRCCOPY);
            BitBlt(hdcHackPattern, 0, bmpRect.bottom - y, bmpRect.right, y, hdcPattern, 0, 0, SRCCOPY);

            hbmpold2 = (HBITMAP)SelectObject(hdcHackPattern, hbmpold2);
            hbmpOld1 = (HBITMAP)SelectObject(hdcPattern, hbmpOld1);

            DeleteDC(hdcPattern);
            DeleteDC(hdcHackPattern);

            /* Keep the handle of the bitmap we created so that it can be used later */
            pwndData->hTabBackgroundBmp = hbmpHack;
            hbmp = hbmpHack;
        }

        /* hbmp is cached so there is no need to free it */
        pwndData->hTabBackgroundBrush = CreatePatternBrush(hbmp);
    }

    if (!pwndData->hTabBackgroundBrush)
        return E_FAIL;

    *result = pwndData->hTabBackgroundBrush;
    return S_OK;
}

void HackFillStaticBg(HWND hwnd, HDC hdc, HBRUSH* result)
{
    RECT rcStatic;

    GetClientRect(hwnd, &rcStatic);
    FillRect(hdc, &rcStatic, *result);

    SetBkMode (hdc, TRANSPARENT);
    *result = GetStockObject (NULL_BRUSH);
}

static LRESULT CALLBACK
ThemeDlgPreWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ret,PDWORD unknown)
{
    return 0;
}

static LRESULT CALLBACK
ThemeDlgPostWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, ULONG_PTR ret,PDWORD unknown)
{
    switch(Msg)
    {
        case WM_CTLCOLORDLG:
        case WM_CTLCOLORBTN:
        case WM_CTLCOLORSTATIC:
        {
            HWND hwndTarget = (HWND)lParam;
            HDC hdc = (HDC)wParam;
            HBRUSH* phbrush = (HBRUSH*)ret;
            HTHEME hTheme;

            if(!IsAppThemed() || !(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
                break;

            if (!IsThemeDialogTextureEnabled (hWnd))
                break;

            hTheme = GetWindowTheme(hWnd);
            if (!hTheme)
                hTheme = OpenThemeData(hWnd, L"TAB");

            if (!hTheme)
                break;

            GetDiaogTextureBrush(hTheme, hwndTarget, hdc, phbrush, Msg != WM_CTLCOLORDLG);

#if 1 
            {
                WCHAR controlClass[32];
                GetClassNameW (hwndTarget, controlClass, sizeof(controlClass) / sizeof(controlClass[0]));

                /* This is a hack for the static class. Windows have a v6 static class just for this. */
                if (lstrcmpiW (controlClass, WC_STATICW) == 0)
                    HackFillStaticBg(hwndTarget, hdc, phbrush);
            }
#endif
            SetBkMode( hdc, TRANSPARENT );
            break;
        }
    }

    return 0;
}

int WINAPI ThemeSetWindowRgn(HWND hWnd, HRGN hRgn, BOOL bRedraw)
{
    PWND_DATA pwndData = ThemeGetWndData(hWnd);
    if(pwndData)
    {
        pwndData->HasAppDefinedRgn = TRUE;
        pwndData->HasThemeRgn = FALSE;
    }

    return g_user32ApiHook.SetWindowRgn(hWnd, hRgn, bRedraw);
}

BOOL WINAPI ThemeGetScrollInfo(HWND hwnd, int fnBar, LPSCROLLINFO lpsi)
{
    PWND_DATA pwndData;
    DWORD style;
    BOOL ret;

    /* Avoid creating a window context if it is not needed */
    if(!IsAppThemed() || !(GetThemeAppProperties() & STAP_ALLOW_NONCLIENT))
        goto dodefault;

    style = GetWindowLongW(hwnd, GWL_STYLE);
    if((style & (WS_HSCROLL|WS_VSCROLL))==0)
        goto dodefault;

    pwndData = ThemeGetWndData(hwnd);
    if (pwndData == NULL)
        goto dodefault;

    /* 
     * Uxtheme needs to handle the tracking of the scrollbar itself 
     * This means than if an application needs to get the track position
     * with GetScrollInfo, it will get wrong data. So uxtheme needs to
     * hook it and set the correct tracking position itself
     */
    ret = g_user32ApiHook.GetScrollInfo(hwnd, fnBar, lpsi);
    if ( lpsi && 
        (lpsi->fMask & SIF_TRACKPOS) &&
         pwndData->SCROLL_TrackingWin == hwnd && 
         pwndData->SCROLL_TrackingBar == fnBar)
    {
        lpsi->nTrackPos = pwndData->SCROLL_TrackingVal;
    }
    return ret;

dodefault:
    return g_user32ApiHook.GetScrollInfo(hwnd, fnBar, lpsi);
}

/**********************************************************************
 *      Exports
 */

BOOL CALLBACK 
ThemeInitApiHook(UAPIHK State, PUSERAPIHOOK puah)
{
    if (!puah || State != uahLoadInit)
    {
        UXTHEME_LoadTheme(FALSE);
        ThemeCleanupWndContext(NULL, 0);
        g_bThemeHooksActive = FALSE;
        return TRUE;
    }

    g_bThemeHooksActive = TRUE;

    /* Store the original functions from user32 */
    g_user32ApiHook = *puah;
    
    puah->DefWindowProcA = ThemeDefWindowProcA;
    puah->DefWindowProcW = ThemeDefWindowProcW;
    puah->PreWndProc = ThemePreWindowProc;
    puah->PostWndProc = ThemePostWindowProc;
    puah->PreDefDlgProc = ThemeDlgPreWindowProc;
    puah->PostDefDlgProc = ThemeDlgPostWindowProc;
    puah->DefWndProcArray.MsgBitArray  = gabDWPmessages;
    puah->DefWndProcArray.Size = UAHOWP_MAX_SIZE;
    puah->WndProcArray.MsgBitArray = gabMSGPmessages;
    puah->WndProcArray.Size = UAHOWP_MAX_SIZE;
    puah->DlgProcArray.MsgBitArray = gabDLGPmessages;
    puah->DlgProcArray.Size = UAHOWP_MAX_SIZE;

    puah->SetWindowRgn = ThemeSetWindowRgn;
    puah->GetScrollInfo = ThemeGetScrollInfo;

    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCPAINT);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCACTIVATE);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCMOUSEMOVE);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCMOUSELEAVE);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCHITTEST);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCLBUTTONDOWN);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCUAHDRAWCAPTION);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCUAHDRAWFRAME);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_SETTEXT);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_WINDOWPOSCHANGED);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_CONTEXTMENU);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_STYLECHANGED);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_SETICON);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_NCDESTROY);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_SYSCOMMAND);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_CTLCOLORMSGBOX);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_CTLCOLORBTN);
    UAH_HOOK_MESSAGE(puah->DefWndProcArray, WM_CTLCOLORSTATIC);

    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_CREATE);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_SETTINGCHANGE);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_DRAWITEM);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_MEASUREITEM);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_WINDOWPOSCHANGING);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_WINDOWPOSCHANGED);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_STYLECHANGING);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_STYLECHANGED);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_NCCREATE);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_NCDESTROY);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_NCPAINT);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_MENUCHAR);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_MDISETMENU);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_THEMECHANGED);
    UAH_HOOK_MESSAGE(puah->WndProcArray, WM_UAHINIT);

    puah->DlgProcArray.MsgBitArray = gabDLGPmessages;
    puah->DlgProcArray.Size = UAHOWP_MAX_SIZE;

    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_INITDIALOG);
    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_CTLCOLORMSGBOX);
    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_CTLCOLORBTN);
    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_CTLCOLORDLG);
    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_CTLCOLORSTATIC);
    UAH_HOOK_MESSAGE(puah->DlgProcArray, WM_PRINTCLIENT);

    UXTHEME_LoadTheme(TRUE);

    return TRUE;
}

typedef BOOL (WINAPI * PREGISTER_UAH_WINXP)(HINSTANCE hInstance, USERAPIHOOKPROC CallbackFunc);
typedef BOOL (WINAPI * PREGISTER_UUAH_WIN2003)(PUSERAPIHOOKINFO puah);

BOOL WINAPI
ThemeHooksInstall()
{
    PVOID lpFunc;
    OSVERSIONINFO osvi;
    BOOL ret;

    lpFunc = GetProcAddress(GetModuleHandle("user32.dll"), "RegisterUserApiHook");

    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osvi);

    if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
    {
        PREGISTER_UAH_WINXP lpfuncxp = (PREGISTER_UAH_WINXP)lpFunc;
        ret = lpfuncxp(hDllInst, ThemeInitApiHook);
    }
    else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
    {
        PREGISTER_UUAH_WIN2003 lpfunc2003 = (PREGISTER_UUAH_WIN2003)lpFunc;
        USERAPIHOOKINFO uah;

        uah.m_size = sizeof(uah);
        uah.m_dllname1 = L"uxtheme.dll";
        uah.m_funname1 = L"ThemeInitApiHook";
        uah.m_dllname2 = NULL;
        uah.m_funname2 = NULL;

        ret = lpfunc2003(&uah);
    }
    else
    {
        UNIMPLEMENTED;
        ret = FALSE;
    }

    UXTHEME_broadcast_theme_changed (NULL, TRUE);

    return ret;
}

BOOL WINAPI
ThemeHooksRemove()
{
    BOOL ret;

    ret = UnregisterUserApiHook();

    UXTHEME_broadcast_theme_changed (NULL, FALSE);

    return ret;
}

INT WINAPI ClassicSystemParametersInfoW(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni)
{
    if (g_bThemeHooksActive)
    {
        return g_user32ApiHook.SystemParametersInfoW(uiAction, uiParam, pvParam, fWinIni);
    }

    return SystemParametersInfoW(uiAction, uiParam, pvParam, fWinIni);
}

INT WINAPI ClassicSystemParametersInfoA(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni)
{
    if (g_bThemeHooksActive)
    {
        return g_user32ApiHook.SystemParametersInfoA(uiAction, uiParam, pvParam, fWinIni);
    }

    return SystemParametersInfoA(uiAction, uiParam, pvParam, fWinIni);
}

INT WINAPI ClassicGetSystemMetrics(int nIndex)
{
    if (g_bThemeHooksActive)
    {
        return g_user32ApiHook.GetSystemMetrics(nIndex);
    }

    return GetSystemMetrics(nIndex);
}

BOOL WINAPI ClassicAdjustWindowRectEx(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle)
{
    if (g_bThemeHooksActive)
    {
        return g_user32ApiHook.AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
    }

    return AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}