windows - TB_SETPADDING 消息对垂直填充没有影响

标签 windows winapi button toolbar

我试图将工具栏填充设置为零,但 TB_SETPADDING 消息只影响水平填充,而不是垂直填充。

我将配色方案设置为红色和绿色,以突出显示按钮边框,这是输出:

enter image description here

有人能解释一下为什么会这样吗?

完整代码:

#include <windows.h> 
#include <stdlib.h>
#include <CommCtrl.h>
#pragma comment(lib, "comctl32.lib")

#define IDB_PRINT 40000

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE instance;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    instance = hInstance;

    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style           = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc    = WndProc; 
    wcex.cbClsExtra     = 0; 
    wcex.cbWndExtra     = 0;  
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));  
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName   = NULL; 
    wcex.lpszClassName  = L"Example"; 
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    RegisterClassEx(&wcex);

    HWND hWnd = CreateWindow(L"Example", L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        500, 500, NULL, NULL, hInstance, NULL);

    // Initialize common controls.
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC   = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
    InitCommonControlsEx(&icex);

    // create toolbar
    HWND hToolbar = CreateWindowExW(WS_EX_TOOLWINDOW | TBSTYLE_EX_HIDECLIPPEDBUTTONS, TOOLBARCLASSNAME, NULL, CCS_NODIVIDER | WS_CHILD | WS_VISIBLE | CCS_ADJUSTABLE | TBSTYLE_ALTDRAG | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
        0, 0, 0, 0, hWnd, (HMENU)0, instance, NULL);

    SendMessage(hToolbar, TB_SETMAXTEXTROWS, 0, 0);

    // create image list
    HIMAGELIST hImageList = ImageList_Create(20,20, ILC_COLORDDB, 4, 0);
    ImageList_Add(hImageList, LoadBitmap(instance, MAKEINTRESOURCEW(IDB_PRINT)), NULL);
    ImageList_Add(hImageList, LoadBitmap(instance, MAKEINTRESOURCEW(IDB_PRINT)), NULL);

    // set the image list
    SendMessage(hToolbar, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList);
    SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    // create button
    TBBUTTON tbb[1] = 
    { 
        {0, 0, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Print"},
    };

    // add button to the toolbar
    SendMessage(hToolbar, (UINT)TB_ADDBUTTONS, 1, (LPARAM)&tbb);
    SendMessage(hToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(20, 20));
    SendMessage(hToolbar, TB_AUTOSIZE, 0, 0);
    DWORD res = SendMessage(hToolbar, TB_SETPADDING, 0, MAKELPARAM(0, 0));

    // set color scheme to red and green
    COLORSCHEME cs;
    cs.dwSize = sizeof(cs);
    cs.clrBtnShadow = RGB(255, 0, 0);
    cs.clrBtnHighlight = RGB(0, 255, 0);
    SendMessage(hToolbar, TB_SETCOLORSCHEME, 0, (LPARAM)&cs);

    // set the padding size to zero
    SendMessage(hToolbar, TB_SETPADDING, 0, MAKELPARAM(0, 0));

    // show the toolbar
    ShowWindow(hToolbar, SW_SHOW);

    // show the main window
    ShowWindow(hWnd, nCmdShow);

    MSG msg;

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

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE: 
            return 0;

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

=== 编辑 ===

这是我的资源文件中的内容:

#define IDB_PRINT 40000
IDB_PRINT BITMAP "print.bmp"

这是 print.bmp 文件:
enter image description here

最佳答案

我认为问题出在工具栏窗口的大小,而不是按钮的大小。

如果调用 CreateWindowEx 并包含 CCS_NORESIZE 样式,这将禁用工具栏窗口的默认大小调整行为。 As MSDN says

enter image description here

这是MSDN recommends的技术当工具栏由 rebar 控件托管时

rebar

显然,您必须在必要时“手动”调整工具栏的大小和位置,这更加痛苦,但它确实允许您创建窗口以获得所需的效果。例如,在创建时指定固定的宽度和高度(并应用 CCS_NORESIZE 样式)

// create toolbar
HWND hToolbar = CreateWindowExW(WS_EX_TOOLWINDOW | TBSTYLE_EX_HIDECLIPPEDBUTTONS, TOOLBARCLASSNAME, NULL, 
    CCS_NORESIZE | CCS_NODIVIDER | WS_CHILD | WS_VISIBLE | CCS_ADJUSTABLE | TBSTYLE_ALTDRAG | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
    0, 0, 500, 21, hWnd, (HMENU)0, instance, NULL);

这是一个放大的截图,就像你原来的例子,但有上面的变化(高度设置为 21 以适应 1px y 偏移)

enter image description here

要强制工具栏按钮恰好为 20x20 并移除按钮顶部的填充,还可以使用 CCS_NOPARENTALIGN 样式,然后将窗口原点设置为负 y 值并相应地增加高度。

比如这段代码

// create toolbar
HWND hToolbar = CreateWindowExW(WS_EX_TOOLWINDOW | TBSTYLE_EX_HIDECLIPPEDBUTTONS, TOOLBARCLASSNAME, NULL, 
    CCS_NORESIZE | CCS_NOPARENTALIGN | CCS_NODIVIDER | WS_CHILD | WS_VISIBLE | CCS_ADJUSTABLE | TBSTYLE_ALTDRAG | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
    0, -2, 500, 22, hWnd, (HMENU)0, instance, NULL);

产生这个结果

tbb2

关于windows - TB_SETPADDING 消息对垂直填充没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16964468/

相关文章:

windows - 从命令行更改 Windows 主机名

c++ - Win32 C++ ReadDirectoryChangesW "creation"和 "modification"文件差异检测?

windows - "This program might not have installed correctly"

c++ - 如何让父子窗口的颜色相同?

windows - 跨线程的数值差异(cygwin 上的 openMP)

c++ - 做一个定时器一段时间

javascript - 如何编写一个 JS 按钮来激活单击和滚动时的转换?

java - 在 JButton 32x32 上设置 1 个或 2 个字符的文本

windows - PerfView:所有进程的 CPU MSec 均为 0

Java - 每次按下按钮时都会遍历数组