gpt4 book ai didi

c - 如何为 Win32 应用程序启用工具栏的现代外观?

转载 作者:可可西里 更新时间:2023-11-01 09:29:46 31 4
gpt4 key购买 nike

最近开始学习基于Win32 API的GUI编程。当我将工具栏控件(来自 comctl32.lib)添加到我的简单应用程序时,我发现它看起来很平坦,而菜单栏具有更多“Windows 7 友好”的 3D 外观和感觉(顶部的白色发光和从上到下的垂直灰色渐变)。差异使它看起来有线。

Default ToolBar style

但是,我发现许多其他应用程序的菜单栏和工具栏具有一致的外观。例如。 Notepad++Notepad 2 .

Notepad++ Notepad 2

看了一些官方文档,尝试了解决方案,比如Visual Styles Overview , Enabling Visual Styles但是,它只启用平面按钮样式(与旧的 Win98 实心按钮样式相比)。这与我在第一张图片中获得的样式没有任何区别。

我试图阅读 Notepad++ 的源代码。找到ToolBar.cpp并相应地对我的代码进行了一些更改,但没有任何改变。我想我迷失在代码库中了。

这是我调用 InitCommonControlsEx 和创建工具栏的代码。

// in WinMain
INITCOMMONCONTROLSEX icce;
icce.dwSize = sizeof(INITCOMMONCONTROLSEX);
icce.dwICC = ICC_BAR_CLASSES | ICC_COOL_CLASSES | ICC_USEREX_CLASSES;
InitCommonControlsEx(&icce);

// called in WM_CREATE handler, hwnd is the handle of the main window
VOID BuildToolBar(HWND hwnd)
{
HWND hTool;
TBBUTTON tbb[3];
TBADDBITMAP tbab;

hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | TBSTYLE_TOOLTIPS |TBSTYLE_FLAT | CCS_TOP | BTNS_AUTOSIZE, 0, 0, 0, 0, hwnd, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hTool, TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_HIDECLIPPEDBUTTONS);

tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = ID_FILE_NEW;

tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = ID_FILE_OPEN;

tbb[2].iBitmap = STD_FILESAVE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = ID_FILE_SAVEAS;

SendMessage(hTool, TB_SETBUTTONSIZE, (WPARAM)0, (LPARAM)MAKELONG(16, 16));
SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb) / sizeof(TBBUTTON), (LPARAM)&tbb);
SendMessage(hTool, TB_AUTOSIZE, 0, 0);
}

所以,问题是:尽管按钮有位图,但我怎样才能获得工具栏的 3D 外观,就像 Notepad++/Notepad2 示例一样?

提前致谢。

最佳答案

先创建Toolbar,再创建Rebar。

rbBand.hbmBack 必须设置为 NULL 以获得现代外观的工具栏,或者从下面的代码中删除 RBBIM_BACKGROUND 标志。

创建钢筋的方法如下:

HWND WINAPI CreateRebar (HWND hwndOwner)
{
REBARINFO rbi;
REBARBANDINFO rbBand;
RECT rc;
HWND hwndCB, hwndRB;
DWORD dwBtnSize;

hwndRB = CreateWindowExW(WS_EX_TOOLWINDOW,
REBARCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN | RBS_VARHEIGHT |
CCS_NODIVIDER,
0, 0, 0, 0,
hwndOwner,
NULL,
GetModuleHandleW(NULL),
NULL);
if (!hwndRB)
return NULL;
// Initialize and send the REBARINFO structure.
rbi.cbSize = sizeof(REBARINFO); // Required when using this
// structure.
rbi.fMask = 0;
rbi.himl = (HIMAGELIST)NULL;
if (!SendMessage(hwndRB, RB_SETBARINFO, 0, (LPARAM)&rbi))
return NULL;
// Initialize structure members that both bands will share.
rbBand.cbSize = sizeof(REBARBANDINFO); // Required
rbBand.fMask = RBBIM_COLORS | RBBIM_TEXT | RBBIM_BACKGROUND |
RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE |
RBBIM_SIZE;
rbBand.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP;
rbBand.hbmBack = NULL; //
// Create the combo box control to be added.
hwndCB = CreateWindowW(TEXT("COMBOBOX"), NULL,
WS_CHILD | WS_VISIBLE | CBS_HASSTRINGS | CBS_DROPDOWNLIST,
410, 20, 120, 110, (HWND) NULL, NULL, NULL, NULL);;
// Set values unique to the band with the combo box.
GetWindowRect(hwndCB, &rc);
rbBand.lpText = "Combo Box";
rbBand.hwndChild = hwndCB;
rbBand.cxMinChild = 0;
rbBand.cyMinChild = rc.bottom - rc.top;
rbBand.cx = 200;

// Add the band that has the combo box.
SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);


// Get the height of the toolbar.
dwBtnSize = SendMessage(toolbar1, TB_GETBUTTONSIZE, 0, 0);

// Set values unique to the band with the toolbar.
rbBand.lpText = "Tool Bar";
rbBand.hwndChild = toolbar1;
rbBand.cxMinChild = 0;
rbBand.cyMinChild = HIWORD(dwBtnSize);
rbBand.cx = 250;

// Add the band that has the toolbar.
SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
return (hwndRB);
}

关于c - 如何为 Win32 应用程序启用工具栏的现代外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134937/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com