积累的VC编程小技巧之滚动条

1.设置滚动条的滚动大小


创建一个基于CScrollview的SDI Project(在第6步中选CScrollview) 
若你已创建了,这步可以省略。 
然后: 
改为如 
void
CTestView::OnInitialUpdate() 

CScrollView::OnInitialUpdate(); 

CSize
sizeTotal; 
// TODO: calculate the total size of this
view 
sizeTotal.cx = 1024;  //改这两个 
sizeTotal.cy =
768;   // 
SetScrollSizes(MM_TEXT,
sizeTotal); 
}

2.滚动条的控制


BOOL CDiagramShowView::PreTranslateMessage(MSG* pMsg)

{

CFileTreeDoc* pDoc =
(CFileTreeDoc*)GetDocument();

CPoint point =
GetScrollPosition();

if(pMsg->message ==
WM_KEYDOWN)

{

switch(pMsg->wParam)

{

case
VK_LEFT:

if(
point.x > 10)

{

EndPoint.x
= EndPoint.x - 10;

EndPoint.y
= EndPoint.y;

}

else

{

EndPoint.x
= 0;

EndPoint.y
= EndPoint.y;

}

ScrollToPosition(EndPoint);

InvalidateRect(NULL,TRUE);

break;

case
VK_RIGHT:

if(
point.x < pDoc->intDiagramColumnCount * pDoc->intColumnWidth - 10 )

{

EndPoint.x
= EndPoint.x + 10;

EndPoint.y
= EndPoint.y;

}

else

{

EndPoint.y
= pDoc->intDiagramColumnCount * pDoc->intColumnWidth;

EndPoint.x
= EndPoint.x;

}

ScrollToPosition(EndPoint);

InvalidateRect(NULL,TRUE);

break;

case
VK_UP:

if(
point.y > 10)

{

EndPoint.y
= EndPoint.y - 10;

EndPoint.x
= EndPoint.x;

}

else

{

EndPoint.y
= 0;

EndPoint.x
= EndPoint.x;

}

ScrollToPosition(EndPoint);

InvalidateRect(NULL,TRUE);

break;

case
VK_DOWN:

if(
point.y < pDoc->intDiagramRowCount * pDoc->intRowHeight - 10 )

{

EndPoint.y
= EndPoint.y + 10;

EndPoint.x
= EndPoint.x;

}

else

{

EndPoint.y
= pDoc->intDiagramRowCount * pDoc->intRowHeight;

EndPoint.x
= EndPoint.x;

}

ScrollToPosition(EndPoint);

InvalidateRect(NULL,TRUE);

break;

default:

break;

}

}

return FALSE;

}

// 通过正负号判断是向上还是向下滚动

if(zDelta==120)

向上滚动
if(zDelta==-120)
向下滚动

BOOL CDiagramShowView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)

{

CFileTreeDoc* pDoc =
(CFileTreeDoc*)GetDocument();

CPoint point =
GetScrollPosition();

if(zDelta==120)

{

if(
point.y >= 20 )

{

EndPoint.x
= point.x;

EndPoint.y
= point.y;

EndPoint.x
= EndPoint.x;

EndPoint.y
= EndPoint.y - 20;

}

else

{

EndPoint.x
= EndPoint.x;

EndPoint.y
= 0;

}

}

if(zDelta==-120)

{

if(
point.y <= pDoc->intDiagramRowCount * pDoc->intRowHeight - 20 )

{

EndPoint.x
= point.x;

EndPoint.y
= point.y;

EndPoint.x
= EndPoint.x;

EndPoint.y
= EndPoint.y + 20;

}

else

{

EndPoint.x
= EndPoint.x;

EndPoint.y
= EndPoint.y;

}

}

ScrollToPosition(EndPoint);

InvalidateRect(NULL,TRUE);

return
CScrollView::OnMouseWheel(nFlags, zDelta, pt);

}

3.给从CWnd派生的窗口添加滚动条


ModifyStyle(0,WS_VSCROLL);

4.如何用键盘滚动分割的视口


我的问题是当我用鼠标滚动分割窗口时,视口滚动都很正常,但用键盘时,却什么也没有发生.

在你的视图继承类中加入如下两个函数,假定该类为CScrollerView:

void CScrollerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT
nFlags)
{
        BOOL
processed;
        for (unsigned int
i=0;i<
nRepCnt&&processed;i++)
                processed=KeyScroll(nChar);
        if
(!processed)
          
CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CScrollerView::KeyScroll(UINT
nChar)
{
        switch
(nChar)
                {
                case
VK_UP:
                        OnVScroll(SB_LINEUP,0,NULL);
                        break;
                case
VK_DOWN:
                        OnVScroll(SB_LINEDOWN,0,NULL);
                        break;
                case
VK_LEFT:
                        OnHScroll(SB_LINELEFT,0,NULL);
                        break;
                case
VK_RIGHT:
                        OnHScroll(SB_LINERIGHT,0,NULL);
                        break;
                case
VK_HOME:
                        OnHScroll(SB_LEFT,0,NULL);
                        break;
                case
VK_END:
                        OnHScroll(SB_RIGHT,0,NULL);
                        break;
                case
VK_PRIOR:
                        OnVScroll(SB_PAGEUP,0,NULL);
                        break;
                case
VK_NEXT:
                        OnVScroll(SB_PAGEDOWN,0,NULL);
                        break;
                default:
                        return
FALSE; // not for
us
                            
// and let the default
class
                            
// process
it.
                }
  
return TRUE;
}

积累的VC编程小技巧之滚动条,布布扣,bubuko.com

时间: 2024-10-06 19:46:31

积累的VC编程小技巧之滚动条的相关文章

积累的VC编程小技巧之对话框

1.用鼠标移动基于对话框的无标题栏程序的简单方法 void CVCTestDlg::OnLButtonDown(UINT nFlags, CPoint point) {    //一句话解决问题     SendMessage(WM_SYSCOMMAND,0xF012,0);    CDialog::OnLButtonDown(nFlags, point);} 2.对话框消息映射 有对话框A,B从A中发消息给B然后B处理.准备工作,先定义消息,如下#define WM_B_NOTIFY WM_U

积累的VC编程小技巧之工具提示

1.用鼠标移动基于对话框的无标题栏程序的简单方法 void CVCTestDlg::OnLButtonDown(UINT nFlags, CPoint point) {    //一句话解决问题     SendMessage(WM_SYSCOMMAND,0xF012,0);    CDialog::OnLButtonDown(nFlags, point);} 2.对话框消息映射 有对话框A,B从A中发消息给B然后B处理.准备工作,先定义消息,如下#define WM_B_NOTIFY WM_U

积累的VC编程小技巧之树操作

1.如何在TreeList中加图标? [问题提出]  请问treeview控件和treectrl控件的用法有何不同呢?向如何imagelist控件中加图象呀?  [解决方法]  1)    HICON hicon[8];    m_imageList.Create(16,16,0,8,8);    hicon[0]=AfxGetApp()->LoadIcon(IDI_ICON0);    hicon[1]=AfxGetApp()->LoadIcon(IDI_ICON1);    hicon[2

积累的VC编程小技巧之图标、光标及位图

1.图标透明 (1).Windows中的图标其实是有两个图像组成的,其中一个用于与它要显示的位置的图像做"AND"操作,另一个作"XOR"操作. 透明:用"白色"AND,用"黑色"XOR反色:用"白色"AND,用"白色"XOR正常色:用"黑色"AND,用正常颜色XOR.(2). WIN9X中好像是对像素的操作实现透明的WIN2K中就有API直接实现透明了!WIN2K中

积累的VC编程小技巧之框架窗口及其他

1.修改主窗口风格 AppWizard生成的应用程序框架的主窗口具有缺省的窗口风格,比如在窗口标题条中自动添加文档名.窗口是叠加型的.可改变窗口大小等.要修改窗口的缺省风格,需要重载CWnd::PreCreateWindow(CREATESTRUCT& cs)函数,并在其中修改CREATESTRUCT型参数cs.CWnd::PreCreateWindow 函数先于窗口创建函数执行.如果该函数被重载,则窗口创建函数将使用CWnd::PreCreateWindow 函数返回的CREATESTRUCT

积累的VC编程小技巧之文件操作

1.删除文件夹 // 删除文件夹及其所有内容void CBaseDoc::RemoveFolder(const CString &strPathName){    CString path = strPathName;    if (path.Right(1) != _T("\\"))        path += _T("\\");    path += _T("*.*");    CFileFind ff;    BOOL res =

积累的VC编程小技巧之组合框

1.如何正确的得到ComBox的指针 CComboBox *mComb = (CComboBox*)GetDlgItem(IDC_DuanCB); CComboBox *mComb = (CComboBox*)::GetDlgItem(m_hWnd,IDC_DuanCB); 2.CComboBox控件详解 CComboBox控件又称作组合框控件,其有三种形态可供选择,1.简单组合框(Simple)2.下拉组合框(Drop-down)3.下拉列表式组合框(Drop-down list). CCom

积累的VC编程小技巧之列表框

1.列表框中标题栏(Column)的添加 创建一个List Control,其ID为IDC_LIST,在其Styles属性项下的View项里选择Report.Align项里选择Top.Sort项里选择None. 然后在该List所在对话框的类(头文件)里创建ClistCtrl的一个对象m_list然后在.cpp文件的OnInitDialog()之类的函数里实现如下代码: CString strname[3]; strname[0]="Screen Name"; strname[1]=&

积累的VC编程小技巧之属性页

1.属性页的添加: 创建对话框的类,该类要从CpropertyPage继承:然后在要添加该对话框为属性页的类(头文件)里创建CpropertySheet类的一个对象m_tabsheet和新创建的对话框类的对象m_skatch:最后,在.cpp文件里的OnInitDialog()之类的函数里实现如下代码: m_tabsheet.Create(this, WS_CHILD | WS_VISIBLE, 0); //使选项卡的按钮在下面 if(m_tabsheet.m_hWnd) m_tabsheet.