TabEdit.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:5k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // TabEdit.cpp : implementation file
  2. //
  3. #include "StdAfx.h"
  4. #include "Resource.h"
  5. #include "TabEdit.h"
  6. #include <io.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CTabEdit
  14. CTabEdit::CTabEdit()
  15. {
  16. }
  17. CTabEdit::~CTabEdit()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CTabEdit, CEdit)
  21. //{{AFX_MSG_MAP(CTabEdit)
  22. ON_WM_CREATE()
  23. ON_WM_CONTEXTMENU()
  24. ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  25. ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
  26. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  27. ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
  28. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  29. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  30. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  31. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  32. ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  33. ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditClear)
  34. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  35. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  36. ON_WM_ERASEBKGND()
  37. ON_WM_PAINT()
  38. ON_WM_DROPFILES()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CTabEdit message handlers
  43. BOOL CTabEdit::PreCreateWindow(CREATESTRUCT& cs) 
  44. {
  45. if (!CEdit::PreCreateWindow(cs))
  46. return FALSE;
  47. cs.style |= ES_MULTILINE|WS_HSCROLL|WS_VSCROLL;
  48. return TRUE;
  49. }
  50. int CTabEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  51. {
  52. if (CEdit::OnCreate(lpCreateStruct) == -1)
  53. return -1;
  54. // Register as a drop target
  55. m_dropTarget.Register(this);
  56. DragAcceptFiles();
  57. CreateFont();
  58. return 0;
  59. }
  60. void CTabEdit::CreateFont()
  61. {
  62. LOGFONT lf;
  63. XTAuxData().font.GetLogFont(&lf);
  64. STRCPY_S(lf.lfFaceName, LF_FACESIZE, _T("Courier New"));
  65. m_font.CreateFontIndirect(&lf);
  66. SetFont(&m_font);
  67. }
  68. void CTabEdit::OnContextMenu(CWnd*, CPoint point)
  69. {
  70. if (point.x == -1 && point.y == -1)
  71. {
  72. //keystroke invocation
  73. CRect rect;
  74. GetClientRect(rect);
  75. ClientToScreen(rect);
  76. point = rect.TopLeft();
  77. point.Offset(5, 5);
  78. }
  79. CMenu menu;
  80. VERIFY(menu.LoadMenu(IDR_POPUP_EDIT));
  81. CMenu* pPopup = menu.GetSubMenu(0);
  82. ASSERT(pPopup != NULL);
  83. CWnd* pWndPopupOwner = this;
  84. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  85. pWndPopupOwner = pWndPopupOwner->GetParent();
  86. pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  87. pWndPopupOwner);
  88. }
  89. void CTabEdit::OnEditUndo() 
  90. {
  91. Undo();
  92. }
  93. void CTabEdit::OnUpdateEditUndo(CCmdUI* pCmdUI) 
  94. {
  95. pCmdUI->Enable(CanUndo());
  96. }
  97. void CTabEdit::OnEditCut() 
  98. {
  99. Cut();
  100. }
  101. void CTabEdit::OnUpdateEditCut(CCmdUI* pCmdUI) 
  102. {
  103. pCmdUI->Enable(SelectionMade());
  104. }
  105. void CTabEdit::OnEditCopy() 
  106. {
  107. Copy();
  108. }
  109. void CTabEdit::OnUpdateEditCopy(CCmdUI* pCmdUI) 
  110. {
  111. pCmdUI->Enable(SelectionMade());
  112. }
  113. void CTabEdit::OnEditPaste() 
  114. {
  115. Paste();
  116. }
  117. void CTabEdit::OnUpdateEditPaste(CCmdUI* pCmdUI) 
  118. {
  119. pCmdUI->Enable(CanPaste());
  120. }
  121. void CTabEdit::OnEditClear() 
  122. {
  123. Clear();
  124. }
  125. void CTabEdit::OnUpdateEditClear(CCmdUI* pCmdUI) 
  126. {
  127. pCmdUI->Enable(SelectionMade());
  128. }
  129. void CTabEdit::OnEditSelectAll() 
  130. {
  131. SetSel(0, -1);
  132. }
  133. void CTabEdit::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
  134. {
  135. pCmdUI->Enable(GetWindowTextLength() > 0);
  136. }
  137. BOOL CTabEdit::CanPaste()
  138. {
  139. return ::IsClipboardFormatAvailable(CF_TEXT);
  140. }
  141. BOOL CTabEdit::SelectionMade()
  142. {
  143. int nStartChar, nEndChar;
  144. GetSel( nStartChar, nEndChar );
  145. return (nStartChar < nEndChar);
  146. }
  147. BOOL CTabEdit::OnEraseBkgnd(CDC* pDC) 
  148. {
  149. UNREFERENCED_PARAMETER(pDC);
  150. return TRUE;
  151. }
  152. void CTabEdit::OnPaint() 
  153. {
  154. CPaintDC dc(this); // device context for painting
  155. // Get the client rect.
  156. CRect r;
  157. GetClientRect(&r);
  158. // Paint to a memory device context to help
  159. // eliminate screen flicker.
  160. CXTMemDC memDC(&dc, r, GetXtremeColor(COLOR_WINDOW));
  161. // Now let the window do its default painting...
  162. CEdit::DefWindowProc( WM_PAINT, (WPARAM)memDC.m_hDC, 0 );
  163. }
  164. void CTabEdit::OnDropFiles(HDROP hDropInfo) 
  165. {
  166.     SetWindowText(_T(""));
  167. TCHAR szFileName[MAX_PATH];
  168.     ::DragQueryFile(hDropInfo,0,szFileName,MAX_PATH);
  169. if (_taccess(szFileName, 0) != -1)
  170.     {
  171.         CStdioFile file(szFileName, CFile::modeRead | CFile::typeText);
  172.         
  173.     CString strBuffer1;
  174.     file.ReadString( strBuffer1 );
  175.     CString strBuffer2;
  176.     while (file.ReadString(strBuffer1))
  177.     {
  178.     strBuffer2 += (strBuffer1 + _T("rn"));
  179.     }
  180.         
  181.         file.Close();
  182.     if ( strBuffer2.GetLength( ) )
  183.     {
  184.             SetWindowText(strBuffer2);
  185.     }
  186. }
  187.     
  188. CEdit::OnDropFiles(hDropInfo);
  189. }