17.9.3 电子相册浏览模块的设计与实现(5)
第7行所定义的区域是相册的区域。
(7)重载对话框的OnPanit()函数。
- void CMyBookDlg::OnPaint()
- {
- CPaintDC dc(this);
- if(m_turnType == type_left || m_turnType == type_endToleft)
- {
- LeftTurn(); //左侧翻页
- }
- else if(m_turnType == type_right || m_turnType == type_headToright)
- {
- RightTurn(); //右侧翻页
- }
- else
- {
- DrawBothEnds(); //封底或封面
- }
- //将内存设备内的内容复制到屏幕上
- dc.BitBlt(0,0,m_bkRect.Width(),m_bkRect.Height(),&dcMemory,0,0,SRCCO PY);
- }
【代码解析】
根据当前的翻页状态决定下一步的翻页动作。当前状态为左侧翻页或为封底到左侧翻页时,将进行左侧翻页,为右侧翻页或为封面到右侧翻页时将进行右侧翻页,否则进行两侧的翻页动作。
(8)函数RightTurn()实现右侧翻页的功能,在前面已经讲过。LeftTurn()实现左侧翻页的功能,实现过程与RightTurn()类似。DrawBothEnds()函数实现封面、封底参与的翻页过程。共分4种情况:封面到右侧翻页;左侧翻页到封面;封底到左侧翻页;右侧翻页到封底。
下面分步讲解DrawBothEnds()函数。
① 变量的定义与计算
- 01 Graphics g(dcMemory.m_hDC);
- 02 Image bk_image(L"背景\\bg002.bmp");
- 03 //绘制背景图像
- 04 g.DrawImage(&bk_image,m_bkRect.left,m_bkRect.top,
- 05 m_bkRect.Width(),m_bkRect.Height());
- 06 Color clrPen(255,0,0,0); //画笔颜色
- 07 Pen penDraw(clrPen,1 ); //画笔
- 08 SolidBrush solidBrush(Color(255, 255, 255, 255)); //画刷
- 09 double a = 45 + ((45 * m_x) /(m_pPage[0]->GetWidth()));
- //对称线角度
- 10 double radians = a * (PI / 180.0);
- 11 double pageUndersideRotationAngle = (180 - (2 * a));
- //区域3旋转角度
- 12 double calculated_y = 0;
- 13 double calculated_x = 0;
- 14 calculated_y = (m_x)* (tan(radians)); //h(见图17.21)
- 15 int width = m_pPage[5]->GetWidth(); //width
- 16 int height = m_pPage[5]->GetHeight(); //height
【代码解析】
这一部分同RightTurn()函数的第1~5行绘制背景图像。第6~8行定义下面需要的画笔与画刷。第10、11行计算对称线的角度及区域3旋转的角度。第14行计算h。第15、16行计算页图像的高度与宽度。
② 翻页完成时的处理
- 01 if (m_x >= width)
- 02 {
- 03 m_x = 0; //初始化m_x
- 04 if(m_turnType == type_head ||m_turnType == type_end)
- 05 { //初始化页面1及页面2
- 06 g.DrawImage(m_pPage[0],m_photoFrameLeft,m_photoFrameTop,
- 07 m_pPage[0]->GetWidth(),m_pPage[0]->GetHei-
- ght());
- 08 g.DrawImage(m_pPage[1],m_photoFrameLeft + m_pPage[0]->Get
- Width(),
- 09 m_photoFrameTop,m_pPage[1]->GetWidth(),
- 10 m_pPage[1]->GetHeight());
- 11 if(m_turnType == type_head) //封面到右侧翻页
- 12 {
- 13 m_turnType = type_headToright;
- 14 PostMessage(WM_MY_TURN,0,0); //发送换页消息
- 15 }
- 16 else
- 17 {
- 18 m_turnType = type_endToleft; //封底到左侧翻页
- 19 PostMessage(WM_MY_TURN,0,0); //发送换页消息
- 20 }
- 21 }
- 22 else if(m_turnType == type_leftTohead) //左侧翻页到封面
- 23 { //只绘制封面即可
- 24 g.DrawImage(m_pPage[4],m_photoFrameLeft + m_pPage[0]->
- GetWidth(),
- 25 m_photoFrameTop,m_pPage[1]->GetWidth(),
- 26 m_pPage[1]->GetHeight());
- 27 m_turnType = type_head; //修改翻页类型
- 28 }
- 29 else if(m_turnType == type_rightToend) //右侧翻页到封底
- 30 { //只绘制封底即可
- 31 g.DrawImage(m_pPage[5],m_photoFrameLeft,m_photoFrameTop,
- 32 m_pPage[0]->GetWidth(),m_pPage[0]->
- GetHeight());
- 33 m_turnType = type_end; //修改翻页类型
- 34 }
- 35 if(m_bTOnce)
- 36 m_bTOnce =false; //结束一次性翻页
- 37 }