1、????建立工程時(shí),將View類繼承自CScrollView
2、????
在
OnInitialUpdate
中添加初始化時(shí)
Scroll
的相關(guān)屬性值
CSize sizeTotal(0,0);
SetScrollSizes(MM_TEXT,sizeTotal);
3、????
打開(kāi)文件
(
圖像后
)
根據(jù)圖像的大小,設(shè)置滾動(dòng)屬性值
ChangeScrollRange();?? //
自定義函數(shù)
4、????
在
View
類中添加成員變量。
作用是存儲(chǔ)圖像顯示位置與原點(diǎn)的
offset
????????
int
?????????????
m_ImgVScrollPos
;???????????????????????????????????
//?????? VScroll distance
int
?????????????
m_ImgHScrollPos
;??????????????????????????????????
//?????? HScroll distance
5、????
滾動(dòng)處理
添加自定義響應(yīng)
WM_VSCROLL
、
WM_HSCROLL
消息的事件函數(shù):
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
函數(shù)中圖像顯示的代碼:
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
庫(kù)函數(shù),設(shè)置滾動(dòng)條屬性。參數(shù)含義參見(jiàn)
MSDN
。
void SetScrollSizes(
?? int nMapMode,?????????????????????????????????????????? //
影射模式。
?? SIZE sizeTotal,?????????????????????????????????????????? //
滾動(dòng)范圍,即所有
page
加起來(lái)的高度或?qū)挾?/span>
?? const SIZE& sizePage = sizeDefault,???????? //
每頁(yè)的大小。根據(jù)模式的不同,度量的尺度不同
?? const SIZE& sizeLine = sizeDefault ???????? //
每行的大小。根據(jù)模式的不同,度量的尺度不同
);
?
nMapMode
,一般使用
MM_TEXT
,以像素為單位。參見(jiàn)
MSDN
?
?????????
BitBlt
:
將內(nèi)存中的圖拷貝到屏幕上進(jìn)行顯示。參數(shù)含義
參見(jiàn)
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
);
如有不明白,請(qǐng)留言。