Edit.cpp
上传用户:shilei2004
上传日期:2020-07-18
资源大小:83k
文件大小:12k
源码类别:

RichEdit

开发平台:

Visual C++

  1. // Edit.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "Edit.h"
  5. #include "KSFileDialog.h"
  6. #include "MainFrm.h"
  7. #include "ChildFrm.h"
  8. #include "EditDoc.h"
  9. #include "EditView.h"
  10. #include "StatLink.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CEditApp
  18. BEGIN_MESSAGE_MAP(CEditApp, CWinApp)
  19. //{{AFX_MSG_MAP(CEditApp)
  20. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  21. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  22. ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateFileMruFile1)
  23. ON_COMMAND(ID_FILE_SAVE_ALL, OnFileSaveAll)
  24. //}}AFX_MSG_MAP
  25. // Standard file based document commands
  26. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  27. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  28. // Standard print setup command
  29. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CEditApp construction
  33. CEditApp::CEditApp()
  34. {
  35. // TODO: add construction code here,
  36. // Place all significant initialization in InitInstance
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // The one and only CEditApp object
  40. CEditApp theApp;
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CEditApp initialization
  43. BOOL CEditApp::InitInstance()
  44. {
  45. // Initialize OLE libraries
  46. if (!AfxOleInit())
  47. {
  48. AfxMessageBox(IDP_OLE_INIT_FAILED);
  49. return FALSE;
  50. }
  51. AfxEnableControlContainer();
  52. // Standard initialization
  53. // If you are not using these features and wish to reduce the size
  54. //  of your final executable, you should remove from the following
  55. //  the specific initialization routines you do not need.
  56. #ifdef _AFXDLL
  57. Enable3dControls(); // Call this when using MFC in a shared DLL
  58. #else
  59. Enable3dControlsStatic(); // Call this when linking to MFC statically
  60. #endif
  61. // Change the registry key under which our settings are stored.
  62. // TODO: You should modify this string to be something appropriate
  63. // such as the name of your company or organization.
  64. SetRegistryKey(_T("Sameer Edit"));
  65. LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  66. // Register the application's document templates.  Document templates
  67. //  serve as the connection between documents, frame windows and views.
  68. CMainFrame* pMainFrame = new CMainFrame;
  69. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  70. return FALSE;
  71. m_pMainWnd = pMainFrame;
  72. CMultiDocTemplate* pDocTemplate;
  73. pDocTemplate = new CMultiDocTemplate(
  74. IDR_EDITTYPE,
  75. RUNTIME_CLASS(CEditDoc),
  76. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  77. RUNTIME_CLASS(CMyEditView));
  78. pDocTemplate->SetContainerInfo(IDR_EDITTYPE_CNTR_IP);
  79. AddDocTemplate(pDocTemplate);
  80. // create main MDI Frame window
  81. // Enable drag/drop open
  82. m_pMainWnd->DragAcceptFiles();
  83. // Enable DDE Execute open
  84. EnableShellOpen();
  85. RegisterShellFileTypes(TRUE);
  86. // Parse command line for standard shell commands, DDE, file open
  87. CCommandLineInfo cmdInfo;
  88. ParseCommandLine(cmdInfo);
  89. // Dispatch commands specified on the command line
  90. if (!ProcessShellCommand(cmdInfo))
  91. return FALSE;
  92. if (!cmdInfo.m_bRunEmbedded)
  93. {
  94. m_pMainWnd->UpdateWindow();
  95. if (!m_pMainWnd->IsIconic()  &&
  96. m_splash.Create(m_pMainWnd))
  97. {
  98. m_splash.ShowWindow(SW_SHOW);
  99. m_splash.UpdateWindow();
  100. m_splash.SetTimer(1, 500, NULL);
  101. Sleep(500);
  102. m_splash.DestroyWindow();
  103. }
  104. }
  105. // The main window has been initialized, so show and update it.
  106. ReadConfig(); 
  107. m_pMainWnd->SetWindowPlacement(&m_WP);
  108. pMainFrame->ShowWindow(m_nCmdShow);
  109. pMainFrame->UpdateWindow();
  110. return TRUE;
  111. }
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CAboutDlg dialog used for App About
  114. class CAboutDlg : public CDialog
  115. {
  116. protected:
  117. CStaticLink m_wndLink[3];  // static controls with hyperlinks
  118. public:
  119. CAboutDlg() : CDialog(IDD_ABOUTBOX) { }
  120. virtual BOOL OnInitDialog();
  121. };
  122. BOOL CAboutDlg::OnInitDialog() 
  123. {
  124. // TODO: Add extra initialization here
  125. m_wndLink[1].SubclassDlgItem(IDC_EMAIL, this,
  126. _T("mailto:sabbasi00@hotmail.com"));
  127. m_wndLink[2].SubclassDlgItem(IDC_STATIC3, this,
  128. _T("http://www.CodeGuru.com"));
  129. return CDialog::OnInitDialog();
  130. }
  131. // App command to run the dialog
  132. void CEditApp::OnAppAbout()
  133. {
  134. CAboutDlg aboutDlg;
  135. aboutDlg.DoModal();
  136. }
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CSplashWnd dialog
  139. BEGIN_MESSAGE_MAP(CSplashWnd, CDialog)
  140. //{{AFX_MSG_MAP(CSplashWnd)
  141. //}}AFX_MSG_MAP
  142. END_MESSAGE_MAP()
  143. void CSplashWnd::DoDataExchange(CDataExchange* pDX)
  144. {
  145. CDialog::DoDataExchange(pDX);
  146. //{{AFX_DATA_MAP(CSplashWnd)
  147. // NOTE: the ClassWizard will add DDX and DDV calls here
  148. //}}AFX_DATA_MAP
  149. }
  150. BOOL CSplashWnd::Create(CWnd* pParent)
  151. {
  152. //{{AFX_DATA_INIT(CSplashWnd)
  153. // NOTE: the ClassWizard will add member initialization here
  154. //}}AFX_DATA_INIT
  155. if (!CDialog::Create(CSplashWnd::IDD, pParent))
  156. {
  157. TRACE0("Warning: creation of CSplashWnd dialog failedn");
  158. return FALSE;
  159. }
  160. return TRUE;
  161. }
  162. BOOL CSplashWnd::OnInitDialog()
  163. {
  164. CDialog::OnInitDialog();
  165. CenterWindow();
  166. // initialize the big icon control
  167. m_icon.SubclassDlgItem(IDC_BIGICON, this);
  168. m_icon.SizeToContent();
  169. return TRUE;  // return TRUE  unless you set the focus to a control
  170. }
  171. /////////////////////////////////////////////////////////////////////////////
  172. // CBigIcon
  173. BEGIN_MESSAGE_MAP(CBigIcon, CButton)
  174. //{{AFX_MSG_MAP(CBigIcon)
  175. ON_WM_DRAWITEM()
  176. ON_WM_ERASEBKGND()
  177. //}}AFX_MSG_MAP
  178. END_MESSAGE_MAP()
  179. /////////////////////////////////////////////////////////////////////////////
  180. // CBigIcon message handlers
  181. #define CY_SHADOW   4
  182. #define CX_SHADOW   4
  183. void CBigIcon::SizeToContent()
  184. {
  185. int cxIcon = ::GetSystemMetrics(SM_CXICON);
  186. int cyIcon = ::GetSystemMetrics(SM_CYICON);
  187. SetWindowPos(NULL, 0, 0, cxIcon*2 + CX_SHADOW + 4, cyIcon*2 + CY_SHADOW + 4,
  188. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
  189. }
  190. void CBigIcon::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  191. {
  192. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  193. ASSERT(pDC != NULL);
  194. CRect rect;
  195. GetClientRect(rect);
  196. int cxClient = rect.Width();
  197. int cyClient = rect.Height();
  198. HICON hicon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  199. if (hicon == NULL)
  200. return;
  201. int cxIcon = ::GetSystemMetrics(SM_CXICON);
  202. int cyIcon = ::GetSystemMetrics(SM_CYICON);
  203. CBitmap bitmap;
  204. if (!bitmap.CreateCompatibleBitmap(pDC, cxIcon, cyIcon))
  205. return;
  206. CDC dcMem;
  207. if (!dcMem.CreateCompatibleDC(pDC))
  208. return;
  209. CBitmap* pBitmapOld = dcMem.SelectObject(&bitmap);
  210. if (pBitmapOld == NULL)
  211. return;
  212. dcMem.StretchBlt(0, 0, cxIcon, cyIcon, pDC,
  213. 2, 2, cxClient-CX_SHADOW-4, cyClient-CY_SHADOW-4, SRCCOPY);
  214. dcMem.DrawIcon(0, 0, hicon);
  215. CPen pen;
  216. pen.CreateStockObject(BLACK_PEN);
  217. CPen* pPenOld = pDC->SelectObject(&pen);
  218. pDC->Rectangle(0, 0, cxClient-CX_SHADOW, cyClient-CY_SHADOW);
  219. if (pPenOld)
  220. pDC->SelectObject(pPenOld);
  221. CBrush br;
  222. br.CreateStockObject(DKGRAY_BRUSH);
  223. rect.SetRect(cxClient-CX_SHADOW, CY_SHADOW, cxClient, cyClient);
  224. pDC->FillRect(rect, &br);
  225. rect.SetRect(CX_SHADOW, cyClient-CY_SHADOW, cxClient, cyClient);
  226. pDC->FillRect(rect, &br);
  227. pDC->StretchBlt(2, 2, cxClient-CX_SHADOW-4, cyClient-CY_SHADOW-4,
  228. &dcMem, 0, 0, cxIcon, cyIcon, SRCCOPY);
  229. }
  230. BOOL CBigIcon::OnEraseBkgnd(CDC*)
  231. {
  232. return TRUE;
  233. }
  234. /////////////////////////////////////////////////////////////////////////////
  235. BOOL CEditApp::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags,
  236.       BOOL bOpenFileDialog, CDocTemplate* pTemplate)
  237. {
  238. CKSFileDialog dlgFile(bOpenFileDialog);
  239. dlgFile.SetAppPointer(this);
  240. dlgFile.SetStringFilter("Edit Files(*.edt)|*.edt|Rich Text Format(*.rtf)|*.rtf|Text File(*.txt)|*.txt|All Files(*.*)|*.*");
  241. dlgFile.SetMyParent(AfxGetMainWnd());
  242. return dlgFile.DoPromptFileName(fileName,nIDSTitle,lFlags,bOpenFileDialog, pTemplate);
  243. }
  244. /////////////////////////////////////////////////////////////////////////////
  245. // CEditApp message handlers
  246. void CEditApp::OnFileOpen() 
  247. {
  248. // TODO: Add your command handler code here
  249. CString newName;
  250. if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
  251.    OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, TRUE, NULL))
  252. return; // open cancelled
  253. OpenDocumentFile(newName); // TODO: Add your command handler code here
  254. }
  255. void CEditApp::OnUpdateFileMruFile1(CCmdUI* pCmdUI) 
  256. {
  257. // TODO: Add your command update UI handler code here
  258. if (pCmdUI->m_pSubMenu!=NULL)
  259. {
  260. return;
  261. }
  262. CWinApp::OnUpdateRecentFileMenu(pCmdUI);
  263. return;
  264. }
  265. void CEditApp::OnFileSaveAll() 
  266. {
  267. // TODO: Add your command handler code here
  268.   POSITION pos = GetFirstDocTemplatePosition ();
  269.   ASSERT (pos);
  270.   CDocTemplate *pDocTempl = GetNextDocTemplate (pos);
  271.   pos = pDocTempl->GetFirstDocPosition ();
  272.   while (pos)
  273.     {
  274.       CEditDoc *pDoc = (CEditDoc*) pDocTempl->GetNextDoc (pos);
  275.       ASSERT (pDoc);
  276.       CString sDocPath = pDoc->GetPathName ();
  277.       if (pDoc->IsModified ())
  278.         pDoc->DoSave (sDocPath);
  279.     }
  280. }
  281. int CEditApp::ExitInstance() 
  282. {
  283. WriteConfig();
  284. // any other code you need before exit
  285. return CWinApp::ExitInstance();
  286. }
  287. const WINDOWPLACEMENT& CEditApp::GetWP(void) const
  288. {
  289. return m_WP;
  290. }
  291. void CEditApp::PutWP(const WINDOWPLACEMENT& newval)
  292. {
  293. m_WP=newval;
  294. m_WP.length=sizeof(m_WP);
  295. }
  296. void CEditApp::ReadConfig(void)
  297. {
  298. ReadWindowConfig();
  299. }
  300. void CEditApp::WriteConfig(void)
  301. {
  302. WriteWindowConfig();
  303. }
  304. void CEditApp::ReadWindowConfig(void)
  305. {
  306. WINDOWPLACEMENT wp;
  307. wp.length=sizeof(wp);
  308. m_pMainWnd->GetWindowPlacement(&wp);
  309. m_WP.length=sizeof(m_WP);
  310. m_WP.showCmd=GetProfileInt("Window","show",wp.showCmd);
  311. m_WP.flags=GetProfileInt("Window","flags",wp.flags);
  312. m_WP.ptMinPosition.x=GetProfileInt("Window","minposx",
  313. wp.ptMinPosition.x);
  314. m_WP.ptMinPosition.y=GetProfileInt("Window","minposy",
  315. wp.ptMinPosition.y);
  316. m_WP.ptMaxPosition.x=GetProfileInt("Window","maxposx",
  317. wp.ptMaxPosition.x);
  318. m_WP.ptMaxPosition.y=GetProfileInt("Window","maxposy",
  319. wp.ptMaxPosition.y);
  320. m_WP.rcNormalPosition.left=GetProfileInt("Window","left",
  321. wp.rcNormalPosition.left);
  322. m_WP.rcNormalPosition.top=GetProfileInt("Window","top",
  323. wp.rcNormalPosition.top);
  324. m_WP.rcNormalPosition.right=GetProfileInt("Window","right",
  325. wp.rcNormalPosition.right);
  326. m_WP.rcNormalPosition.bottom=GetProfileInt("Window","bottom",
  327. wp.rcNormalPosition.bottom);
  328. CRect rcTemp;
  329. CRect rcScreen(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
  330. ::SystemParametersInfo(SPI_GETWORKAREA,0,&rcScreen,0);
  331. rcTemp=m_WP.rcNormalPosition;
  332. rcTemp.NormalizeRect();
  333. m_WP.rcNormalPosition=rcTemp;
  334. if(::IntersectRect(&m_WP.rcNormalPosition, &rcTemp, &rcScreen))
  335. {
  336. if(rcTemp!=m_WP.rcNormalPosition)
  337. { // some part of the window is off the screen
  338. if(rcTemp.left<rcScreen.left)  // off the left edge
  339. rcTemp.OffsetRect(rcScreen.left-rcTemp.left,0);
  340. if(rcTemp.right>rcScreen.right)  // off the right edge
  341. rcTemp.OffsetRect(rcScreen.right-rcTemp.right,0);
  342. if(rcTemp.left<rcScreen.left)  // won't fit, shrink
  343. rcTemp.left=rcScreen.left;
  344. if(rcTemp.top<rcScreen.top)  // off the top edge
  345. rcTemp.OffsetRect(0,rcScreen.top-rcTemp.top);
  346. if(rcTemp.bottom>rcScreen.bottom)  // off the bottom edge
  347. rcTemp.OffsetRect(0,rcScreen.bottom-rcTemp.bottom);
  348. if(rcTemp.top<rcScreen.top)  // won't fit, shrink
  349. rcTemp.top=rcScreen.top;
  350. m_WP.rcNormalPosition=rcTemp;
  351. }
  352. }
  353. else
  354. {
  355. m_WP=wp;
  356. }
  357. if(m_WP.showCmd==SW_MINIMIZE && wp.showCmd!=SW_MINIMIZE)
  358. {
  359. m_WP.showCmd=SW_RESTORE;
  360. }
  361. if(m_WP.showCmd!=SW_MAXIMIZE && wp.showCmd==SW_MAXIMIZE)
  362. {
  363. m_WP.showCmd=SW_MAXIMIZE;
  364. }
  365. }
  366. void CEditApp::WriteWindowConfig(void)
  367. {
  368. WriteProfileInt("Window","show",m_WP.showCmd);
  369. WriteProfileInt("Window","flags",m_WP.flags);
  370. WriteProfileInt("Window","minposx",m_WP.ptMinPosition.x);
  371. WriteProfileInt("Window","minposy",m_WP.ptMinPosition.y);
  372. WriteProfileInt("Window","maxposx",m_WP.ptMaxPosition.x);
  373. WriteProfileInt("Window","maxposy",m_WP.ptMaxPosition.y);
  374. WriteProfileInt("Window","left",m_WP.rcNormalPosition.left);
  375. WriteProfileInt("Window","top",m_WP.rcNormalPosition.top);
  376. WriteProfileInt("Window","right",m_WP.rcNormalPosition.right);
  377. WriteProfileInt("Window","bottom",m_WP.rcNormalPosition.bottom);
  378. }