我正在使用自己的位图图片创建工具栏,并且在按钮大小上遇到了问题。
该图片尺寸为20/20像素。
我创建了一个工具栏,并将按钮大小设置为20/20像素,使用以下代码:
SendMessage(hToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(20, 20));
我将颜色方案设置为红色和绿色,这样当鼠标指针停留在按钮上时,按钮框架将清晰可见。
当鼠标指针停留在按钮上时,我看到的是这样的:
如您所见,按钮大小不是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);
}
}