17.9.3 电子相册浏览模块的设计与实现(4)
【代码解析】
m_pGPage在PraparePage()函数中会将相应的相片绘制到4个参与翻动的页面。
(5)PrapatePage()函数将开始的4张相片分别绘制到4个参与翻动的页面上。
- 01 void CMyBookDlg::PraparePage()
- 02 {
- 03 Color clrPen(255,0,0,0);
- 04 Pen penDraw(clrPen,1 );
- 05 SolidBrush solidBrush(Color(255, 255, 255, 255));
- 06 m_pGPage[0]->DrawRectangle(&penDraw,m_rectPL.left,m_rectPL.top,
- 07 m_rectPL.Width(),m_rectPL.Height());
- 08 //相片的宽度大于高度
- 09 if(m_pImage[0]->GetWidth()/float(m_rectPL.Width()) >=
- 10 m_pImage[0]->GetHeight()/float(m_rectPL.Height()))
- 11 { //相片的宽度比大于高度比时按照宽度比绘制
- 12 m_rectPLm_rectPL.right = m_rectPL.left + m_rectPL.Width();
- 13 int temp_height = (float(m_rectPL.Width())/
- 14 m_pImage[0]->GetWidth())*m_pImage[0]->
- GetHeight();
- 15 m_rectPL.top += (m_rectPL.Height() - temp_height) / 2;
- //计算高度
- 16 m_rectPLm_rectPL.bottom = m_rectPL.top + temp_height;
- 17 m_pGPage[0]->DrawImage(m_pImage[0],m_rectPL.left +1 ,m_
- rectPL.top +1,
- 18 m_rectPL.Width() -1 ,m_rectPL.Height()
- -1);
- 19 }
- 20 else
- 21 { //相片的高度大于宽度比时按照宽度比绘制图像
- 22 m_rectPLm_rectPL.bottom = m_rectPL.top + m_rectPL.Height();
- 23 int temp_width = (float(m_rectPL.Height())/
- 24 m_pImage[0]->GetHeight())*m_pImage[0]->Get-
- Width();
- 25 m_rectPL.left +=(m_rectPL.Width()-temp_width)/2;; //计算宽度
- 26 m_rectPLm_rectPL.right = m_rectPL.left + temp_width
- 27 m_pGPage[0]->DrawImage(m_pImage[0],m_rectPL.left +1 ,m_
- rectPL.top +1,
- 28 m_rectPL.Width() -1 ,m_rectPL.Height() -1);
- 29 }
- 30 … //页面2、3、4的绘制方法同上
- 31 }
【代码解析】
PraparePage()函数主要是根据图像的宽度比与高度比来绘制图像,当图像的宽度比大于高度比时说明图像在相框中宽占满整个相框,而高则不足,需要根据宽度比计算。当高度比大于宽度比时,则相反,如图17.39所示。
|
| 图17.39 高度比与宽度比 |
(6)在对话框初始化函数中定义一个定时器:
- 01 BOOL CMyBookDlg::OnInitDialog()
- 02 {
- 03 PraparePage();
- 04 SetTimer(m_hWnd,1,100,NULL); //开始定时器
- 05 }
实现定时器函数,增加m_x并重绘界面。- 01 void CMyBookDlg::OnTimer(UINT nIDEvent)
- 02 {
- 03 if(m_bAuto || m_bTOnce)
- 04 {
- 05 m_x+= 20; //增加m_x
- 06 }
- 07 CRect rect(m_photoFrameLeft,0, m_photoFrameLeft + 1080,m_photo
- FrameTop + 600);
- 08 InvalidateRect(rect); //重绘相册
- 09 CDialog::OnTimer(nIDEvent);
- 10 }