我寫的win32程序,我有一個滾動條的窗口,現在我添加了一個工具欄。 我的問題是,當我滾動窗口時,工具欄會滾動。 如何將工具欄設置在他的位置,如菜單?工具欄滾動,當我滾動窗口
這是代碼的例子,說明如何在工具欄滾動滾動窗口時,代碼是不完美的,但它顯示瞭如何將工具欄滾動,當你sroll窗口排隊,或下移行:
#include <windows.h>
#include <stdlib.h>
#include <CommCtrl.h>
#pragma comment(lib, "comctl32.lib")
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);
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:
{
// create toolbar
HWND hWndToolbar = CreateWindowEx(0 , TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_TOOLTIPS,
0, 0, 0, 0, hWnd, (HMENU)0, instance, NULL);
HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR16 | ILC_MASK, 3, 0);
SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList);
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[4] =
{
{0,0,TBSTATE_ENABLED,TBSTYLE_BUTTON,},
{1,1,TBSTATE_ENABLED,TBSTYLE_BUTTON,},
{2,2,TBSTATE_ENABLED,TBSTYLE_BUTTON,},
{0,0,TBSTATE_ENABLED,BTNS_SEP}
};
SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 4, (LPARAM)&tbb);
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar , SW_SHOW);
// scrollbar
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMax = 1000;
si.nMin = 0;
si.nPos = 0;
si.nPage = 500;
SetScrollInfo(hWnd, SBS_VERT, &si, TRUE);
}
break;
case WM_VSCROLL:
switch(LOWORD(wParam))
{
case SB_LINEDOWN:
ScrollWindow(hWnd, 0, -20, NULL, NULL);
break;
case SB_LINEUP:
ScrollWindow(hWnd, 0, 20, NULL, NULL);
break;
}
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
您的第一個兩個解決方案不起作用。 第三種解決方案是作品,而有益的,但它看起來有點彆扭,我相信有一些方法,使工具欄將不包括在父窗口的客戶區完全一樣,不包括在父窗口的菜單客戶區,並且不會隨父窗口一起滾動。 ---也許我應該設置工具欄的父窗口是菜單嗎? – user1544067
菜單位於非客戶區域。如果您添加了狀態欄,您也會遇到同樣的問題,該狀態欄也會放在客戶區中。 – CoreyStup
我想有一種方法可以在客戶區外創建工具欄,就像在Microsoft Word中一樣。 – user1544067