⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scrollimpl.h

📁 ATL SCROLLING ,ATL窗口滚动实现。滚动条的动态显示。
💻 H
字号:
#ifndef __SCROLLIMPL_H__
#define __SCROLLIMPL_H__



#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

/////////////////////////////////////////////////////////////////////////////
// CScrollImpl
class CScrollImpl : public CMessageMap
{
	
public:

	CScrollImpl() :		m_hScrollWnd(0),
							m_nScrollDist(5),
							m_bHandled(true),
							m_nLastVertPos(0), 
							m_nLastHorzPos(0) {}



	// function to set the size at which the scroll bars will appear.
	// m_hWnd should always be passed into this function
	//
	void SetScrollSize(HWND p_hWnd, long p_cx, long p_cy)
	{
		m_hScrollWnd = p_hWnd;
		m_sizeScroll.cx = p_cx;
		m_sizeScroll.cy = p_cy;
	}

	void GetScrollSize( long& p_cx, long& p_cy )
	{
		p_cx = m_sizeScroll.cx;
		p_cy = m_sizeScroll.cy;
	}

	// sets the new scrolling distance each time the scroll button 
	// is pressed.  This defaults to 5 pixels.
	//
	void SetScrollDist(int p_nScrollDist)
	{
		m_nScrollDist = p_nScrollDist;
	}

	void SetOnSizeHandled ( bool p_bHandled )
	{
		m_bHandled = p_bHandled;
	}

	BEGIN_MSG_MAP(COtlLayoutPlugin)
		MESSAGE_HANDLER(WM_SIZE, OnSizeLayout)
		MESSAGE_HANDLER(WM_VSCROLL, OnVScroll)
		MESSAGE_HANDLER(WM_HSCROLL, OnHScroll)
	END_MSG_MAP()


	LRESULT OnSizeLayout(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if(!m_hScrollWnd)	
		{
			bHandled = FALSE;
			return 0;
		}
		int cx = LOWORD(lParam);  // width of client area 
		int cy = HIWORD(lParam);  // height of client area  

		int l_nVertPos = 0;
		int l_nHorzPos = 0;

		// if scrolling has been set
		//
		if ( m_hScrollWnd )
		{
			// if the height is small enough that we need scrolling
			//
			if ( m_sizeScroll.cy > cy )
			{
				// show the scroll bar
				//
				::ShowScrollBar(m_hScrollWnd, SB_VERT, TRUE );

				// make the client height remain constant
				//
				lParam &= 0x0000ffff;
				lParam |= (int)m_sizeScroll.cy << 16; 

				// set up the new scroll info for the vertical scroll bar
				//
				SCROLLINFO l_si;
				l_si.cbSize = sizeof(SCROLLINFO);
				l_si.fMask = SIF_ALL;
				::GetScrollInfo( m_hScrollWnd, SB_VERT, &l_si );
				l_si.nMin = 0;
				l_si.nMax = m_sizeScroll.cy;
				l_si.nPage = cy;
				l_nVertPos = l_si.nPos;
				::SetScrollInfo( m_hScrollWnd, SB_VERT, &l_si, TRUE );
			}
			// else hide the scroll bar
			//
			else
				::ShowScrollBar(m_hScrollWnd, SB_VERT, FALSE );


			// if the width is small enough to need scroll bars
			//
			if ( m_sizeScroll.cx > cx )
			{
				// show the horizontal scroll bar
				//
				::ShowScrollBar(m_hScrollWnd, SB_HORZ, TRUE );

				// make the client width remain constant
				//
				m_sizeScroll.cx &= 0x0000ffff;
				lParam &= 0xffff0000;
				lParam |= (int)m_sizeScroll.cx;

				// set up the scroll info
				//
				SCROLLINFO l_si;
				l_si.cbSize = sizeof(SCROLLINFO);
				l_si.fMask = SIF_ALL;
				::GetScrollInfo( m_hScrollWnd, SB_HORZ, &l_si );
				l_si.nMin = 0;
				l_si.nMax = m_sizeScroll.cx;
				l_si.nPage = cx;
				l_nHorzPos = l_si.nPos;
				::SetScrollInfo( m_hScrollWnd, SB_HORZ, &l_si, TRUE );
			}

			// else hide the scroll bars
			//
			else
				::ShowScrollBar(m_hScrollWnd, SB_HORZ, FALSE );
		}
		
		// scroll window to proper location
		//
		ScrollWindow( m_hScrollWnd, -(l_nHorzPos - m_nLastHorzPos), -(l_nVertPos - m_nLastVertPos), NULL, NULL );
		m_nLastVertPos = l_nVertPos;
		m_nLastHorzPos = l_nHorzPos;

		bHandled = m_bHandled;
		return S_OK;
	}


	LRESULT OnVScroll(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// get the info from the message
		//
		int l_nScrollCode = (int) LOWORD(wParam); // scroll bar value 
		short l_nPos = (short int) HIWORD(wParam);  // scroll box position 
		HWND l_hScrollBar = (HWND) lParam;      // handle to scroll bar 

		// get info from the scroll bar
		//
		SCROLLINFO l_clScrollInfo;
		l_clScrollInfo.cbSize = sizeof(SCROLLINFO);
		l_clScrollInfo.fMask = SIF_ALL;
		int l_nOldPos;

		::GetScrollInfo( m_hScrollWnd, SB_VERT, &l_clScrollInfo );

		// remember the old scroll position
		//
		l_nOldPos = l_clScrollInfo.nPos;

		// set the new scroll position based on the command
		//
		switch (l_nScrollCode)
		{
		case SB_LINEDOWN:
			l_clScrollInfo.nPos += m_nScrollDist;
			break;
		case SB_LINEUP:
			l_clScrollInfo.nPos -= m_nScrollDist;
			break;
		case SB_PAGEDOWN:
			l_clScrollInfo.nPos += l_clScrollInfo.nPage;
			break;
		case SB_PAGEUP:
			l_clScrollInfo.nPos -= l_clScrollInfo.nPage;
			break;
		case SB_TOP:
			l_clScrollInfo.nPos = l_clScrollInfo.nMin;
			break;
		case SB_BOTTOM:
			l_clScrollInfo.nPos = l_clScrollInfo.nMax;
			break;
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			l_clScrollInfo.nPos = l_clScrollInfo.nTrackPos;
			break;
		case SB_ENDSCROLL:
		default:
			return 0;
		}

		// Make sure that scroll position is in range.     
		if (0 > l_clScrollInfo.nPos)         
			l_clScrollInfo.nPos = 0;     
		else if (l_clScrollInfo.nMax - (int) l_clScrollInfo.nPage + 1 < l_clScrollInfo.nPos)         
			l_clScrollInfo.nPos = l_clScrollInfo.nMax  - l_clScrollInfo.nPage + 1;      
		
		// Set new scroll position.     
		l_clScrollInfo.fMask = SIF_POS;     
		SetScrollInfo(m_hScrollWnd, SB_VERT, &l_clScrollInfo, TRUE);      
		
		// determine the amount of scrolling needed
		//
		int l_nVertScroll = l_nOldPos - l_clScrollInfo.nPos;     
		
		// scroll the window
		//
		ScrollWindow(m_hScrollWnd, 0, l_nVertScroll, NULL, NULL);
		m_nLastVertPos = l_clScrollInfo.nPos;

		return 0;
	}
	LRESULT OnHScroll(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// get the info from the message
		//
		int l_nScrollCode = (int) LOWORD(wParam); // scroll bar value 
		short l_nPos = (short int) HIWORD(wParam);  // scroll box position 
		HWND l_hScrollBar = (HWND) lParam;      // handle to scroll bar 

		// get scroll info from the scroll bar
		//
		SCROLLINFO l_clScrollInfo;
		l_clScrollInfo.cbSize = sizeof(SCROLLINFO);
		l_clScrollInfo.fMask = SIF_ALL;
		int l_nOldPos;

		::GetScrollInfo( m_hScrollWnd, SB_HORZ, &l_clScrollInfo );

		// remember the old position
		//
		l_nOldPos = l_clScrollInfo.nPos;

		// get the new position based on the scroll command
		//
		switch (l_nScrollCode)
		{
		case SB_LINERIGHT:
			l_clScrollInfo.nPos += m_nScrollDist;
			break;
		case SB_LINELEFT:
			l_clScrollInfo.nPos -= m_nScrollDist;
			break;
		case SB_PAGERIGHT:
			l_clScrollInfo.nPos += l_clScrollInfo.nPage;
			break;
		case SB_PAGELEFT:
			l_clScrollInfo.nPos -= l_clScrollInfo.nPage;
			break;
		case SB_RIGHT:
			l_clScrollInfo.nPos = l_clScrollInfo.nMin;
			break;
		case SB_LEFT:
			l_clScrollInfo.nPos = l_clScrollInfo.nMax;
			break;
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			l_clScrollInfo.nPos = l_clScrollInfo.nTrackPos;
			break;
		case SB_ENDSCROLL:
		default:
			return 0;
		}

		// Make sure that scroll position is in range.     
		if (0 > l_clScrollInfo.nPos)         
			l_clScrollInfo.nPos = 0;     
		else if (l_clScrollInfo.nMax - (int) l_clScrollInfo.nPage + 1 < l_clScrollInfo.nPos)         
			l_clScrollInfo.nPos = l_clScrollInfo.nMax  - l_clScrollInfo.nPage + 1;      
		
		// Set new scroll position.     
		l_clScrollInfo.fMask = SIF_POS;     
		SetScrollInfo(m_hScrollWnd, SB_HORZ, &l_clScrollInfo, TRUE);      
		
		// determine the amount of scrolling needed
		//
		int l_nHorzScroll = l_nOldPos - l_clScrollInfo.nPos;     
		
		// scroll the window
		//
		ScrollWindow(m_hScrollWnd, l_nHorzScroll, 0, NULL, NULL);
		m_nLastHorzPos = l_clScrollInfo.nPos;

		return 0;
	}

protected:
	HWND m_hScrollWnd;
	SIZE m_sizeScroll;
	int m_nScrollDist;
	bool m_bHandled;
	long m_nLastVertPos, m_nLastHorzPos;
};

#endif // __SCROLLIMPL_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -