WTL 给一个对话框添加垂直滚动条

         阅读(657)  2017-04-15 12:11:23

消息映射宏

MESSAGE_HANDLER(WM_VSCROLL, OnVscroll)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)

在OnInitDialog里获取窗口的实际大小,以及初始化滚动条位置

::GetWindowRect(m_hWnd, &m_wndRect);
ScreenToClient(&m_wndRect);
m_nScrollPos = 0;

响应消息


	LRESULT OnVscroll(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		int curPos = HIWORD(wParam);
		int nDelta = 0;
		int nScrollStep = (m_nScrollRange) / 9;
		m_nScrollPos = ::GetScrollPos(m_hWnd, SB_VERT);
		switch (LOWORD(wParam))
		{
		case SB_LINEDOWN:
			if (m_nScrollPos >= m_nScrollRange)
			{
				return 0;
			}
			nDelta = min(nScrollStep, m_nScrollRange - m_nScrollPos);
			break;
		case SB_LINEUP:
			if (m_nScrollPos <= 0)
			{
				return 0;
			}
			nDelta = -min(nScrollStep, m_nScrollPos);
			break;
		case SB_PAGEDOWN:
			if (m_nScrollPos >= m_nScrollRange)
			{
				return 0;
			}
			nDelta = min(m_nScrollRange, m_nScrollRange - m_nScrollPos);
			break;
		case SB_PAGEUP:
			if (m_nScrollPos <= 0)
			{
				return 0;
			}
			nDelta = -min(m_nScrollRange, m_nScrollPos);
			break;
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			nDelta = curPos - m_nScrollPos;
			break;
		default:
			break;
		}
		m_nScrollPos += nDelta;
		::SetScrollPos(m_hWnd, SB_VERT, m_nScrollPos, TRUE);
		::ScrollWindow(m_hWnd, 0, -nDelta, NULL, m_wndRect);
		::UpdateWindow(m_hWnd);
		return 0;
	}
	LRESULT OnSize(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
	{
		SCROLLINFO si;
		si.cbSize		= sizeof(SCROLLINFO);
		si.fMask		= SIF_ALL;
		si.nMin			= 0;
		si.nMax			= m_wndRect.Height();			//窗口的实际高度
		si.nPage		= HIWORD(lParam);				//窗口的显示高度
		si.nPos			= 0;
		SetScrollInfo(SB_VERT, &si, TRUE);
		
		m_nScrollRange = si.nMax - si.nPage;			//滚动范围
		return 0;
	}
	LRESULT OnMouseWheel(UINT , WPARAM wParam, LPARAM , BOOL& )
	{
		MouseWheelHandle(HIWORD(wParam));
		return 0;
	}
	void MouseWheelHandle(short nDir)
	{
		if (nDir > 0)
		{
			::SendMessage(m_hWnd, WM_VSCROLL, SB_LINEUP, 0);
		}
		else if (nDir < 0)
		{
			::SendMessage(m_hWnd, WM_VSCROLL, SB_LINEDOWN, 0);
		}
	}

(迁移2011-05-08)

文章评论

Keep it simple,stupid
文章数
284
总访问量
263229
今日访问
142
最近评论

ningto : 请到next.ningto.com里发表评论。
tujiaw : 抱歉csdn code服务关闭了,这个代码我也找不到了
于淞 : 你好,这个文章的源码能分享一下吗,songsong9181@163.com,谢谢了 上面的写错了
于淞 : 你好,这个文章的源码能分享一下吗,838106303@163.com,谢谢了 上面的链接不能用了
tujiaw : 多谢多谢
essaypinglun college-paper.org : 很好的博客,赞赞
Folly : 这个实现有点奇怪,Qt为什么没有统一的比对方法。
过多s : alert("hello, world!")
tujiaw : 还不错哦
回到顶部