Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C++
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, // give it a standard border
						 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:

C++
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);													// Width Size of hWnd
		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);
		// Parse the menu selections:
		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.
Posted
Comments
Member 11593571 30-Aug-15 1:28am    
hey where did all of my comments go?
Richard MacCutchan 30-Aug-15 4:22am    
in scroll portion it just show around 15 of it.
Not sure what this means, can you explain more?

1 solution

You should set the scrollinfo vertical maximum to be the number of lines in your displayable data. The page size is the number of lines that fit in the window's viewport. This allows you to calculate where to start displaying, when the user scrolls by a page, a line, or a drag of the thumb. The horizontal scroll calculations are the same but based on the window's width and number of characters in the widest line. For a picture based window the rules are much the same but based on pixels rather than characters.
 
Share this answer
 
Comments
Member 11593571 30-Aug-15 11:22am    
hey what is going on, I can't find either of my comment and solutions.
Richard MacCutchan 30-Aug-15 11:34am    
Sorry, no idea. But if you click on your name above, select the "Reputation" tab, and then click on "Recent Reputation Points", you may be able to see if they have been voted away. If not then post a message in http://www.codeproject.com/suggestions.aspx and one of the administrators will help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900