DrawDlg.cpp
上传用户:junliqi
上传日期:2022-05-01
资源大小:1869k
文件大小:7k
源码类别:

2D图形编程

开发平台:

Visual C++

  1. // DrawDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "rotation.h"
  5. #include "DrawDlg.h"
  6. #include "rotationDlg.h"
  7. #include <math.h>
  8. #define DU 3.14159/180
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDrawDlg dialog
  16. CDrawDlg::CDrawDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(CDrawDlg::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CDrawDlg)
  20. //}}AFX_DATA_INIT
  21. }
  22. void CDrawDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CDrawDlg)
  26. DDX_Control(pDX, IDC_SCROLLBAR1, m_scroll1);
  27. DDX_Control(pDX, IDC_SCROLLBAR2, m_scroll2);
  28. DDX_Control(pDX, IDC_SCROLLBAR3, m_scroll3);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CDrawDlg, CDialog)
  32. //{{AFX_MSG_MAP(CDrawDlg)
  33. ON_BN_CLICKED(IDC_BUTTON_ROTATION1, OnButtonRotation1)
  34. ON_BN_CLICKED(IDC_BUTTON_ROTATION2, OnButtonRotation2)
  35. ON_BN_CLICKED(IDC_BUTTON_ROTATION3, OnButtonRotation3)
  36. ON_WM_HSCROLL()
  37. ON_WM_PAINT()
  38. ON_WM_TIMER()
  39. ON_BN_CLICKED(IDC_BUTTON_ROTATION4, OnButtonRotation4)
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CDrawDlg message handlers
  44. void CDrawDlg::OnButtonRotation1() 
  45. {
  46. // TODO: Add your control notification handler code here
  47. Ctrl=1;
  48. }
  49. void CDrawDlg::OnOK() 
  50. {
  51. // TODO: Add extra validation here
  52. KillTimer(1);
  53. CDialog::OnOK();
  54. CRotationDlg dlg;
  55. dlg.DoModal();
  56. }
  57. void CDrawDlg::Draw()
  58. {
  59. CWnd *pWnd=GetDlgItem(IDC_DRAW);
  60.     pWnd->UpdateWindow();
  61. CDC *pDC=pWnd->GetDC();
  62. CRect rect;
  63. pWnd->GetClientRect(rect);
  64. int cx=(int)((rect.right-rect.left)/2);
  65. int cy=(int)((rect.bottom-rect.top)/2);
  66. D v[8]={
  67. {-50,-50,50},{-50,50,50},{50,50,50},{50,-50,50},{-50,-50,-50},{-50,50,-50},{50,50,-50},{50,-50,-50}
  68. },d[8];
  69. POINT p0[4],p1[4],p2[4],p3[4],p4[4],p5[4],w[8];
  70. int z[8];
  71. for (int i=0;i<8;i++)
  72. {
  73. d[i].x=v[i].x;
  74. d[i].y=(int)(v[i].y*cos(a*DU)-v[i].z*sin(a*DU));
  75. d[i].z=(int)(v[i].y*sin(a*DU)+v[i].z*cos(a*DU));
  76. v[i].x=(int)(d[i].x*cos(b*DU)+d[i].z*sin(b*DU));
  77. v[i].y=d[i].y;
  78. v[i].z=(int)(d[i].z*cos(b*DU)-d[i].x*sin(b*DU));
  79. d[i].x=(int)(v[i].x*cos(c*DU)-v[i].y*sin(c*DU));
  80. d[i].y=(int)(v[i].x*sin(c*DU)+v[i].y*cos(c*DU));
  81. d[i].z=v[i].z;
  82. w[i].x=d[i].x+cx;
  83. w[i].y=d[i].y+cy;
  84. z[i]=d[i].z;
  85. }
  86. p0[0]=w[0];p0[1]=w[1];p0[2]=w[2];p0[3]=w[3];
  87. p1[0]=w[4];p1[1]=w[5];p1[2]=w[6];p1[3]=w[7];
  88. p2[0]=w[0];p2[1]=w[1];p2[2]=w[5];p2[3]=w[4];
  89. p3[0]=w[1];p3[1]=w[2];p3[2]=w[6];p3[3]=w[5];
  90. p4[0]=w[2];p4[1]=w[3];p4[2]=w[7];p4[3]=w[6];
  91. p5[0]=w[0];p5[1]=w[3];p5[2]=w[7];p5[3]=w[4];
  92. switch(Maxnum(z,7))
  93. {
  94. case 0:fill(p0,p2,p5,0,2,5);break;
  95. case 1:fill(p0,p2,p3,0,2,3);break;
  96. case 2:fill(p0,p3,p4,0,3,4);break;
  97. case 3:fill(p0,p4,p5,0,4,5);break;
  98. case 4:fill(p1,p2,p5,1,2,5);break;
  99. case 5:fill(p1,p2,p3,1,2,3);break;
  100. case 6:fill(p1,p3,p4,1,3,4);break;
  101. case 7:fill(p1,p4,p5,1,4,5);break;
  102. }
  103. }
  104. void CDrawDlg::fill( POINT *x,POINT *y,POINT *z,int i,int j,int q )
  105. {
  106. CWnd*pWnd=GetDlgItem(IDC_DRAW);
  107. pWnd->UpdateWindow();
  108. CDC *pDC=pWnd->GetDC();
  109. CRect rect;
  110. pWnd->GetClientRect(rect);
  111. CDC dcmem;
  112. dcmem.CreateCompatibleDC(pDC);
  113. CBitmap bmp,*oldbmp;
  114. bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
  115.     oldbmp=dcmem.SelectObject(&bmp);
  116. CBrush brush[6];
  117. brush[0].CreateSolidBrush(RGB(255,255,0));
  118. brush[1].CreateSolidBrush(RGB(0,255,255));
  119. brush[2].CreateSolidBrush(RGB(0,255,0));
  120. brush[3].CreateSolidBrush(RGB(255,0,0));
  121. brush[4].CreateSolidBrush(RGB(0,0,255));
  122. brush[5].CreateSolidBrush(RGB(255,0,255));
  123. CBrush *oldbrush=dcmem.SelectObject(&brush[i]);
  124. dcmem.FillSolidRect(rect,RGB(255,255,255));
  125. dcmem.Polygon(x,4);
  126. dcmem.SelectObject(&brush[j]);
  127. dcmem.Polygon(y,4);
  128. dcmem.SelectObject(&brush[q]);
  129. dcmem.Polygon(z,4);
  130. dcmem.SelectObject(oldbrush);
  131. for (int b=0;b<6;b++)
  132. brush[b].DeleteObject();
  133. pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
  134. &dcmem,0,0,SRCCOPY);
  135. dcmem.SelectObject(oldbmp);
  136. bmp.DeleteObject();
  137. dcmem.DeleteDC();
  138. }
  139. int CDrawDlg::Maxnum( int *p,int n )
  140. {
  141. int max=p[0];
  142. int x;
  143. for (int i=0;i<=n;i++)
  144. {
  145. if (max<=p[i])
  146. {
  147. max=p[i];
  148.      x=i;
  149. }
  150. }
  151. return x;
  152. }
  153. void CDrawDlg::OnButtonRotation2() 
  154. {
  155. // TODO: Add your control notification handler code here
  156. Ctrl=2;
  157. }
  158. void CDrawDlg::OnButtonRotation3() 
  159. {
  160. // TODO: Add your control notification handler code here
  161. Ctrl=3;
  162. }
  163. void CDrawDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  164. {
  165. // TODO: Add your message handler code here and/or call default
  166. int nID=pScrollBar->GetDlgCtrlID();
  167. switch (nID)
  168. {
  169. case IDC_SCROLLBAR1:
  170. a=pScrollBar->GetScrollPos();
  171. switch(nSBCode)
  172. {
  173. case SB_LINELEFT:a--;break;
  174. case SB_LINERIGHT: a++;break;
  175. case SB_PAGELEFT: a-=10;break;
  176. case SB_PAGERIGHT: a+=10;break;
  177. case SB_THUMBTRACK: a=nPos;break;
  178. }
  179. if (a<-180)a=180;
  180. if (a>180)a=-180;
  181. pScrollBar->SetScrollPos(a);
  182. break;
  183. case IDC_SCROLLBAR2: b=pScrollBar->GetScrollPos();
  184. switch (nSBCode)
  185. {
  186. case SB_LINELEFT: b--;break;
  187. case SB_LINERIGHT: b++;break;
  188. case SB_PAGELEFT: b-=10;break;
  189. case SB_PAGERIGHT: b+=10;break;
  190. case SB_THUMBTRACK: b=nPos;break;
  191. }
  192. if (b<-180)b=180;
  193. if (b>180)b=-180;
  194. pScrollBar->SetScrollPos(b);
  195. break;
  196. case IDC_SCROLLBAR3: c=pScrollBar->GetScrollPos();
  197. switch (nSBCode)
  198. {
  199. case SB_LINELEFT: c--;break;
  200. case SB_LINERIGHT: c++;break;
  201. case SB_PAGELEFT: c-=10;break;
  202. case SB_PAGERIGHT: c+=10;break;
  203. case SB_THUMBTRACK: c=nPos;break;
  204. }
  205. if (c<-180)c=180;
  206. if (c>180)c=-180;
  207. pScrollBar->SetScrollPos(c);
  208.  break;
  209. }
  210. Draw();
  211. CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  212. }
  213. BOOL CDrawDlg::OnInitDialog() 
  214. {
  215. CDialog::OnInitDialog();
  216. m_scroll1.SetScrollRange(-180,180);
  217. m_scroll1.SetScrollPos(0);
  218. m_scroll2.SetScrollRange(-180,180);
  219. m_scroll2.SetScrollPos(0);
  220. m_scroll3.SetScrollRange(-180,180);
  221. m_scroll3.SetScrollPos(0);
  222. a=b=c=0;
  223. SetTimer(1,100,NULL);
  224. Ctrl=0;
  225. return TRUE;  // return TRUE unless you set the focus to a control
  226.               // EXCEPTION: OCX Property Pages should return FALSE
  227. }
  228. void CDrawDlg::OnPaint() 
  229. {
  230. CPaintDC dc(this); // device context for painting
  231. CWnd*pWnd=GetDlgItem(IDC_DRAW);
  232. pWnd->UpdateWindow();
  233. Draw();
  234. // TODO: Add your message handler code here
  235. // Do not call CDialog::OnPaint() for painting messages
  236. }
  237. void CDrawDlg::OnTimer(UINT nIDEvent) 
  238. {
  239. // TODO: Add your message handler code here and/or call default
  240. if (1==Ctrl)
  241. {
  242. CScrollBar *pscrolla=(CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);
  243.         a--;
  244. if (a<-180)a=180;
  245. pscrolla->SetScrollPos(a);
  246. Draw();
  247. }
  248. if (2==Ctrl)
  249. {
  250. CScrollBar *pscrollb=(CScrollBar *)GetDlgItem(IDC_SCROLLBAR2);
  251. b--;
  252. if (b<-180)b=180;
  253. pscrollb->SetScrollPos(b);
  254. // Invalidate();
  255. Draw();
  256. }
  257. if (3==Ctrl)
  258. {
  259. CScrollBar *pscrollc=(CScrollBar *)GetDlgItem(IDC_SCROLLBAR3);
  260. c--;
  261. if (c<-180)c=180;
  262. pscrollc->SetScrollPos(c);
  263. // Invalidate();
  264. Draw();
  265. }
  266. CDialog::OnTimer(nIDEvent);
  267. }
  268. void CDrawDlg::OnButtonRotation4() 
  269. {
  270. // TODO: Add your control notification handler code here
  271. KillTimer(1);
  272. }