Как создать Toolbar на Winapi только с текстом, т.е. без иконки ?
![]() |
Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
|
| ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
| [216.73.216.28] |
|
|
Правила раздела C/C++: Системное программирование и WinAPI
FAQ Сайта (C++)
FAQ Форума
Наши Исходники
Поиск по Разделу
MSDN Library Online (Windows Driver Kit)
Google
Как создать Toolbar на Winapi только с текстом, т.е. без иконки ?
|
Сообщ.
#1
,
|
|
|
|
Прикреплённая картинка
Подскажите, пожалуйста, как сформировать toolbar, в которых на кнопках отображается только текст. Тестовая программа (классич. прилож. Windows) прилагается. По замыслу CreateButtons2 должна создавать Toolbar с кнопками. Сам toolbar создается (серая полоска вверху окна), кнопки с текстом - нет. Описание кнопок хранится в массиве ButtonsArray. ![]() ![]() #pragma once #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> // ---------------------------------------- #include <vector> #include <string> #include <assert.h> #include <winuser.h> #pragma comment( lib, "comctl32" ) #include <commctrl.h> void DebugOutput_c(const char* msg) { size_t convertedChars = 0; size_t newsize = strlen(msg) + 1; wchar_t* result_wstring = new wchar_t[newsize]; mbstowcs_s(&convertedChars, result_wstring, newsize, msg, _TRUNCATE); OutputDebugStringW(result_wstring); delete[] result_wstring; } void DebugOutput_w(const std::wstring& msg) { OutputDebugStringW(msg.c_str()); } void DebugOutput_s(const std::string& s) { DebugOutput_c(s.c_str()); } HINSTANCE g_hInst; const WCHAR* szMainWindowTitle = L"--- TEST ---"; const WCHAR* szMainWindowClass = L"WinClass"; HWND g_hMainWindow = NULL; LRESULT CALLBACK WndProc_MainWin(HWND, UINT, WPARAM, LPARAM); // --------------------------------- struct ButtonDescription_STRUCT { std::wstring Caption; int ButtonID; }; using ButtonDescriptions_ARRAY = std::vector<ButtonDescription_STRUCT>; #define ID_BUTTON_1 1 #define ID_BUTTON_2 2 const ButtonDescriptions_ARRAY ButtonsArray = { {L"PLUS", ID_BUTTON_1}, {L"MINUS", ID_BUTTON_2} }; bool CreateButtons2(const ButtonDescriptions_ARRAY& ButtonsArray, HINSTANCE hInst, HWND hMainWindow) { bool ok = true; HWND hToolbar = CreateWindowW(TOOLBARCLASSNAME, NULL, TBSTYLE_WRAPABLE | TBSTYLE_LIST | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, g_hMainWindow, NULL, hInst, NULL); DWORD ErrCode = GetLastError(); assert(hToolbar != NULL); WPARAM wParam = 0; LPARAM lParam = (LPARAM)TBSTYLE_EX_MIXEDBUTTONS; DWORD PrevStyles = SendMessageW(hToolbar, TB_SETEXTENDEDSTYLE, wParam, lParam); RECT rect; for (const ButtonDescription_STRUCT& b : ButtonsArray) { DebugOutput_w(L"Next = " + b.Caption + L"\n"); const int numButtons = 1; TBBUTTON tb{ 0 }; tb.iBitmap = I_IMAGENONE; // 0; tb.idCommand = b.ButtonID; tb.iString = (INT_PTR) b.Caption.c_str(); tb.fsState = TBSTATE_ENABLED; tb.fsStyle = BTNS_BUTTON | BTNS_SHOWTEXT; wParam = 1; lParam = (LPARAM)&tb; int iNew = SendMessageW(hToolbar, TB_ADDBUTTONSW, wParam, lParam); assert(iNew >= 0); DebugOutput_s("iNew = " + std::to_string(iNew) + "\n"); } ShowWindow(hToolbar, SW_SHOW); return ok; } ATOM RegisterClass_MainWin() { WNDCLASSEXW wcex; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc_MainWin; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = g_hInst; wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszClassName = szMainWindowClass; return RegisterClassExW(&wcex); } BOOL InitInstance(HINSTANCE hInstance) { g_hInst = hInstance; g_hMainWindow = CreateWindowW(szMainWindowClass, szMainWindowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!g_hMainWindow) { return FALSE; } return TRUE; } void ShowAndUpdateWin(HWND hWnd, int nCmdShow) { ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(icex); icex.dwICC = ICC_BAR_CLASSES; BOOL res_init = InitCommonControlsEx(&icex); assert(res_init == TRUE); RegisterClass_MainWin(); if (!InitInstance(g_hInst)) { return FALSE; } CreateButtons2(ButtonsArray, g_hInst, g_hMainWindow); ShowAndUpdateWin(g_hMainWindow, SW_NORMAL); MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc_MainWin(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); switch (wmId) { default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; /* case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; } */ case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } |
|
Сообщ.
#2
,
|
|
|
|
А документацию прочитать?
Подсказка: CreateWindowW это макрос который раскрывается в CreateWindowExW https://learn.microsoft.com/en-us/windows/w...s/tb-addbuttons |
|
Сообщ.
#3
,
|
|
|
|
Ты имеешь в виду это? Цитата Remarks If the toolbar was created using the CreateWindowEx function, you must send the TB_BUTTONSTRUCTSIZE message to the toolbar before sending TB_ADDBUTTONS |
|
Сообщ.
#4
,
|
|
|
|
Да, именно эту ремарку.
![]() ![]() SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0); |
|
Сообщ.
#5
,
|
|
|
|
Цитата sharky72 @ А документацию прочитать? Подсказка: CreateWindowW это макрос который раскрывается в CreateWindowExW https://learn.microsoft.com/en-us/windows/w...s/tb-addbuttons sharky72, спасибо! Видимо, на эту часть внимание не обратил. С этим сообщением действительно кнопки появились. |