DalsuRichEdit.cpp
上传用户:zggdcz
上传日期:2007-01-02
资源大小:37k
文件大小:4k
源码类别:

RichEdit

开发平台:

Visual C++

  1. // DalsuRichEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. //#include "ExRichEdit.h"
  5. #include "DalsuRichEdit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDalsuRichEdit
  13. CDalsuRichEdit::CDalsuRichEdit()
  14. {
  15. }
  16. CDalsuRichEdit::~CDalsuRichEdit()
  17. {
  18. }
  19. BEGIN_MESSAGE_MAP(CDalsuRichEdit, CRichEditCtrl)
  20. //{{AFX_MSG_MAP(CDalsuRichEdit)
  21. ON_WM_CREATE()
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDalsuRichEdit message handlers
  26. int CDalsuRichEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  27. {
  28. if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
  29. return -1;
  30. // TODO: Add your specialized creation code here
  31. CHARFORMAT cf;
  32. cf.cbSize = sizeof (CHARFORMAT);  
  33. cf.dwMask = CFM_FACE | CFM_SIZE; 
  34. //cf.dwEffects; 
  35. cf.yHeight = 180; 
  36. //cf.yOffset; 
  37. //cf.crTextColor=RGB(250,0,0); 
  38. //cf.bCharSet; 
  39. //cf.bPitchAndFamily; 
  40. sprintf(cf.szFaceName, "MS Sans Serif"); 
  41.  
  42. SetDefaultCharFormat(cf); 
  43.  
  44. return 0;
  45. }
  46. void CDalsuRichEdit::AddText(LPCTSTR szTextIn, COLORREF &crNewColor)
  47. {
  48. int iTotalTextLength = GetWindowTextLength();
  49. SetSel(iTotalTextLength, iTotalTextLength);
  50. ReplaceSel(szTextIn);
  51. int iStartPos = iTotalTextLength;
  52. CHARFORMAT cf;
  53. cf.cbSize = sizeof(CHARFORMAT);
  54. cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
  55. cf.dwEffects =(unsigned long) ~( CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
  56. cf.crTextColor = crNewColor;//RGB(0, 0, 0);
  57. int iEndPos = GetWindowTextLength();
  58. SetSel(iStartPos, iEndPos);
  59. SetSelectionCharFormat(cf);
  60. HideSelection(TRUE, FALSE);
  61. LineScroll(1);
  62. }
  63. void CDalsuRichEdit::AddText(CString &strTextIn, COLORREF &crNewColor)
  64. {
  65. int iTotalTextLength = GetWindowTextLength();
  66. SetSel(iTotalTextLength, iTotalTextLength);
  67. ReplaceSel((LPCTSTR)strTextIn);
  68. int iStartPos = iTotalTextLength;
  69. CHARFORMAT cf;
  70. cf.cbSize = sizeof(CHARFORMAT);
  71. cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
  72. cf.dwEffects = (unsigned long)~( CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
  73. cf.crTextColor = crNewColor;//RGB(0, 0, 0);
  74. // SetSelectionCharFormat(cf);
  75. int iEndPos = GetWindowTextLength();
  76. SetSel(iStartPos, iEndPos);
  77. SetSelectionCharFormat(cf);
  78. HideSelection(TRUE, FALSE);
  79. LineScroll(1);
  80. }
  81. // Example : Total 20 char, Name 16char +  " : "
  82. void CDalsuRichEdit::AddName(CString &strName, COLORREF &crNewColor)
  83. {
  84. // Write the strName on the DalRichEdit
  85. int iTotalTextLength = GetWindowTextLength();
  86. SetSel(iTotalTextLength, iTotalTextLength);
  87. ReplaceSel((LPCTSTR)strName);
  88. int iStartPos = iTotalTextLength;
  89. // Formating the string that will be changed
  90. CHARFORMAT cf;
  91. cf.cbSize = sizeof (CHARFORMAT);  
  92. cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
  93. cf.dwEffects = (unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
  94. cf.crTextColor = crNewColor;//RGB(0, 0, 255); 
  95. //  SetSelectionCharFormat(cf); 
  96.  
  97. // The last iTotalTextLength is The iStartPos
  98. // After Replace the strName, ReCall GetWindowTextLength() and the return value
  99. // is iEndPos...
  100. int iEndPos = GetWindowTextLength();
  101. SetSel(iStartPos, iEndPos);
  102. TRACE(_T("=====Start:%d, End:%dn"), iStartPos, iEndPos);
  103.   SetSelectionCharFormat(cf); 
  104. HideSelection(TRUE, FALSE);
  105. }
  106. void CDalsuRichEdit::AddMsg(CString &strMsg, COLORREF &crNewColor, 
  107. BOOL bUnderLine, 
  108. BOOL bBold)
  109. {
  110. int iTotalLength = GetWindowTextLength();
  111. SetSel(iTotalLength, iTotalLength);
  112. ReplaceSel((LPCTSTR)strMsg);
  113. int iStartPos = iTotalLength;
  114. int iEndPos = GetWindowTextLength();
  115. CHARFORMAT cf;
  116. cf.cbSize = sizeof(CHARFORMAT);
  117. cf.dwMask = CFM_COLOR | CFM_BOLD | CFM_UNDERLINE;
  118. cf.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR);
  119. cf.crTextColor = crNewColor;
  120. cf.dwEffects |= bUnderLine ? CFE_UNDERLINE : cf.dwEffects ;
  121. cf.dwEffects |= bBold ? CFE_BOLD : cf.dwEffects;
  122. SetSel(iStartPos, iEndPos);
  123. TRACE(_T("AddMsg=====Start:%d, End:%d, crNewColor:%ldn"), iStartPos, iEndPos, cf.crTextColor);
  124. SetSelectionCharFormat(cf);
  125. HideSelection(TRUE, FALSE);
  126. LineScroll(1);
  127. }