Hi,
basically I have small project which in it there is a custom window which can carry out other control, the code for it is down below. so the problem is that it have to have a horizontal scroll bar. well if you add WS_HSCROLL to its style then you should control its stuff like up and down arrow dragging the thumb and so and so forth. to be precise i actually have made it working like 50% but still there are few problem which i thought i really should get help with, now the stuff which is working is up and down arrow i mean like if you press the scrollbar up and down arrow it scroll alright so you could drag the thumb either. now here is the stuff not working. first-: it has its limitation i mean like in scroll bar info structure there is the class which says
si.nMax = 100 which make he scroll bar only be scrollable i guess till 100 lines. even if you don't need that much of line to show it just scroll it that much and no more. but i want to be like a textbox like as much you type on the textbox it just scroll that much. Problem number two:- is that you can't press on the space of scroll bar i mean that space which you press and it just scroll you one page or something the place that carry the thumb, hope that do not make any confusion.
Here is the code how i create the custom window with WS_HSCROLL stype:
WNDCLASSEX wcs;
wcs.cbSize = sizeof(wcs);
wcs.lpszClassName = szClassName;
wcs.hInstance = GetModuleHandle(0);
wcs.lpfnWndProc = CustWndProc;
wcs.hCursor = LoadCursor(NULL, IDC_ARROW);
wcs.hIcon = 0;
wcs.lpszMenuName = 0;
wcs.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcs.style = 0;
wcs.cbClsExtra = 0;
wcs.cbWndExtra = 0;
wcs.hIconSm = 0;
if(!RegisterClassEx(&wcs))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwndCtrl = CreateWindowEx(
0L, szClassName,
_T("A custom control"),
WS_VISIBLE|WS_CHILD|WS_BORDER|WS_VSCROLL,
0, 0, 0, 0,
hWnd,
NULL, GetModuleHandle(0), CustWndProc
);
ShowWindow (hwndCtrl, SW_SHOW);
and here is the code for handling its messages:
HWND Scrollbar;
LRESULT CALLBACK CustWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
TRACKMOUSEEVENT tmv = {0};
RECT rc = {};
GetClientRect(hwnd, &rc);
const SIZE sz = { rc.right - rc.left, rc.bottom - rc.top };
SCROLLINFO si;
switch(msg)
{
case WM_MOUSEHOVER:
::MessageBox(hwnd, "Enter", "Info", MB_OK);
return 0;
case WM_LBUTTONDOWN:
{
SCROLLINFO si = { 0 };
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = 0;
si.nTrackPos = 0;
GetScrollInfo(hwnd, SB_VERT, &si);
break;
}
case WM_VSCROLL:
{
auto action = LOWORD(wParam);
HWND hScroll = (HWND)lParam;
int pos = -1;
if (action == SB_THUMBPOSITION || action == SB_THUMBTRACK) {
pos = HIWORD(wParam);
} else if (action == SB_LINEDOWN) {
pos = g_scrollY + 30;
} else if (action == SB_LINEUP) {
pos = g_scrollY - 30;
}
if (pos == -1)
break;
SCROLLINFO si = { 0 };
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = pos;
si.nTrackPos = 0;
SetScrollInfo(hwnd, SB_VERT, &si, true);
GetScrollInfo(hwnd, SB_VERT, &si);
pos = si.nPos;
POINT pt;
pt.x = 0;
pt.y = pos - g_scrollY;
auto hdc = GetDC(hwnd);
LPtoDP(hdc, &pt, 1);
ReleaseDC(hwnd, hdc);
ScrollWindow(hwnd, 0, -pt.y, NULL, NULL);
g_scrollY = pos;
return 0;
}
case WM_CREATE:
int w , h;
w = 10;
h = 10;
HWND buttons;
for(h=10;h<5000; h+=35){
buttons = CreateWindow("BUTTON", "How", WS_VISIBLE|WS_CHILD, w, h, 50, 30, hwnd, (HMENU)1231,NULL, NULL);
}
RECT rc;
GetClientRect(hwnd, &rc);
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = 40;
si.nPage = (rc.bottom - rc.top);
si.nPos = 0;
si.nTrackPos = 0;
SetScrollInfo(hwnd, SB_VERT, &si, true);
int width, height;
width = LOWORD(lParam); height = HIWORD(lParam);
return 0;
case WM_SIZE:
{
RECT rc = { 0 };
GetClientRect(hwnd, &rc);
SCROLLINFO si = { 0 };
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = 1000;
si.nPage = (rc.bottom - rc.top);
si.nPos = 0;
si.nTrackPos = 0;
SetScrollInfo(hwnd, SB_VERT, &si, true);
break;
}
case WM_INITDIALOG:
return TRUE;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwndCtrl, &ps);
EndPaint(hwndCtrl, &ps);
break;
int wmId, wmEvent;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return FALSE;
}
as you can see in WM_CREATE it automatically create so button with for loop but in scroll portion it just show around 15 of it. all replies are really appreciated.