c++ - 当我移动轨迹栏时,如何防止控件(选项卡)在 Windows 通用控件 6.0 中闪烁和消失?

标签 c++ winapi controls trackbar

滑动轨迹条并释放鼠标按钮时,整个窗口闪烁,选项卡消失。

当我使用旧版本时,一切正常!

当我使用新的 Microsoft Windows Common Controls ver.6.0 时,发现了这个问题!!!

#include <windows.h>
#include <commctrl.h>
#include <tchar.h>

#pragma comment(lib,"comctl32.lib")
HWND hWin, hTab;

#if defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif

LRESULT CALLBACK WndProcedure( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) {
    switch( Msg ) {
        case WM_CREATE:{
            HINSTANCE hInst = GetModuleHandle( NULL );
            RECT rc;
            int dx, dy;

            GetClientRect( hWnd, &rc );
            dx = rc.right - rc.left;
            dy = rc.bottom - rc.top;

            TCITEM tie = { TCIF_TEXT | TCIF_IMAGE, 0, 0, NULL, 0, -1, 0 };

            hTab = CreateWindowEx( NULL, WC_TABCONTROL, _T(""), WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 0, 0, dx, dy, hWnd, ( HMENU )( DWORDLONG )1001, hInst, NULL );

            tie.pszText = _T("Tab One");
            TabCtrl_InsertItem( hTab, 0, &tie );
            tie.pszText = _T("Tab Two");
            TabCtrl_InsertItem( hTab, 1, &tie );

            CreateWindowEx( NULL, TRACKBAR_CLASS, _T(""), WS_VISIBLE | WS_CHILD | WS_TABSTOP, 50, 50, 200, 40, hTab, ( HMENU )1002, hInst, NULL );
        }
        break;

        case WM_NOTIFY: {
            LPNMHDR ns = (LPNMHDR)lParam;
            if( (ns->idFrom == 1001) && (ns->code == TCN_SELCHANGING) )
                return FALSE;
        }
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return (DefWindowProc(hWnd, Msg, wParam, lParam));
    }

    return FALSE;
}

INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
    INITCOMMONCONTROLSEX icc = { sizeof( INITCOMMONCONTROLSEX ), ICC_WIN95_CLASSES };
    WNDCLASSEX  WndClsEx;
    MSG         Msg;

    ZeroMemory( &WndClsEx, sizeof( WNDCLASSEX ) );
    WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc   = WndProcedure;
    WndClsEx.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
    WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW );
    WndClsEx.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
    WndClsEx.lpszClassName = _T("Trackbar_Tester");
    WndClsEx.hInstance     = hInstance;

    RegisterClassEx(&WndClsEx);
    InitCommonControlsEx( &icc );

    if( !(hWin = CreateWindow( WndClsEx.lpszClassName, _T("TB_Tester"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL )) )
        return 0;

    ShowWindow( hWin, SW_SHOWNORMAL );
    UpdateWindow( hWin );

    while( GetMessage(&Msg, NULL, 0, 0) ) {
        TranslateMessage( &Msg );
        DispatchMessage( &Msg );
    }

    return (int)Msg.wParam;
}

这就是整个程序。

我举一个简洁的例子。

如果你运行它,你就会看到问题。

如果您评论新控件,问题就会消失。

最佳答案

选项卡控件公共(public)控件不会为您管理单独的内容 Pane ,因此如果您希望该轨迹栏成为选项卡控件的子项,并以我们期望选项卡控件的方式工作,您需要自己管理子 Pane 。

您可以使用“TabCtrl_AdjustRect”来确定子 Pane 需要多大。例如,请参阅以下代码修改:

#define TAB_ID 2000

HWND hWin, hTab;
HWND g_tabPanes[2];

HWND CreateTabPane(HWND tabctrl, int id, HINSTANCE instance)
{
    RECT rcTab;
    GetClientRect(tabctrl, &rcTab);
    TabCtrl_AdjustRect(tabctrl, FALSE, &rcTab);
    int wd = rcTab.right - rcTab.left;
    int hgt = rcTab.bottom - rcTab.top;
    return CreateWindow(
        L"static", L"", 
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 
        rcTab.left, rcTab.top, wd, hgt, 
        tabctrl, 
        (HMENU) id, 
        instance, 
        NULL
    );
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
    switch (Msg) {
    case WM_CREATE: {
            HINSTANCE hInst = GetModuleHandle(NULL);
            RECT rc; int dx, dy;

            GetClientRect(hWnd, &rc);
            dx = rc.right - rc.left;
            dy = rc.bottom - rc.top;

            TCITEM tie = {
                TCIF_TEXT | TCIF_IMAGE,
                0, 0,
                NULL,
                0, -1, 0
            };

            hTab = CreateWindowEx(NULL, WC_TABCONTROL, _T(""),
                WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS,
                0, 0, dx, dy, hWnd,
                (HMENU)1001, hInst, NULL
            );

            tie.pszText = (LPWSTR)_T("Tab One");
            TabCtrl_InsertItem(hTab, 0, &tie);

            tie.pszText = (LPWSTR)_T("Tab Two");
            TabCtrl_InsertItem(hTab, 1, &tie);

            for (int i = 0; i < 2; i++)
                g_tabPanes[i] = CreateTabPane(hTab, TAB_ID + i, hInst);

            CreateWindowEx(NULL, TRACKBAR_CLASS, _T(""), WS_VISIBLE | WS_CHILD | WS_TABSTOP, 
                50, 50, 200, 40, g_tabPanes[0], (HMENU)1002, hInst, NULL);
        }
        break;

        case WM_NOTIFY: {   
            LPNMHDR ns = (LPNMHDR)lParam; 
            if ((ns->idFrom == 1001) && (ns->code == TCN_SELCHANGE)) {
                int pane = TabCtrl_GetCurSel(hTab);
                for (int i = 0; i < 2; i++)
                    if (pane == i)
                        ShowWindow(g_tabPanes[i], SW_SHOW);
                    else
                        ShowWindow(g_tabPanes[i], SW_HIDE);
            }
        } break;


        case WM_DESTROY: 
            PostQuitMessage(0); 
            break;
        default: return (DefWindowProc(hWnd, Msg, wParam, lParam));
        }
    return FALSE;
}

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    INITCOMMONCONTROLSEX icc = { 
        sizeof(INITCOMMONCONTROLSEX), 
        ICC_WIN95_CLASSES 
    };

    WNDCLASSEX  WndClsEx;
    MSG         Msg;

    ZeroMemory(&WndClsEx, sizeof(WNDCLASSEX));
    WndClsEx.cbSize = sizeof(WNDCLASSEX);
    WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc = WndProcedure;
    WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
    WndClsEx.lpszClassName = _T("Trackbar_Tester");
    WndClsEx.hInstance = hInstance;
    RegisterClassEx(&WndClsEx);

    InitCommonControlsEx(&icc);

    if (!(hWin = CreateWindow(WndClsEx.lpszClassName, _T("TB_Tester"), WS_OVERLAPPEDWINDOW| WS_CLIPSIBLINGS, 0, 0, 600, 400, NULL, NULL, hInstance, NULL)))
        return 0;

    ShowWindow(hWin, SW_SHOWNORMAL);
    UpdateWindow(hWin);

    while (GetMessage(&Msg, NULL, 0, 0))  { 
        TranslateMessage(&Msg); 
        DispatchMessage(&Msg); 
    }
    return (int)Msg.wParam;
}

关于c++ - 当我移动轨迹栏时,如何防止控件(选项卡)在 Windows 通用控件 6.0 中闪烁和消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426662/

相关文章:

winapi - 正确移除窗框/边框

c++ - 如何调用另一个对话框(Win32 应用程序)

wpf - 为什么我不能在我的 UserControl 中重置 TextBox 的背景?

dependencies - 如何直接从 debian/control 文件安装构建依赖项?

c# - 查询 x64-GAC

c# - UWP 有没有在 Canvas 中实现调整大小和移动文本框的控件?

C++函数将文本居中到给定大小

c++ - 通过 UDP 套接字传输结构的问题

c++ - 未知的内存泄漏?

c++ - 如何在派生类函数中修改基类变量的保护值?