为啥我的程序的选项卡控件以块状方式呈现其背景,而标准的窗口对话框却不是?

Posted

技术标签:

【中文标题】为啥我的程序的选项卡控件以块状方式呈现其背景,而标准的窗口对话框却不是?【英文标题】:Why are my programs's tab controls rendering their background in a blocky way, but the standard Window dialogs are not?为什么我的程序的选项卡控件以块状方式呈现其背景,而标准的窗口对话框却不是? 【发布时间】:2015-07-17 05:27:42 【问题描述】:

tl;dr 对于那些阅读旧问题的人:新情况让我看得更深入,我发现这会影响裸 Tab 控件本身;我已经调整了问题以进行补偿。如果我应该完全删除旧问题文本,请告诉我。

这是我正在开发的用于测试我也在开发的包装器库的程序的屏幕截图:

如果您仔细观察,右边的窗口看起来是块状的,而左边的窗口(标准的 Windows 资源管理器文件属性属性表)看起来很平滑。一开始我以为这只是my video card,但其他人也看到了,甚至调整图像颜色以显示块状:

最近的错误导致我编写了另一个程序来自行测试选项卡控件。在下面的截图中,左边的图片是一个真实的tab控件,右边的图片是用WM_PRINTCLIENT渲染的tab控件(红色是从那个tab控件调用我自己的WM_PRINTCLIENT的地方):

这两个都是块状渲染:

为什么我自己的软件中的选项卡控件是这样的,而 Windows 自己的选项卡却不是?

这是 Windows XP,因为它具有要测试的渐变。我需要以 XP 和更新版本为目标,尽管我可能很快就会放弃 XP。我不想在我的代码或设置中隐藏最低系统要求之后的错误。

此测试程序如下。通过 comctl6 作为参数运行它。请注意,为了简洁起见,并且作为一个测试事物的小程序,它不会进行错误检查。

谢谢!

// 18 may 2015
// based on wintabparentwinebug.c 3 may 2015
#define UNICODE
#define _UNICODE
#define STRICT
#define STRICT_TYPED_ITEMIDS
#define CINTERFACE
// get Windows version right; right now Windows XP
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#define _WIN32_WINDOWS 0x0501       /* according to Microsoft's winperf.h */
#define _WIN32_IE 0x0600            /* according to Microsoft's sdkddkver.h */
#define NTDDI_VERSION 0x05010000    /* according to Microsoft's sdkddkver.h */
#include <windows.h>
#include <commctrl.h>
#include <stdint.h>
#include <uxtheme.h>
#include <string.h>
#include <wchar.h>
#include <windowsx.h>
#include <vsstyle.h>
#include <vssym32.h>
#include <stdarg.h>
#include <oleacc.h>
#include <stdio.h>

void die(char *s)

    // TODO


void initCommonControls(BOOL);

HWND mainwin;
HWND tab;

#define BGCOLOR RGB(0x0A, 0x24, 0x6A)
#define PCOLOR RGB(0x6A, 0x24, 0x0A)

LRESULT CALLBACK wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

    NMHDR *nm = (NMHDR *) lParam;
    PAINTSTRUCT ps;
    HDC dc;
    POINT prev;
    RECT r;
    HBRUSH b;

    switch (uMsg) 
    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        dc = BeginPaint(hwnd, &ps);
        SetWindowOrgEx(dc, -240, -20, &prev);
        SendMessage(tab, WM_PRINTCLIENT, (WPARAM) dc, PRF_CLIENT);
        SetWindowOrgEx(dc, prev.x, prev.y, NULL);
        EndPaint(hwnd, &ps);
COLORREF r;
r=GetSysColor(COLOR_ACTIVECAPTION);
printf("%I32X\n", r);
        return 0;
    case WM_PRINTCLIENT:
        // the tab control sends this to draw the background of the area where the tab buttons are
        b = CreateSolidBrush(PCOLOR);
        GetClientRect(hwnd, &r);
        FillRect((HDC) wParam, &r, b);
        DeleteObject(b);
        return 0;
    
    return DefWindowProcW(hwnd, uMsg, wParam, lParam);


static void makeWindows(void)

    mainwin = CreateWindowExW(0,
        L"mainwin", L"Full Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 500,
        NULL, NULL, GetModuleHandle(NULL), NULL);

    // create the tab as a child of the empty window...
    tab = CreateWindowExW(0,
        WC_TABCONTROLW, L"",
        TCS_TOOLTIPS | WS_TABSTOP | WS_CHILD | WS_VISIBLE,
        20, 20, 200, 440,
        mainwin, (HMENU) 100, GetModuleHandle(NULL), NULL);


void addTab(WCHAR *name)

    TCITEMW item;
    LRESULT n;

    n = SendMessageW(tab, TCM_GETITEMCOUNT, 0, 0);
    ZeroMemory(&item, sizeof (TCITEMW));
    item.mask = TCIF_TEXT;
    item.pszText = name;
    SendMessageW(tab, TCM_INSERTITEM, (WPARAM) n, (LPARAM) (&item));


int main(int argc, char *argv[])

    WNDCLASSW wc;
    MSG msg;
    HBRUSH b;

    initCommonControls(argc > 1 && strcmp(argv[1], "comctl6") == 0);

    ZeroMemory(&wc, sizeof (WNDCLASSW));
    wc.lpszClassName = L"mainwin";
    wc.lpfnWndProc = wndProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    // if printing client doesn't print the tab background, this color will bleed through instead
    b = CreateSolidBrush(BGCOLOR);
    wc.hbrBackground = b;
    RegisterClassW(&wc);

    makeWindows();
    addTab(L"Page 1");
    addTab(L"Page 2");

    ShowWindow(mainwin, SW_SHOWDEFAULT);
    UpdateWindow(mainwin);

    while (GetMessageW(&msg, NULL, 0, 0) > 0) 
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    
    return 0;


static const char manifest[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">\n<assemblyIdentity\n    version=\"1.0.0.0\"\n    processorArchitecture=\"*\"\n    name=\"CompanyName.ProductName.YourApplication\"\n    type=\"win32\"\n/>\n<description>Your application description here.</description>\n<dependency>\n    <dependentAssembly>\n        <assemblyIdentity\n            type=\"win32\"\n            name=\"Microsoft.Windows.Common-Controls\"\n            version=\"6.0.0.0\"\n            processorArchitecture=\"*\"\n            publicKeyToken=\"6595b64144ccf1df\"\n            language=\"*\"\n        />\n    </dependentAssembly>\n</dependency>\n</assembly>\n";

static ULONG_PTR comctlManifestCookie;
static HMODULE comctl32;

void initCommonControls(BOOL comctl6)

    WCHAR temppath[MAX_PATH + 1];
    WCHAR filename[MAX_PATH + 1];
    HANDLE file;
    DWORD nExpected, nGot;
    ACTCTX actctx;
    HANDLE ac;
    INITCOMMONCONTROLSEX icc;
    FARPROC f;
    // this is listed as WINAPI in both Microsoft's and MinGW's headers, but not on MSDN for some reason
    BOOL (*WINAPI ficc)(const LPINITCOMMONCONTROLSEX);

    if (comctl6) 
        if (GetTempPathW(MAX_PATH + 1, temppath) == 0)
            die("getting temporary path for writing manifest file");
        if (GetTempFileNameW(temppath, L"manifest", 0, filename) == 0)
            die("getting temporary filename for writing manifest file");
        file = CreateFileW(filename, GENERIC_WRITE,
            0,          // don't share while writing
            NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (file == NULL)
            die("creating manifest file");
        nExpected = (sizeof manifest / sizeof manifest[0]) - 1;     // - 1 to omit the terminating null character)
        if (WriteFile(file, manifest, nExpected, &nGot, NULL) == 0)
            die("writing manifest file");
        if (nGot != nExpected)
            die("short write to manifest file");
        if (CloseHandle(file) == 0)
            die("closing manifest file (this IS an error here because not doing so will prevent Windows from being able to use the manifest file in an activation context)");

        ZeroMemory(&actctx, sizeof (ACTCTX));
        actctx.cbSize = sizeof (ACTCTX);
        actctx.dwFlags = ACTCTX_FLAG_SET_PROCESS_DEFAULT;
        actctx.lpSource = filename;
        ac = CreateActCtx(&actctx);
        if (ac == INVALID_HANDLE_VALUE)
            die("creating activation context for synthesized manifest file");
        if (ActivateActCtx(ac, &comctlManifestCookie) == FALSE)
            die("activating activation context for synthesized manifest file");
    

    ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX));
    icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
    icc.dwICC = ICC_TAB_CLASSES;

    comctl32 = LoadLibraryW(L"comctl32.dll");
    if (comctl32 == NULL)
        die("loading comctl32.dll");
    f = GetProcAddress(comctl32, "InitCommonControlsEx");
    if (f == NULL)
        die("loading InitCommonControlsEx()");
    ficc = (BOOL (*WINAPI)(const LPINITCOMMONCONTROLSEX)) f;
    if ((*ficc)(&icc) == FALSE)
        die("initializing Common Controls (comctl32.dll)");


原问题:

标题:为什么CreateCompatibleDC()、CreateCompatibleBitmap()、WM_PRINTCLIENT会以块状方式渲染父标签控件背景?

我的容器窗口 clss 在其 WM_PAINT 中绘制任何 WM_PRINTCLIENT 为其父级绘制的内容(它本身不是容器)。因此,例如,如果它的背景是一个主题选项卡控件,它将绘制主题选项卡控件背景作为其绘制的内容。下图右边的窗口就是一个例子:

如果您仔细观察,右边的窗口看起来是块状的,而左边的窗口(标准的 Windows 资源管理器文件属性属性表)看起来很平滑。一开始我以为这只是my video card,但其他人也看到了,甚至调整图像颜色以显示块状:

绘制的代码本质很简单:

// not actual code, just algorithm to demonstrate what I'm *thinking* is supposed to happen
dc = BeginPaint(hwnd, &ps);
parent = GetAncestor(hwnd, GA_PARENT);
GetClientRect(parent, &r);
cdc = CreateCompatibleDC(dc);
bitmap = CreateCompatibleBitmap(dc, r.right - r.left, r.bottom - r.top);
SelectObject(cdc, pd->bitmap);
SendMessageW(parent, WM_PRINTCLIENT, (WPARAM) cdc, PRF_CLIENT);
updateRect = ps.rcPaint;
parentRect = updateRect;
MapWindowRect(hwnd, parent, &parentRect);
BitBlt(dc, updateRect.left, updateRect.top, updateRect.right - updateRect.left, updateRect.bottom - updateRect.top,
    cdc, parentRect.left, parentRect.top,
    SRCCOPY);

我在这里看不到任何会影响绘制图像质量的东西,除非我缺少一些关于兼容位图的东西?其他人可以解释吗?我真的不知道该怎么做,除了为每个可能的容器父级重新实现绘图并希望有所帮助。

谢谢。

这是容器的完整代码:

// 26 april 2015
#include "uipriv_windows.h"

#define containerClass L"libui_uiContainerClass"

HWND initialParent;

struct container 
    HWND hwnd;
    uiContainer *parent;
    int hidden;
    HBRUSH brush;
;

static HWND realParent(HWND hwnd)

    HWND parent;
    int class;

    parent = hwnd;
    for (;;) 
        parent = GetAncestor(parent, GA_PARENT);
        // skip groupboxes; they're (supposed to be) transparent
        // skip uiContainers; they don't draw anything
        class = windowClassOf(parent, L"button", containerClass, NULL);
        if (class != 0 && class != 1)
            break;
    
    return parent;


struct parentDraw 
    HDC cdc;
    HBITMAP bitmap;
    HBITMAP prevbitmap;
;

static void parentDraw(HDC dc, HWND parent, struct parentDraw *pd)

    RECT r;

    if (GetClientRect(parent, &r) == 0)
        logLastError("error getting parent's client rect in parentDraw()");
    pd->cdc = CreateCompatibleDC(dc);
    if (pd->cdc == NULL)
        logLastError("error creating compatible DC in parentDraw()");
    pd->bitmap = CreateCompatibleBitmap(dc, r.right - r.left, r.bottom - r.top);
    if (pd->bitmap == NULL)
        logLastError("error creating compatible bitmap in parentDraw()");
    pd->prevbitmap = SelectObject(pd->cdc, pd->bitmap);
    if (pd->prevbitmap == NULL)
        logLastError("error selecting bitmap into compatible DC in parentDraw()");
    SendMessageW(parent, WM_PRINTCLIENT, (WPARAM) (pd->cdc), PRF_CLIENT);


static void endParentDraw(struct parentDraw *pd)

    if (SelectObject(pd->cdc, pd->prevbitmap) != pd->bitmap)
        logLastError("error selecting previous bitmap back into compatible DC in endParentDraw()");
    if (DeleteObject(pd->bitmap) == 0)
        logLastError("error deleting compatible bitmap in endParentDraw()");
    if (DeleteDC(pd->cdc) == 0)
        logLastError("error deleting compatible DC in endParentDraw()");


// see http://www.codeproject.com/Articles/5978/Correctly-drawn-themed-dialogs-in-WinXP
static HBRUSH getControlBackgroundBrush(HWND hwnd, HDC dc)

    HWND parent;
    RECT hwndScreenRect;
    struct parentDraw pd;
    HBRUSH brush;

    parent = realParent(hwnd);

    parentDraw(dc, parent, &pd);
    brush = CreatePatternBrush(pd.bitmap);
    if (brush == NULL)
        logLastError("error creating pattern brush in getControlBackgroundBrush()");
    endParentDraw(&pd);

    // now figure out where the control is relative to the parent so we can align the brush properly
    if (GetWindowRect(hwnd, &hwndScreenRect) == 0)
        logLastError("error getting control window rect in getControlBackgroundBrush()");
    // this will be in screen coordinates; convert to parent coordinates
    mapWindowRect(NULL, parent, &hwndScreenRect);
    if (SetBrushOrgEx(dc, -hwndScreenRect.left, -hwndScreenRect.top, NULL) == 0)
        logLastError("error setting brush origin in getControlBackgroundBrush()");

    return brush;


static void paintContainerBackground(HWND hwnd, HDC dc, RECT *paintRect)

    HWND parent;
    RECT paintRectParent;
    struct parentDraw pd;

    parent = realParent(hwnd);
    parentDraw(dc, parent, &pd);

    paintRectParent = *paintRect;
    mapWindowRect(hwnd, parent, &paintRectParent);
    if (BitBlt(dc, paintRect->left, paintRect->top, paintRect->right - paintRect->left, paintRect->bottom - paintRect->top,
        pd.cdc, paintRectParent.left, paintRectParent.top,
        SRCCOPY) == 0)
        logLastError("error drawing parent background over uiContainer in paintContainerBackground()");

    endParentDraw(&pd);


// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing and https://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
// this X value is really only for buttons but I don't see a better one :/
#define winXPadding 4
#define winYPadding 4

// abort the resize if something fails and we don't have what we need to do a resize
static HRESULT resize(uiContainer *cc, RECT *r)

    struct container *c = (struct container *) (uiControl(cc)->Internal);
    uiSizing d;
    uiSizingSys sys;
    HDC dc;
    HFONT prevfont;
    TEXTMETRICW tm;
    SIZE size;

    dc = GetDC(c->hwnd);
    if (dc == NULL)
        return logLastError("error getting DC in resize()");
    prevfont = (HFONT) SelectObject(dc, hMessageFont);
    if (prevfont == NULL)
        return logLastError("error loading control font into device context in resize()");

    ZeroMemory(&tm, sizeof (TEXTMETRICW));
    if (GetTextMetricsW(dc, &tm) == 0)
        return logLastError("error getting text metrics in resize()");
    if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
        return logLastError("error getting text extent point in resize()");

    sys.baseX = (int) ((size.cx / 26 + 1) / 2);
    sys.baseY = (int) tm.tmHeight;
    sys.internalLeading = tm.tmInternalLeading;

    if (SelectObject(dc, prevfont) != hMessageFont)
        return logLastError("error restoring previous font into device context in resize()");
    if (ReleaseDC(c->hwnd, dc) == 0)
        return logLastError("error releasing DC in resize()");

    d.xPadding = uiDlgUnitsToX(winXPadding, sys.baseX);
    d.yPadding = uiDlgUnitsToY(winYPadding, sys.baseY);
    d.sys = &sys;
    uiContainerResizeChildren(cc, r->left, r->top, r->right - r->left, r->bottom - r->top, &d);
    return S_OK;


static LRESULT CALLBACK containerWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

    uiContainer *cc;
    struct container *c;
    CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
    HWND control;
    NMHDR *nm = (NMHDR *) lParam;
    WINDOWPOS *wp = (WINDOWPOS *) lParam;
    RECT r;
    HDC dc;
    PAINTSTRUCT ps;

    cc = uiContainer(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
    if (cc == NULL)
        if (uMsg == WM_NCCREATE)
            SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) (cs->lpCreateParams));
        // DO NOT RETURN DEFWINDOWPROC() HERE
        // see the next block of comments as to why
        // instead, we simply check if c == NULL again later

    switch (uMsg) 
    // these must always be run, even on the initial parent
    // why? http://blogs.msdn.com/b/oldnewthing/archive/2010/03/16/9979112.aspx
    case WM_COMMAND:
        // bounce back to the control in question
        // except if to the initial parent, in which case act as if the message was ignored
        control = (HWND) lParam;
        if (control != NULL && IsChild(initialParent, control) == 0)
            return SendMessageW(control, msgCOMMAND, wParam, lParam);
        break;          // fall through to DefWindowProcW()
    case WM_NOTIFY:
        // same as WM_COMMAND
        control = nm->hwndFrom;
        if (control != NULL && IsChild(initialParent, control) == 0)
            return SendMessageW(control, msgNOTIFY, wParam, lParam);
        break;

    // these are only run if c is not NULL
    case WM_CTLCOLORSTATIC:
    case WM_CTLCOLORBTN:
        if (cc == NULL)
            break;
        c = (struct container *) (uiControl(cc)->Internal);
        if (c->brush != NULL)
            if (DeleteObject(c->brush) == 0)
                logLastError("error deleting old background brush in containerWndProc()");
/*TODO      // read-only TextFields and Textboxes are exempt
        // this is because read-only edit controls count under WM_CTLCOLORSTATIC
        if (windowClassOf((HWND) lParam, L"edit", NULL) == 0)
            if (textfieldReadOnly((HWND) lParam))
                return DefWindowProcW(hwnd, uMsg, wParam, lParam);
*/      if (SetBkMode((HDC) wParam, TRANSPARENT) == 0)
            logLastError("error setting transparent background mode to controls in containerWndProc()");
        c->brush = getControlBackgroundBrush((HWND) lParam, (HDC) wParam);
        return (LRESULT) (c->brush);
    case WM_PAINT:
        if (cc == NULL)
            break;
        c = (struct container *) (uiControl(cc)->Internal);
        dc = BeginPaint(c->hwnd, &ps);
        if (dc == NULL)
            logLastError("error beginning container paint in containerWndProc()");
        r = ps.rcPaint;
        paintContainerBackground(c->hwnd, dc, &r);
        EndPaint(c->hwnd, &ps);
        return 0;
    // tab controls use this to draw the background of the tab area
    case WM_PRINTCLIENT:
        if (cc == NULL)
            break;
        c = (struct container *) (uiControl(cc)->Internal);
        if (GetClientRect(c->hwnd, &r) == 0)
            logLastError("error getting client rect in containerWndProc()");
        paintContainerBackground(c->hwnd, (HDC) wParam, &r);
        return 0;
    case WM_ERASEBKGND:
        // avoid some flicker
        // we draw the whole update area anyway
        return 1;
    case WM_WINDOWPOSCHANGED:
        if ((wp->flags & SWP_NOSIZE) != 0)
            break;
        // fall through
    case msgUpdateChild:
        if (cc == NULL)
            break;
        c = (struct container *) (uiControl(cc)->Internal);
        if (GetClientRect(c->hwnd, &r) == 0)
            logLastError("error getting client rect for resize in containerWndProc()");
        resize(cc, &r);
        return 0;
    

    return DefWindowProcW(hwnd, uMsg, wParam, lParam);


const char *initContainer(HICON hDefaultIcon, HCURSOR hDefaultCursor)

    WNDCLASSW wc;

    ZeroMemory(&wc, sizeof (WNDCLASSW));
    wc.lpszClassName = containerClass;
    wc.lpfnWndProc = containerWndProc;
    wc.hInstance = hInstance;
    wc.hIcon = hDefaultIcon;
    wc.hCursor = hDefaultCursor;
    wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    if (RegisterClassW(&wc) == 0)
        return "registering uiContainer window class";

    initialParent = CreateWindowExW(0,
        containerClass, L"",
        WS_OVERLAPPEDWINDOW,
        0, 0,
        100, 100,
        NULL, NULL, hInstance, NULL);
    if (initialParent == NULL)
        return "creating initial parent window";

    // just to be safe, disable the initial parent so it can't be interacted with accidentally
    // if this causes issues for our controls, we can remove it
    EnableWindow(initialParent, FALSE);
    return NULL;


// subclasses override this and call back here when all children are destroyed
static void containerDestroy(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);

    if (c->parent != NULL)
        complain("attempt to destroy uiContainer %p while it has a parent", cc);
    if (DestroyWindow(c->hwnd) == 0)
        logLastError("error destroying uiContainer window in containerDestroy()");
    uiFree(c);


static uintptr_t containerHandle(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);

    return (uintptr_t) (c->hwnd);


static void containerSetParent(uiControl *cc, uiContainer *parent)

    struct container *c = (struct container *) (cc->Internal);
    uiContainer *oldparent;
    HWND newparent;

    oldparent = c->parent;
    c->parent = parent;
    newparent = initialParent;
    if (c->parent != NULL)
        newparent = (HWND) uiControlHandle(uiControl(c->parent));
    if (SetParent(c->hwnd, newparent) == 0)
        logLastError("error changing uiContainer parent in containerSetParent()");
    if (oldparent != NULL)
        uiContainerUpdate(oldparent);
    if (c->parent != NULL)
        uiContainerUpdate(c->parent);


static void containerResize(uiControl *cc, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)

    struct container *c = (struct container *) (cc->Internal);

    if (MoveWindow(c->hwnd, x, y, width, height, TRUE) == 0)
        logLastError("error resizing uiContainer in containerResize()");


static int containerVisible(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);

    return !c->hidden;


static void containerShow(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);

    ShowWindow(c->hwnd, SW_SHOW);
    // hidden controls don't count in boxes and grids
    c->hidden = 0;
    if (c->parent != NULL)
        uiContainerUpdate(c->parent);


static void containerHide(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);

    ShowWindow(c->hwnd, SW_HIDE);
    c->hidden = 1;
    if (c->parent != NULL)
        uiContainerUpdate(c->parent);


static void containerEnable(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);
    uiControlSysFuncParams p;

    EnableWindow(c->hwnd, TRUE);
    p.Func = uiWindowsSysFuncContainerEnable;
    uiControlSysFunc(cc, &p);


static void containerDisable(uiControl *cc)

    struct container *c = (struct container *) (cc->Internal);
    uiControlSysFuncParams p;

    EnableWindow(c->hwnd, FALSE);
    p.Func = uiWindowsSysFuncContainerDisable;
    uiControlSysFunc(cc, &p);


static void containerUpdate(uiContainer *cc)

    struct container *c = (struct container *) (uiControl(cc)->Internal);

    SendMessageW(c->hwnd, msgUpdateChild, 0, 0);


void uiMakeContainer(uiContainer *cc)

    struct container *c;

    c = uiNew(struct container);

    c->hwnd = CreateWindowExW(WS_EX_CONTROLPARENT,
        containerClass, L"",
        WS_CHILD | WS_VISIBLE,
        0, 0,
        100, 100,
        initialParent, NULL, hInstance, cc);
    if (c->hwnd == NULL)
        logLastError("error creating uiContainer window in uiMakeContainer()");

    uiControl(cc)->Internal = c;
    uiControl(cc)->Destroy = containerDestroy;
    uiControl(cc)->Handle = containerHandle;
    uiControl(cc)->SetParent = containerSetParent;
    // PreferredSize() is provided by subclasses
    uiControl(cc)->Resize = containerResize;
    uiControl(cc)->Visible = containerVisible;
    uiControl(cc)->Show = containerShow;
    uiControl(cc)->Hide = containerHide;
    uiControl(cc)->Enable = containerEnable;
    uiControl(cc)->Disable = containerDisable;

    // ResizeChildren() is provided by subclasses
    uiContainer(cc)->Update = containerUpdate;

【问题讨论】:

【参考方案1】:

好吧,我想我已经大部分想通了。

问题是这里有两个不同的主题部分,TABP_PANETABP_BODYTABP_PANE 是选项卡控件本身绘制的背景,TABP_BODY 是我假设是实际的选项卡背景。

比较:

我猜微软希望你做什么,我猜属性表控件做什么,是让每个标签页成为一个 WC_DIALOG 对话框,并且你调用 EnableThemeDialogTexture() 函数来获得 @987654329 @ 纹理绘制在顶部。

但是如果仔细看原题中Explorer属性表的截图,我们的TABP_BODY还是不太一样。所以剩下两个问题:

    TABP_BODY 是如何绘制在TABP_PANE 之上的?它只是画在上面吗?是否以某种方式混合?

    主题对话框纹理是如何绘制的?它是否以最小宽度平铺TABP_BODY?反正在我看来就是这样……

所以这至少是部分答案。我愿意进一步调查,但我不确定何时。


图片来源:

// 20 may 2015
// based on wintabprintclient.c 18 may 2015
// based on wintabparentwinebug.c 3 may 2015
#define UNICODE
#define _UNICODE
#define STRICT
#define STRICT_TYPED_ITEMIDS
#define CINTERFACE
// get Windows version right; right now Windows XP
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#define _WIN32_WINDOWS 0x0501       /* according to Microsoft's winperf.h */
#define _WIN32_IE 0x0600            /* according to Microsoft's sdkddkver.h */
#define NTDDI_VERSION 0x05010000    /* according to Microsoft's sdkddkver.h */
#include <windows.h>
#include <commctrl.h>
#include <stdint.h>
#include <uxtheme.h>
#include <string.h>
#include <wchar.h>
#include <windowsx.h>
#include <vsstyle.h>
#include <vssym32.h>
#include <stdarg.h>
#include <oleacc.h>
#include <stdio.h>

void die(char *s)

    // TODO


HWND mainwin;

#define BGCOLOR RGB(0x0A, 0x24, 0x6A)
#define PCOLOR RGB(0x6A, 0x24, 0x0A)

LRESULT CALLBACK wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

    NMHDR *nm = (NMHDR *) lParam;
    PAINTSTRUCT ps;
    HDC dc;
    POINT pt;
    RECT r;
    HTHEME theme;

    switch (uMsg) 
    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        dc = BeginPaint(hwnd, &ps);
#define X 20
#define Y 20
#define X2 240
#define YT 40
#define WIDTH 200
#define HEIGHT 440
        theme = OpenThemeData(hwnd, L"tab");
        r.left = X;
        r.top = Y;
        r.right = r.left + WIDTH;
        r.bottom = YT - 5;
        DrawTextW(dc, L"TABP_PANE", -1, &r, DT_LEFT | DT_TOP);
        r.left = X;
        r.top = YT;
        r.right = r.left + WIDTH;
        r.bottom = r.top + HEIGHT;
        DrawThemeBackground(theme, dc,
            TABP_PANE, 0,
            &r, NULL);
        r.left = X2;
        r.top = Y;
        r.right = r.left + WIDTH;
        r.bottom = YT - 5;
        DrawTextW(dc, L"TABP_BODY", -1, &r, DT_LEFT | DT_TOP);
        r.left = X2;
        r.top = YT;
        r.right = r.left + WIDTH;
        r.bottom = r.top + HEIGHT;
        DrawThemeBackground(theme, dc,
            TABP_BODY, 0,
            &r, NULL);
        CloseThemeData(theme);
        EndPaint(hwnd, &ps);
        return 0;
    
    return DefWindowProcW(hwnd, uMsg, wParam, lParam);


static void makeWindows(void)

    mainwin = CreateWindowExW(0,
        L"mainwin", L"Full Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 500,
        NULL, NULL, GetModuleHandle(NULL), NULL);


int main(int argc, char *argv[])

    WNDCLASSW wc;
    MSG msg;
    HBRUSH b;

    ZeroMemory(&wc, sizeof (WNDCLASSW));
    wc.lpszClassName = L"mainwin";
    wc.lpfnWndProc = wndProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    // if printing client doesn't print the tab background, this color will bleed through instead
    b = CreateSolidBrush(BGCOLOR);
    wc.hbrBackground = b;
    RegisterClassW(&wc);

    makeWindows();

    ShowWindow(mainwin, SW_SHOWDEFAULT);
    UpdateWindow(mainwin);

    while (GetMessageW(&msg, NULL, 0, 0) > 0) 
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    
    return 0;

【讨论】:

以上是关于为啥我的程序的选项卡控件以块状方式呈现其背景,而标准的窗口对话框却不是?的主要内容,如果未能解决你的问题,请参考以下文章

重新制作 MFC 对话框以使用选项卡控件

Vb.net 如何以编程方式选择选项卡控件中的最后一个选项卡

在选项卡栏控制器中选择时以模态方式呈现视图控制器

Django:为啥选项不会呈现为选中状态?

labview中制作一个选项卡按钮设置好背景,放入进去的控件怎么才能不被覆盖

SAP ABAP,选项卡控件中有个表格控件,表格控件能得到值,但就是初始化时显示不出来,这是为啥?