windows - 工具栏中按钮的大小 - win32 程序

标签 windows winapi button toolbar

我正在用我的位图图像创建工具栏,但按钮大小有问题。
此图像大小为 20/20 像素。

enter image description here

我创建了一个工具栏,并通过以下代码将按钮大小设置为 20/20 像素:

SendMessage(hToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(20, 20));

并且我将配色方案设置为红色和绿色,这样当光标悬停在按钮上时,按钮框架会清晰显示。

这是当光标停留在按钮上时我看到的:

enter image description here

如您所见,按钮大小不是 20\20 像素,而是 26 像素。那为什么会这样呢?

还有一个问题,当鼠标光标在它上面时,是否可以取消突出显示按钮,而不是我将设置热门图像列表(通过 TB_SETHOTIMAGELIST 消息),所以当光标将站在按钮上方,将显示热门图像,但不会突出显示按钮。

这是完整的代码:

#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);

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

    // set color scheme to blue
    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);

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

最佳答案

Windows 在位图周围添加填充。您可以通过 TB_GETPADDING 以编程方式检测多少给控件的消息。在我的工具栏创建中,我做了这样的事情:

// By default Windows creates the tiles as 22x23, minus the padding area of 6x7, for a bitmap size of 16x16.
    DWORD iSize = SendMessage(hTool, TB_GETBUTTONSIZE, 0, 0);
    DWORD iPadSize = SendMessage(hTool, TB_GETPADDING, 0, 0);

    // Build our bitmap
    int xsize=LOWORD(iSize) - LOWORD(iPadSize);  // width
    int ysize=HIWORD(iSize) - HIWORD(iPadSize);  // height

您可以使用 TB_SETBUTTONSIZE 设置您自己的位图大小,但您仍然必须允许填充。

关于windows - 工具栏中按钮的大小 - win32 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883251/

相关文章:

linux - 控制 Linux 和 Windows 上的内存虚拟化

windows - 如何关闭打开的 OLE 对话框

创建文件函数

c++ - 如何检查在我的程序运行的控制台上设置了什么字符编码?

c - WINAPI 会自动释放附加到控件的内存吗?

android - 将点击监听器设置为 ListView Android 中的按钮

python - Paster 守护进程不会关闭,因为无法读取自己的 pid 文件

windows - 何时以及在批处理文件中的 FOR/F 和 WMIC 中转义什么

java - 表达式预期错误

jquery - 如何使用纯 css 按钮(无图像)更改输入按钮?