MFC滚动条的实现

0

1、     建立工程时,将 View 类继承自 CScrollView

2、     OnInitialUpdate 中添加初始化时 Scroll 的相关属性值

CSize sizeTotal(0,0);

SetScrollSizes(MM_TEXT,sizeTotal);

3、     打开文件 ( 图像后 ) 根据图像的大小,设置滚动属性值

ChangeScrollRange();   // 自定义函数

4、     View 类中添加成员变量。

作用是存储图像显示位置与原点的 offset

         int               m_ImgVScrollPos ;                                    //       VScroll distance

int               m_ImgHScrollPos ;                                   //       HScroll distance

5、     滚动处理

添加自定义响应 WM_VSCROLL WM_HSCROLL 消息的事件函数: OnVScroll OnHScroll

void CSockIIView :: OnVScroll ( UINT nSBCode , UINT nPos , CScrollBar * pScrollBar )

{

    // TODO: Add your message handler code here and/or call default

    SCROLLINFO si ;

    GetScrollInfo ( SB_VERT ,& si , SIF_ALL );

    m_ImgVScrollPos = si . nPos ;

    Invalidate ( TRUE );

 

    CScrollView :: OnVScroll ( nSBCode , nPos , pScrollBar );

}

6、     图像显示

更改 OnPaint 函数中图像显示的代码:

dc . BitBlt ( rc . left , rc . top , rc . Width (), rc . Height (), m_pMemDC , rc . left , rc . top , SRCCOPY );

为:

dc.BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),m_pMemDC,(rc.left+m_ImgHScrollPos),(rc.top+m_ImgVScrollPos),SRCCOPY);

 

备注:

?         SetScrollSizes()  MFC 库函数,设置滚动条属性。参数含义参见 MSDN

void SetScrollSizes(

   int nMapMode,                                           // 影射模式。

    SIZE sizeTotal,                                           // 滚动范围,即所有 page 加起来的高度或宽度

   const SIZE& sizePage = sizeDefault,         // 每页的大小。根据模式的不同,度量的尺度不同

   const SIZE& sizeLine = sizeDefault          // 每行的大小。根据模式的不同,度量的尺度不同

);

 

nMapMode ,一般使用 MM_TEXT ,以像素为单位。参见 MSDN

 

?         BitBlt 将内存中的图拷贝到屏幕上进行显示。参数含义 参见 MSDN

BOOL BitBlt(

  HDC hdcDest,       // handle to destination DC

  int nXDest,           // x-coord of destination upper-left corner

  int nYDest,            // y-coord of destination upper-left corner

  int nWidth,           // width of destination rectangle

  int nHeight,             // height of destination rectangle

  HDC hdcSrc,       // handle to source DC

  int nXSrc,            // x-coordinate of source upper-left corner

  int nYSrc,            // y-coordinate of source upper-left corner

  DWORD dwRop  // raster operation code

);
posted @ 2009-08-14 15:15  eric_lgf  阅读(3639)  评论(0编辑  收藏  举报