c - 为什么我的程序的选项卡控件以 block 状方式呈现它们的背景,而标准的窗口对话框却不是?

标签 c winapi gdi

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

这是我正在处理的一个程序的屏幕截图,用于测试我也在处理的包装器库:

Image

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

Image edited to show blockiness

最近的错误促使我制作了另一个程序来自行测试选项卡控件。在下面的截图中,左边的图片是一个真正的选项卡控件,右边的图片是用WM_PRINTCLIENT渲染的选项卡控件(红色是来自选项卡控件调用我自己的WM_PRINTCLIENT):

Test

这两个都呈现 block 状:

Test blockiness

为什么我自己的软件中的选项卡控件是这样的,而 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 以 block 状方式呈现父选项卡控件背景?

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

Image

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

Image edited to show blockiness

绘画的代码本质很简单:

// 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;
}

最佳答案

好吧,我想我大部分都弄明白了。

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

比较:

The two theme parts, normally

The two theme parts, color flipped

我猜想 Microsoft 希望您做什么,我猜想属性表控件的作用是让每个选项卡页成为一个 WC_DIALOG 对话框,并且您调用 EnableThemeDialogTexture () 函数在顶部绘制 TABP_BODY 纹理。

但是,如果您仔细查看原始问题中 Explorer 属性表的屏幕截图,我们的 TABP_BODY 仍然不太一样。所以还有两个问题:

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

  2. 主题对话框纹理是如何绘制的?它是否以最小宽度 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;
}

关于c - 为什么我的程序的选项卡控件以 block 状方式呈现它们的背景,而标准的窗口对话框却不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087540/

相关文章:

c - 共享 V 内存系统调用的 shmflg 中的 0 标志是什么意思?

init ELF 可执行文件的 C 文件?

c - 为什么我在下面的计算链表元素数量的代码中少了一个

c++ - 如何对 Windows 文本框中的数据执行三角函数

windows - GetWindowText 如何在没有系统调用的情况下获取另一个进程拥有的窗口的名称来读取该进程的内存?

c++ - 将 PDF 打印到 HDC(MFC 设备上下文)

c - C 中 long int 和 int 的大小显示 4 个字节

winapi - WM_TOUCH 与 WM_POINTER

c++ - 海森堡 : WinApi program crashes on some computers

.net - 如何调试 GDI 对象泄漏?