工具栏(ToolBar)
- 平面风格工具栏 虽然已经说明了工具栏的基础,但现在很少使用3D风格的按钮,更多地使用二维的平面风格。 为此,在创建工具栏后,只需向窗口样式中添加TBSTYLE_FLAT即可。程序如下所示。 toolbar10.c
#include <windows.h> #include <commctrl.h> #pragma comment(lib, "comctl32.lib") HWND hToolbar; TBBUTTON tbButton[9]; void onCreate(HWND hWnd, LPARAM lParam) { int i; for (i = 0; i < 9; i++) { tbButton[i].iBitmap = i; tbButton[i].idCommand = i+1; tbButton[i].fsState = TBSTATE_ENABLED; tbButton[i].fsStyle = TBSTYLE_BUTTON; } hToolbar = CreateToolbarEx(hWnd , WS_CHILD|WS_VISIBLE, 0, 9, HINST_COMMCTRL, IDB_STD_LARGE_COLOR, tbButton, 9, 50, 50, 30, 30, sizeof(TBBUTTON)); // 向窗口样式添加TBSTYLE_FLAT SetWindowLong(hToolbar, GWL_STYLE, GetWindowLong(hToolbar,GWL_STYLE)|TBSTYLE_FLAT); } LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp) { switch (uMsg) { case WM_CREATE: onCreate(hwnd, lp); return 0; case WM_SIZE: SendMessage(hToolbar, WM_SIZE, wp, lp); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, uMsg, wp, lp); } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPInst, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wc = { 0, WindowProc, 0, 0, hInst, NULL, LoadCursor(NULL,IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "Demo" }; if (!RegisterClass(&wc)) return FALSE; InitCommonControls(); hWnd = CreateWindowEx(0, "Demo", "工具栏", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 10, 10, 400, 100, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; while (GetMessage(&msg,NULL,0,0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } #include <windows.h> #include <commctrl.h> #pragma comment(lib, "comctl32.lib") HWND hToolbar; TBBUTTON tbButton[9]; void onCreate(HWND hWnd, LPARAM lParam) { int i; for (i = 0; i < 9; i++) { tbButton[i].iBitmap = i; tbButton[i].idCommand = i+1; tbButton[i].fsState = TBSTATE_ENABLED; tbButton[i].fsStyle = TBSTYLE_BUTTON; } hToolbar = CreateToolbarEx(hWnd , WS_CHILD|WS_VISIBLE, 0, 9, HINST_COMMCTRL, IDB_STD_LARGE_COLOR, tbButton, 9, 50, 50, 30, 30, sizeof(TBBUTTON)); // 向窗口样式添加TBSTYLE_FLAT SetWindowLong(hToolbar, GWL_STYLE, GetWindowLong(hToolbar,GWL_STYLE)|TBSTYLE_FLAT); } LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp) { switch (uMsg) { case WM_CREATE: onCreate(hwnd, lp); return 0; case WM_SIZE: SendMessage(hToolbar, WM_SIZE, wp, lp); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, uMsg, wp, lp); } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPInst, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wc = { 0, WindowProc, 0, 0, hInst, NULL, LoadCursor(NULL,IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "mh" }; if (!RegisterClass(&wc)) return FALSE; InitCommonControls(); hWnd = CreateWindowEx(0, "mh", "工具栏", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 10, 10, 400, 100, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; while (GetMessage(&msg,NULL,0,0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
执行结果如下所示。鼠标光标放置的位置会以3D按钮的风格凸显出来。
tooltips01.c
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
void onCreate(HWND hWnd, LPARAM lp) {
HWND hBtn = CreateWindowEx(0, "BUTTON", "BTN", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 100, 30, hWnd, (HMENU)777, ((CREATESTRUCT*)lp)->hInstance, NULL);
HWND hTool = CreateWindowEx(0, TOOLTIPS_CLASS, NULL, TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hBtn, NULL, ((CREATESTRUCT*)lp)->hInstance, NULL);
TOOLINFO ti = { sizeof(TOOLINFO), TTF_SUBCLASS, hBtn };
ti.lpszText = "按钮";
GetClientRect(hBtn, &ti.rect);
SendMessage(hTool, TTM_ADDTOOL, 0, (LPARAM)&ti);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: onCreate(hwnd, lParam); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
WNDCLASS wc = { 0, WindowProc, 0, 0, hInst, NULL, LoadCursor(NULL,IDC_ARROW),
(HBRUSH)(COLOR_WINDOW+1), NULL, "mh" };
if (!RegisterClass(&wc)) return FALSE;
InitCommonControls();
if (!CreateWindowEx(0, "mh", "工具提示", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
100, 100, 350, 100, NULL, NULL, hInst, NULL)) return FALSE;
while (GetMessage(&msg,NULL,0,0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
主题 | 目录 |
TB_ADDBITMAP | 将一个或多个图像添加到可用于工具栏的按钮图像列表中。 |
TB_ADDBUTTONS | 将一个或多个按钮添加到工具栏。 |
TB_ADDSTRING | 将新字符串添加到工具栏的字符串池。 |
TB_AUTOSIZE | 导致调整工具栏的大小。 |
TB_BUTTONCOUNT | 检索工具栏中当前按钮的计数。 |
TB_BUTTONSTRUCTSIZE | 指定 TBBUTTON 结构的大小。 |
TB_CHANGEBITMAP | 更改工具栏中按钮的位图。 |
TB_CHECKBUTTON | 选中或取消选中工具栏中的给定按钮。 |
TB_COMMANDTOINDEX | 检索与指定命令标识符关联的按钮的从零开始的索引。 |
TB_CUSTOMIZE | 显示“ 自定义工具栏 ”对话框。 |
TB_DELETEBUTTON | 从工具栏中删除按钮。 |
TB_ENABLEBUTTON | 启用或禁用工具栏中的指定按钮。 |
TB_GETANCHORHIGHLIGHT | 检索工具栏的定位点突出显示设置。 |
TB_GETBITMAP | 检索与工具栏中的按钮关联的位图的索引。 |
TB_GETBITMAPFLAGS | 检索描述要使用的位图类型的标志。 |
TB_GETBUTTON | 检索有关工具栏中指定按钮的信息。 |
TB_GETBUTTONINFO | 检索工具栏中按钮的扩展信息。 |
TB_GETBUTTONSIZE | 检索工具栏按钮当前的宽度和高度(以像素为单位)。 |
TB_GETBUTTONTEXT | 检索工具栏上按钮的显示文本。 |
TB_GETCOLORSCHEME | 从工具栏控件中检索配色方案信息。 |
TB_GETDISABLEDIMAGELIST | 检索工具栏控件用于显示非活动按钮的图像列表。 |
TB_GET |