3DHelperDemoSimpleDlg.cpp
上传用户:qzpk666
上传日期:2022-08-04
资源大小:59k
文件大小:5k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // 3DHelperDemoSimpleDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3DHelperDemoSimple.h"
  5. #include "3DHelperDemoSimpleDlg.h"
  6. #include "MemDC.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CMy3DHelperDemoSimpleDlg dialog
  51. CMy3DHelperDemoSimpleDlg::CMy3DHelperDemoSimpleDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CMy3DHelperDemoSimpleDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CMy3DHelperDemoSimpleDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CMy3DHelperDemoSimpleDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CMy3DHelperDemoSimpleDlg)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CMy3DHelperDemoSimpleDlg, CDialog)
  68. //{{AFX_MSG_MAP(CMy3DHelperDemoSimpleDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_WM_TIMER()
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CMy3DHelperDemoSimpleDlg message handlers
  77. BOOL CMy3DHelperDemoSimpleDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  81. ASSERT(IDM_ABOUTBOX < 0xF000);
  82. CMenu* pSysMenu = GetSystemMenu(FALSE);
  83. if (pSysMenu != NULL)
  84. {
  85. CString strAboutMenu;
  86. strAboutMenu.LoadString(IDS_ABOUTBOX);
  87. if (!strAboutMenu.IsEmpty())
  88. {
  89. pSysMenu->AppendMenu(MF_SEPARATOR);
  90. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  91. }
  92. }
  93. SetIcon(m_hIcon, TRUE);
  94. SetIcon(m_hIcon, FALSE);
  95. // Initialize the 3D system
  96. initialize();
  97. return TRUE;
  98. }
  99. void CMy3DHelperDemoSimpleDlg::OnSysCommand(UINT nID, LPARAM lParam)
  100. {
  101. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  102. {
  103. CAboutDlg dlgAbout;
  104. dlgAbout.DoModal();
  105. }
  106. else
  107. {
  108. CDialog::OnSysCommand(nID, lParam);
  109. }
  110. }
  111. void CMy3DHelperDemoSimpleDlg::OnPaint() 
  112. {
  113. if (IsIconic())
  114. {
  115. CPaintDC dc(this); // device context for painting
  116. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  117. // Center icon in client rectangle
  118. int cxIcon = GetSystemMetrics(SM_CXICON);
  119. int cyIcon = GetSystemMetrics(SM_CYICON);
  120. CRect rect;
  121. GetClientRect(&rect);
  122. int x = (rect.Width() - cxIcon + 1) / 2;
  123. int y = (rect.Height() - cyIcon + 1) / 2;
  124. dc.DrawIcon(x, y, m_hIcon);
  125. }
  126. else
  127. {
  128. CDialog::OnPaint();
  129. }
  130. }
  131. HCURSOR CMy3DHelperDemoSimpleDlg::OnQueryDragIcon()
  132. {
  133. return (HCURSOR) m_hIcon;
  134. }
  135. void CMy3DHelperDemoSimpleDlg::OnTimer(UINT nIDEvent) 
  136. {
  137. // Timer is used to refresh trackball information
  138. if(nIDEvent == 1) {
  139. KillTimer(nIDEvent);
  140. m_c3DHelper.UpdateTrackBall(GetDlgItem(IDC_STATIC_PLACEHOLDER)->GetDC());
  141. redraw();
  142. SetTimer(1, 25, NULL);
  143. }
  144. CDialog::OnTimer(nIDEvent);
  145. }
  146. void CMy3DHelperDemoSimpleDlg::redraw()
  147. {
  148. // Create off-screen dc (prevents flickering)
  149. CRect rcDraw;
  150. CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
  151. pTarget->GetClientRect(rcDraw);
  152. // Create the offscreen dc
  153. CDC *pTDC = pTarget->GetDC();
  154. CMemDC cDC(pTDC, &rcDraw);
  155. CDC *pDC = (CDC*)cDC;
  156. // Clean up the screen an draw a hint
  157. pDC->FillSolidRect(rcDraw.left, rcDraw.top, rcDraw.Width(),rcDraw.Height(), RGB(0,0,0));
  158. pDC->SetTextColor(RGB(255,255,0));
  159. pDC->SetBkMode(OPAQUE);
  160. pDC->TextOut(1,1, _T("Drag mouse to rotate..."));
  161. // Calculate screen coordinates of point <1,1,1>
  162. NeHe::Vector cIn (1.0f, 1.0f, 1.0f);
  163. NeHe::Vector cOut = m_c3DHelper.RenderPoint(cIn);
  164. // And display this information
  165. pDC->SetTextColor(RGB(255,255,255));
  166. pDC->SetBkMode(OPAQUE); 
  167. pDC->TextOut((int)cOut.x, (int)cOut.y, _T("* <1, 1, 1>"));
  168. // Ease relative position recognition
  169. m_c3DHelper.DrawVisibilityCube(pDC);
  170. // cDC will be automatically destruced 
  171. }
  172. // Set up initial 3D system
  173. void CMy3DHelperDemoSimpleDlg::initialize()
  174. {
  175. CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
  176. m_c3DHelper.Initialize(pTarget->GetDC(), NeHe::Vector(-2.0f, -2.0f, -2.0f), NeHe::Vector(2.0f,2.0f,2.0f), TRUE);
  177. // Timer used to refresh "trackball" information
  178. SetTimer(1, 25, NULL);
  179. }