设为首页 加入收藏

TOP

17.8 利用GDI+实现翻页效果(3)
2013-10-07 14:39:35 来源: 作者: 【 】 浏览:56
Tags:17.8 利用 GDI 实现 效果

17.8  利用GDI+实现翻页效果(3)

翻转过程完整代码如下:

  1. 01  void CMyBookDlg::RightTurn()  
  2. 02  {  
  3. 03      Graphics g(dcMemory.m_hDC);                 //内存设备  
  4. 04      Image bk_image(L"背景\\bg002.bmp");           //载入背景图像  
  5. 05      Image image(L"001.jpg");  
  6. 06      g.DrawImage(&bk_image,m_bkRect.left,m_bkRect.top,  
  7. 07                  m_bkRect.Width(),m_bkRect.Height());  
  8. 08      Color clrPen(255,0,0,0);                    //定制画刷画笔  
  9. 09      Pen penDraw(clrPen,1 );  
  10. 10      SolidBrush solidBrush(Color(255, 255, 255, 255));  
  11. 11      double a = 45 + ((45 * m_x) /(m_pPage[0]->GetWidth()));  
  12. 12      double radians = a * (PI / 180.0);  
  13. 13      double pageUndersideRotationAngle =  (180 - (2 * a));  
  14.                                                     //计算区域3翻转角度  
  15. 14      double calculated_y = 0;  
  16. 15      double calculated_x = 0;  
  17. 16      calculated_y =  (m_x)* (tan(radians));      //calculated_y就是h  
  18. 17      int width = m_pPage[0]->GetWidth();     //图像宽度  
  19. 18      int height = m_pPage[0]->GetHeight();       //图像高度  
  20. 19      if (m_x >= width)                           //翻页完成  
  21. 20      {   //直接绘制图像3和图像4  
  22. 21          g.DrawImage(m_pPage[2],m_photoFrameLeft,m_photoFrameTop,  
  23. 22                      m_pPage[2]->GetWidth(),m_pPage[2]->GetHeight());  
  24. 23          g.DrawImage(m_pPage[3],  
  25. 24                      m_photoFrameLeft + m_pPage[2]->GetWidth(),  
  26. 25                      m_photoFrameTop,m_pPage[3]->GetWidth(),  
  27. 26                      m_pPage[3]->GetHeight());  
  28. 27          PostMessage(WM_MY_TURN,0,0);  
  29. 28          if(m_bTOnce)  
  30. 29              m_bTOnce = false;  
  31. 30      }  
  32. 31      else  
  33. 32      {   //首先将图像1和图像2完全绘制出来  
  34. 33          g.DrawImage(m_pPage[0],m_photoFrameLeft,m_photoFrameTop,  
  35. 34                      m_pPage[0]->GetWidth(),m_pPage[0]->GetHeight());  
  36. 35          g.DrawImage(m_pPage[1],  
  37. 36                      m_photoFrameLeft + m_pPage[0]->GetWidth(),  
  38. 37                      m_photoFrameTop,m_pPage[1]->GetWidth(),  
  39. 38                      m_pPage[1]->GetHeight());  
  40. 39          GraphicsPath gpLeft,gpRight;  
  41. 40          int undersideOffset = width;  
  42. 41          if(calculated_y < height)  
  43. 42          {   //计算三角形时区域2的路径  
  44. 43              gpLeft.AddLine( PointF(abs(width - m_x + undersideOffset),   
  45.                 height),  
  46. 44                              PointF(width + undersideOffset, height));  
  47. 45              gpLeft.AddLine( PointF(width + undersideOffset, height),  
  48. 46                              PointF(width + undersideOffset,  
  49. 47                              (height - (float)calculated_y)));  
  50. 48          }  
  51. 49          else  
  52. 50          {   //计算梯形时区域2的路径  
  53. 51              gpLeft.AddLine(PointF(abs(width - m_x + undersideOffset),               height),  
  54. 52                              PointF(width + undersideOffset, height));  
  55. 53              gpLeft.AddLine(PointF(width + undersideOffset, height),  
  56. 54                              PointF(width + undersideOffset, 0));  
  57. 55              calculated_x = height / tan(radians);        
  58. 56              gpLeft.AddLine( PointF(width + undersideOffset, 0),  
  59. 57                              PointF(abs(width - (m_x - (float)                                       calculated_x) +  
  60. 58                              undersideOffset),0));  
  61. 59          }  
  62. 60          gpLeft.CloseFigure();  
  63. 61          Matrix PathTranslationMatrix;  
  64. 62          PathTranslationMatrix.Translate((float)m_photoFrameLeft,  
  65. 63                                      (float)m_photoFrameTop);//移动原点  
  66. 64          gpLeft.Transform(&PathTranslationMatrix);  
  67. 65          Region oldRegion;  
  68. 66          g.GetClip(&oldRegion);                  //保存原属性  
  69. 67          g.SetClip(&gpLeft);                     //设置路径  
  70. 68          g.DrawPath(&penDraw,&gpLeft);  
  71. 69          g.DrawImage(m_pPage[3],  
  72. 70                      m_photoFrameLeft + m_pPage[3]->GetWidth(),  
  73. 71                      m_photoFrameTop,m_pPage[3]->GetWidth(),  
  74. 72                      m_pPage[3]->GetHeight());   //绘制区域2  
  75. 73          g.SetClip(&oldRegion);                  //回复原属性  
  76. 74          //图像3的画板  
  77. 75          Bitmap pageUndersideImage(width,height);  
  78. 76          undersideOffset =  width;  
  79. 77          if(calculated_y < height)  
  80. 78          {   //计算三角形时区域3的路径  
  81. 79              gpRight.AddLine( PointF(abs(width - m_x - undersideOffset),                 height),  
  82. 80                               PointF(width - undersideOffset, height));  
  83. 81              gpRight.AddLine( PointF(width - undersideOffset, height),  
  84. 82                              PointF(width - undersideOffset,  
  85. 83                              (height - (float)calculated_y)));  
  86. 84          }  
  87. 85          else  
  88. 86          {   //计算梯形时区域3的路径  
  89. 87              gpRight.AddLine(PointF(abs(float(width) - m_x – underside-  
  90.                 Offset), height),  
  91. 88                              PointF(width - undersideOffset, height));  
  92. 89              gpRight.AddLine(PointF(width - undersideOffset, height),  
  93. 90                              PointF(width - undersideOffset, 0));  
  94. 91              //绘制对称线  
  95. 92              calculated_x = height / tan(radians);   
  96. 93              gpRight.AddLine( PointF(width - undersideOffset, 0),  
  97. 94                              PointF(abs(width - (m_x - (float)  
  98.                                 calculated_x) –  
  99. 95                              undersideOffset), 0));  
  100. 96          }  
  101. 97          Graphics ug(&pageUndersideImage);  
  102. 98          ug.SetClip(&gpRight);                   //设置图像3的路径  
  103. 99          ug.DrawPath(&penDraw,&gpRight);  
  104. 100         ug.DrawImage(m_pPage[2], 0,0,m_pPage[3]->GetWidth(),  
  105. 101                     m_pPage[3]->GetHeight());   //绘制图像3  
  106. 102         Matrix PathTranslationMatrix1;  
  107. 103         PathTranslationMatrix1.Translate(m_photoFrameLeft +  
  108. 104                                     m_pPage[3]->GetWidth()*2 - m_x,  
  109. 105                                     m_photoFrameTop +  
  110. 106                                     m_pPage[3]->GetHeight());  
  111.                                                     //移动原点  
  112. 107         //将图像3的画板旋转pageUndersideRotationAngle度  
  113. 108         PathTranslationMatrix1.Rotate((float)(pageUndersideRotation-  
  114.             Angle ));  
  115. 109         g.SetTransform(&PathTranslationMatrix1);  
  116. 110         //绘制区域3  
  117. 111         g.DrawImage(&pageUndersideImage, -int(m_x)  ,-height ,width,  
  118.             height );  
  119. 112     }  
  120. 113     PageInit();  
  121. 114 }  

【代码解析】

第3~7行设置内存设备环境,并将背景图像绘制到内存设备上。第8~10行定制画笔和画刷。第11~13行计算对称线角度及区域3需要旋转的角度。第14~18行计算图像的高度,宽度及h(h的含义见上面的分析过程)的值。当整个翻页过程完成后在,第21~26行直接绘制图像3和图像4,此时图像1和图像2已经被完全覆盖,无须再绘制。在翻页过程没有完成时,第33~38行将先绘制图像1和图像2。第44~59行计算区域2的路径,其中第44~48行计算的是三角形时区域2的路径(见图17.21),第50~59行计算的是梯形时区域2的路径(见图17.22)。第60~73行将原点移到图像1的左上角(也就是相册的左上角,这样便于绘图)后,根据区域2的路径,绘制图像4。第77~96行定义了一个区域3的临时画板,并计算出区域3在临时画板上的路径(也是分三角形和梯形计算)。第97~101行根据计算的路径将图像3绘制到新画板上。第102~111行将坐标原点移动到翻动轴点,坐标系旋转pageUndersideRotationAngle后,绘制图像3的临时画板。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇17.9.2 新建相册模块的设计与实现.. 下一篇17.8 利用GDI+实现翻页效果(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: