DRAW.CPP
上传用户:jinlangri
上传日期:2022-07-17
资源大小:10774k
文件大小:8k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // draw.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "draw.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "drawDoc.h"
  8. #include "drawView.h"
  9. #include "PenWidth.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CDrawApp
  17. BEGIN_MESSAGE_MAP(CDrawApp, CWinApp)
  18. //{{AFX_MSG_MAP(CDrawApp)
  19. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  20. ON_COMMAND(ID_DRAW_FILLCOLOR, OnDrawFillcolor)
  21. ON_COMMAND(ID_DRAW_SELECT, OnDrawSelect)
  22. ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  23. ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
  24. ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
  25. ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
  26. ON_COMMAND(ID_DRAW_RECTFILL, OnDrawRectfill)
  27. ON_UPDATE_COMMAND_UI(ID_DRAW_RECTFILL, OnUpdateDrawRectfill)
  28. ON_COMMAND(ID_DRAW_RECTROUND, OnDrawRectround)
  29. ON_UPDATE_COMMAND_UI(ID_DRAW_RECTROUND, OnUpdateDrawRectround)
  30. ON_COMMAND(ID_DRAW_RECTROUNDFILL, OnDrawRectroundfill)
  31. ON_UPDATE_COMMAND_UI(ID_DRAW_RECTROUNDFILL, OnUpdateDrawRectroundfill)
  32. ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
  33. ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
  34. ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
  35. ON_UPDATE_COMMAND_UI(ID_DRAW_CIRCLE, OnUpdateDrawCircle)
  36. ON_COMMAND(ID_DRAW_CIRCLEFILL, OnDrawCirclefill)
  37. ON_UPDATE_COMMAND_UI(ID_DRAW_CIRCLEFILL, OnUpdateDrawCirclefill)
  38. ON_COMMAND(ID_DRAW_ELLIPSEFILL, OnDrawEllipsefill)
  39. ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSEFILL, OnUpdateDrawEllipsefill)
  40. ON_COMMAND(ID_DRAW_WORD, OnDrawWord)
  41. ON_UPDATE_COMMAND_UI(ID_DRAW_WORD, OnUpdateDrawWord)
  42. ON_COMMAND(ID_DRAW_PENCOLOR, OnDrawPencolor)
  43. ON_COMMAND(ID_DRAW_PEN_WIDTH, OnDrawPenWidth)
  44. ON_UPDATE_COMMAND_UI(ID_DRAW_SELECT, OnUpdateDrawSelect)
  45. //}}AFX_MSG_MAP
  46. // Standard file based document commands
  47. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  48. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  49. // Standard print setup command
  50. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CDrawApp construction
  54. CDrawApp::CDrawApp()
  55. {
  56. m_FillColor = RGB(0, 0, 0);
  57.     m_PenColor = RGB(0, 0, 0);
  58. m_CurrentPenThickness = 1;
  59. m_CurrentLineStyle = PS_DOT;
  60.     m_CurrentTool = ID_DRAW_SELECT;
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. // The one and only CDrawApp object
  64. CDrawApp theApp;
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CDrawApp initialization
  67. BOOL CDrawApp::InitInstance()
  68. {
  69. AfxEnableControlContainer();
  70. // Standard initialization
  71. // If you are not using these features and wish to reduce the size
  72. //  of your final executable, you should remove from the following
  73. //  the specific initialization routines you do not need.
  74. #ifdef _AFXDLL
  75. Enable3dControls(); // Call this when using MFC in a shared DLL
  76. #else
  77. Enable3dControlsStatic(); // Call this when linking to MFC statically
  78. #endif
  79. // Change the registry key under which our settings are stored.
  80. // TODO: You should modify this string to be something appropriate
  81. // such as the name of your company or organization.
  82. SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  83. LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  84. // Register the application's document templates.  Document templates
  85. //  serve as the connection between documents, frame windows and views.
  86. CMultiDocTemplate* pDocTemplate;
  87. pDocTemplate = new CMultiDocTemplate(
  88. IDR_DRAWTYPE,
  89. RUNTIME_CLASS(CDrawDoc),
  90. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  91. RUNTIME_CLASS(CDrawView));
  92. AddDocTemplate(pDocTemplate);
  93. // create main MDI Frame window
  94. CMainFrame* pMainFrame = new CMainFrame;
  95. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  96. return FALSE;
  97. m_pMainWnd = pMainFrame;
  98. // Parse command line for standard shell commands, DDE, file open
  99. CCommandLineInfo cmdInfo;
  100. ParseCommandLine(cmdInfo);
  101. // Dispatch commands specified on the command line
  102. if (!ProcessShellCommand(cmdInfo))
  103. return FALSE;
  104. // The main window has been initialized, so show and update it.
  105. pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
  106. pMainFrame->UpdateWindow();
  107. return TRUE;
  108. }
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CAboutDlg dialog used for App About
  111. class CAboutDlg : public CDialog
  112. {
  113. public:
  114. CAboutDlg();
  115. // Dialog Data
  116. //{{AFX_DATA(CAboutDlg)
  117. enum { IDD = IDD_ABOUTBOX };
  118. //}}AFX_DATA
  119. // ClassWizard generated virtual function overrides
  120. //{{AFX_VIRTUAL(CAboutDlg)
  121. protected:
  122. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  123. //}}AFX_VIRTUAL
  124. // Implementation
  125. protected:
  126. //{{AFX_MSG(CAboutDlg)
  127. // No message handlers
  128. //}}AFX_MSG
  129. DECLARE_MESSAGE_MAP()
  130. };
  131. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  132. {
  133. //{{AFX_DATA_INIT(CAboutDlg)
  134. //}}AFX_DATA_INIT
  135. }
  136. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  137. {
  138. CDialog::DoDataExchange(pDX);
  139. //{{AFX_DATA_MAP(CAboutDlg)
  140. //}}AFX_DATA_MAP
  141. }
  142. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  143. //{{AFX_MSG_MAP(CAboutDlg)
  144. // No message handlers
  145. //}}AFX_MSG_MAP
  146. END_MESSAGE_MAP()
  147. // App command to run the dialog
  148. void CDrawApp::OnAppAbout()
  149. {
  150. CAboutDlg aboutDlg;
  151. aboutDlg.DoModal();
  152. }
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CDrawApp message handlers
  155. void CDrawApp::OnDrawFillcolor() 
  156. {
  157. CColorDialog ColorDialog;
  158. if(ColorDialog.DoModal() == IDOK)
  159. m_FillColor = ColorDialog.GetColor();
  160. }
  161. void CDrawApp::OnDrawSelect() 
  162. {
  163. m_CurrentTool = ID_DRAW_SELECT;
  164. }
  165. void CDrawApp::OnUpdateDrawSelect(CCmdUI* pCmdUI) 
  166. {
  167. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_SELECT ? 1:0);
  168. }
  169. void CDrawApp::OnDrawLine() 
  170. {
  171. m_CurrentTool = ID_DRAW_LINE;
  172. }
  173. void CDrawApp::OnUpdateDrawLine(CCmdUI* pCmdUI) 
  174. {
  175. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_LINE ? 1:0);
  176. }
  177. void CDrawApp::OnDrawRect() 
  178. {
  179. m_CurrentTool = ID_DRAW_RECT;
  180. }
  181. void CDrawApp::OnUpdateDrawRect(CCmdUI* pCmdUI) 
  182. {
  183. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_RECT ? 1:0);
  184. }
  185. void CDrawApp::OnDrawRectfill() 
  186. {
  187. m_CurrentTool = ID_DRAW_RECTFILL;
  188. }
  189. void CDrawApp::OnUpdateDrawRectfill(CCmdUI* pCmdUI) 
  190. {
  191. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_RECTFILL ? 1:0);
  192. }
  193. void CDrawApp::OnDrawRectround() 
  194. {
  195. m_CurrentTool = ID_DRAW_RECTROUND;
  196. }
  197. void CDrawApp::OnUpdateDrawRectround(CCmdUI* pCmdUI) 
  198. {
  199. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_RECTROUND ? 1:0);
  200. }
  201. void CDrawApp::OnDrawRectroundfill() 
  202. {
  203. m_CurrentTool = ID_DRAW_RECTROUNDFILL;
  204. }
  205. void CDrawApp::OnUpdateDrawRectroundfill(CCmdUI* pCmdUI) 
  206. {
  207. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_RECTROUNDFILL ? 1:0);
  208. }
  209. void CDrawApp::OnDrawEllipse() 
  210. {
  211. m_CurrentTool = ID_DRAW_ELLIPSE;
  212. }
  213. void CDrawApp::OnUpdateDrawEllipse(CCmdUI* pCmdUI) 
  214. {
  215. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_ELLIPSE ? 1:0);
  216. }
  217. void CDrawApp::OnDrawCircle() 
  218. {
  219. m_CurrentTool = ID_DRAW_CIRCLE;
  220. }
  221. void CDrawApp::OnUpdateDrawCircle(CCmdUI* pCmdUI) 
  222. {
  223. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_CIRCLE ? 1:0);
  224. }
  225. void CDrawApp::OnDrawCirclefill() 
  226. {
  227. m_CurrentTool = ID_DRAW_CIRCLEFILL;
  228. }
  229. void CDrawApp::OnUpdateDrawCirclefill(CCmdUI* pCmdUI) 
  230. {
  231. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_CIRCLEFILL ? 1:0);
  232. }
  233. void CDrawApp::OnDrawEllipsefill() 
  234. {
  235. m_CurrentTool = ID_DRAW_ELLIPSEFILL;
  236. }
  237. void CDrawApp::OnUpdateDrawEllipsefill(CCmdUI* pCmdUI) 
  238. {
  239. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_ELLIPSEFILL ? 1:0);
  240. }
  241. void CDrawApp::OnDrawWord() 
  242. {
  243. m_CurrentTool = ID_DRAW_WORD;
  244. }
  245. void CDrawApp::OnUpdateDrawWord(CCmdUI* pCmdUI) 
  246. {
  247. pCmdUI->SetCheck(m_CurrentTool == ID_DRAW_WORD ? 1:0);
  248. }
  249. void CDrawApp::OnDrawPencolor() 
  250. {
  251. CColorDialog ColorDialog;
  252. if(ColorDialog.DoModal() == IDOK)
  253. m_PenColor = ColorDialog.GetColor();
  254. }
  255. void CDrawApp::OnDrawPenWidth() 
  256. {
  257. CPenWidth PenWidthDlg;
  258. PenWidthDlg.m_Width = m_CurrentPenThickness;
  259. if(PenWidthDlg.DoModal() == IDOK)
  260. m_CurrentPenThickness = PenWidthDlg.m_Width;
  261. }