如何在vc ++ win32中创建滚动条 [英] how to create a scroll bar in vc++ win32

查看:70
本文介绍了如何在vc ++ win32中创建滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上我有一个小项目,里面有一个可以执行其他控制的自定义窗口,它的代码在下面。所以问题是它必须有一个水平滚动条。好吧,如果你将WS_HSCROLL添加到它的样式,那么你应该控制它的东西,如向上和向下箭头拖动拇指等等。确切地说,我实际上已经让它像50%一样工作,但仍然有一些问题,我认为我真的应该得到帮助,现在正在工作的东西是向上和向下箭头我的意思是,如果你上下滚动滚动条箭头它滚动好,所以你可以拖动拇指。现在这里的东西不起作用。第一:它有它的限制我的意思是在滚动条信息结构中有一个类说

si.nMax = 100这使得他滚动条只能滚动我猜到100行。即使你不需要太多的线来显示它只是滚动那么多而不是更多。但我希望像文本框一样,你在文本框中键入的内容就像滚动那么多。问题二: - 是你不能按滚动条的空间我的意思是你按下的空间,它只是滚动你一页或带有拇指的地方,希望不要混淆。



这是我用WS_HSCROLL stype创建自定义窗口的代码:

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, // 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:

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;
}



你可以在WM_CREATE中看到它自动创建带有for循环的按钮,但在滚动部分它只显示15左右。非常感谢所有回复。


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.

推荐答案

您应该将scrollinfo垂直最大值设置为可显示数据中的行数。页面大小是窗口视口中适合的行数。这允许您计算开始显示的位置,用户滚动页面,线条或拖动拇指的位置。水平滚动计算是相同的,但基于窗口的宽度和最宽行中的字符数。对于基于图片的窗口,规则大致相同,但基于像素而不是字符。
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.


这篇关于如何在vc ++ win32中创建滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆