baidu

[WM]CDialog ScollBar的显示

CDialog的ScrollBar


窗口上面的控件超出了Window的显示区域,就需要Scrollbar.这一操作在C#里面是极其简单的,但是在Win32(MFC)里面,就不是那么简单了……下面给出怎么搞出来Scrollbar,给那些需要帮助的人,以及给自己做一个标记.


设类CMyDialog继承自CDialog.

给CMyDialog添加成员:

 

    CRect m_ClientRect;
    int  m_nVertInc,m_nVscrollMax,m_nVscrollPos;
    void SetupScrollbars();
    void ResetScrollbars();


下来就是显示Scrollbar.需要的时候显示,办法很简答,就是获取最下面一个控件的RECT的bottom,如果bottom>窗口的Bottom,那么就显示:

//这个我没仔细看MSDN,发现能让Scrollbar出来我就没管了..

this->EnableScrollBarCtrl(1,TRUE);

 

下来就是处理WM_VSCROLL消息:

 

void CMyPropertyPage::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	int nInc; 

	switch (nSBCode)
	{
	case SB_TOP:        nInc = -m_nVscrollPos;               break;
	case SB_BOTTOM:     nInc = m_nVscrollMax-m_nVscrollPos;  break;
	case SB_LINEUP:     nInc = -1;                           break;
	case SB_LINEDOWN:   nInc = 1;                            break;
	case SB_PAGEUP:     nInc = min(-1, -m_nVertInc);         break;
	case SB_PAGEDOWN:   nInc = max(1, m_nVertInc);           break;
	case SB_THUMBTRACK: nInc = nPos - m_nVscrollPos;         break;
	default:            nInc = 0;
	}

	nInc = max(-m_nVscrollPos, min(nInc, m_nVscrollMax - m_nVscrollPos));

	if (nInc)
	{
		m_nVscrollPos += nInc;
		int iMove = -VERT_PTS * nInc;
		//就这一句
		ScrollWindowEx(0, iMove, NULL, NULL,NULL,NULL,SW_SCROLLCHILDREN);
		SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
	}

	CPropertyPage::OnVScroll(nSBCode, nPos, pScrollBar);	
}

 

 

//重置Scrollbar的位置

 

void CMyPropertyPage::ResetScrollbars()
{
	//Scroll the Window to its initial Position
	ScrollWindow(0, m_nVscrollPos*VERT_PTS, NULL, NULL);
	m_nVscrollPos = 0;

	//Setup and Initialize a Vertical Scroll bar
	//You can do the same for Horizontal Scroll bar
	SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
}

//设置Scrollbar的边界

 

void CMyPropertyPage::SetupScrollbars()
{
	CRect tempRect;
	GetClientRect(&tempRect);

	m_nVertInc = (m_ClientRect.Height() - tempRect.Height())/VERT_PTS;

	m_nVscrollMax = max(0, m_nVertInc);
	m_nVscrollPos = min(m_nVscrollPos, m_nVscrollMax);
	SetScrollRange(SB_VERT, 0, m_nVscrollMax, FALSE);
	SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);	
}


下来别忘了添加对WM_VSCROLL消息的映射:

ON_WM_VSCROLL(WM_VSCROLL,&CMyDialog::OnVScroll)

 

 

这样,那个Scrollbar就能工作了.


PS:

WinCE上没有ScrollWindow,只有ScrollWindowEx这个API.MFC里面自己有一个ScrollWindow,不知道怎么实现的,貌似不好使.

posted @ 2010-04-06 12:27  egmkang  阅读(1519)  评论(0编辑  收藏  举报