winctrl4.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CMNCTL_SEG
  12. #pragma code_seg(AFX_CMNCTL_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. #ifndef _AFX_NO_RICHEDIT_SUPPORT
  20. /////////////////////////////////////////////////////////////////////////////
  21. // _AFX_RICHEDIT_STATE
  22. _AFX_RICHEDIT_STATE::~_AFX_RICHEDIT_STATE()
  23. {
  24. if (m_hInstRichEdit != NULL)
  25. ::FreeLibrary(m_hInstRichEdit);
  26. }
  27. _AFX_RICHEDIT_STATE* AFX_CDECL AfxGetRichEditState()
  28. {
  29. return _afxRichEditState.GetData();
  30. }
  31. BOOL PASCAL AfxInitRichEdit()
  32. {
  33. _AFX_RICHEDIT_STATE* pState = _afxRichEditState;
  34. if (pState->m_hInstRichEdit == NULL)
  35. pState->m_hInstRichEdit = LoadLibraryA("RICHED32.DLL");
  36. return pState->m_hInstRichEdit != NULL;
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CRichEdit
  40. BOOL CRichEditCtrl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  41. {
  42. if (!AfxInitRichEdit())
  43. return FALSE;
  44. CWnd* pWnd = this;
  45. return pWnd->Create(_T("RICHEDIT"), NULL, dwStyle, rect, pParentWnd, nID);
  46. }
  47. int CRichEditCtrl::GetLine(int nIndex, LPTSTR lpszBuffer) const
  48. {
  49. ASSERT(::IsWindow(m_hWnd));
  50. return (int)::SendMessage(m_hWnd, EM_GETLINE, nIndex,
  51. (LPARAM)lpszBuffer);
  52. }
  53. int CRichEditCtrl::LineIndex(int nLine /* = -1 */) const
  54. {
  55. ASSERT(::IsWindow(m_hWnd));
  56. return (int)::SendMessage(m_hWnd, EM_LINEINDEX, nLine, 0);
  57. }
  58. int CRichEditCtrl::LineLength(int nLine /* = -1 */) const
  59. {
  60. ASSERT(::IsWindow(m_hWnd));
  61. return (int)::SendMessage(m_hWnd, EM_LINELENGTH, nLine, 0);
  62. }
  63. void CRichEditCtrl::LineScroll(int nLines, int nChars /* = 0 */)
  64. {
  65. ASSERT(::IsWindow(m_hWnd));
  66. ::SendMessage(m_hWnd, EM_LINESCROLL, nChars, nLines);
  67. }
  68. void CRichEditCtrl::SetSel(long nStartChar, long nEndChar)
  69. {
  70. ASSERT(::IsWindow(m_hWnd));
  71. CHARRANGE cr;
  72. cr.cpMin = nStartChar;
  73. cr.cpMax = nEndChar;
  74. ::SendMessage(m_hWnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  75. }
  76. BOOL CRichEditCtrl::CanPaste(UINT nFormat) const
  77. {
  78. ASSERT(::IsWindow(m_hWnd));
  79. COleMessageFilter* pFilter = AfxOleGetMessageFilter();
  80. if (pFilter != NULL)
  81. pFilter->BeginBusyState();
  82. BOOL b = (BOOL)::SendMessage(m_hWnd, EM_CANPASTE, nFormat, 0L);
  83. if (pFilter != NULL)
  84. pFilter->EndBusyState();
  85. return b;
  86. }
  87. void CRichEditCtrl::PasteSpecial(UINT nClipFormat, DWORD dvAspect, HMETAFILE hMF)
  88. {
  89. ASSERT(::IsWindow(m_hWnd));
  90. REPASTESPECIAL reps;
  91. reps.dwAspect = dvAspect;
  92. reps.dwParam = (DWORD)hMF;
  93. ::SendMessage(m_hWnd, EM_PASTESPECIAL, nClipFormat, (LPARAM)&reps);
  94. }
  95. int CRichEditCtrl::GetLine(int nIndex, LPTSTR lpszBuffer, int nMaxLength) const
  96. {
  97. ASSERT(::IsWindow(m_hWnd));
  98. *(LPINT)lpszBuffer = nMaxLength;
  99. return (int)::SendMessage(m_hWnd, EM_GETLINE, nIndex, (LPARAM)lpszBuffer);
  100. }
  101. void CRichEditCtrl::GetSel(long& nStartChar, long& nEndChar) const
  102. {
  103. ASSERT(::IsWindow(m_hWnd));
  104. CHARRANGE cr;
  105. ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
  106. nStartChar = cr.cpMin;
  107. nEndChar = cr.cpMax;
  108. }
  109. CString CRichEditCtrl::GetSelText() const
  110. {
  111. ASSERT(::IsWindow(m_hWnd));
  112. CHARRANGE cr;
  113. cr.cpMin = cr.cpMax = 0;
  114. ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
  115. LPSTR lpsz = (char*)_alloca((cr.cpMax - cr.cpMin + 1)*2);
  116. lpsz[0] = NULL;
  117. ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);
  118. return lpsz;
  119. }
  120. IRichEditOle* CRichEditCtrl::GetIRichEditOle() const
  121. {
  122. ASSERT(::IsWindow(m_hWnd));
  123. IRichEditOle *pRichItem = NULL;
  124. ::SendMessage(m_hWnd, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichItem);
  125. return pRichItem;
  126. }
  127. BOOL CRichEditCtrl::SetDefaultCharFormat(CHARFORMAT &cf)
  128. {
  129. ASSERT(::IsWindow(m_hWnd));
  130. cf.cbSize = sizeof(CHARFORMAT);
  131. return (BOOL)::SendMessage(m_hWnd, EM_SETCHARFORMAT, 0, (LPARAM)&cf);
  132. }
  133. BOOL CRichEditCtrl::SetSelectionCharFormat(CHARFORMAT &cf)
  134. {
  135. ASSERT(::IsWindow(m_hWnd));
  136. cf.cbSize = sizeof(CHARFORMAT);
  137. return (BOOL)::SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  138. }
  139. BOOL CRichEditCtrl::SetWordCharFormat(CHARFORMAT &cf)
  140. {
  141. ASSERT(::IsWindow(m_hWnd));
  142. cf.cbSize = sizeof(CHARFORMAT);
  143. return (BOOL)::SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION|SCF_WORD, (LPARAM)&cf);
  144. }
  145. DWORD CRichEditCtrl::GetDefaultCharFormat(CHARFORMAT &cf) const
  146. {
  147. ASSERT(::IsWindow(m_hWnd));
  148. cf.cbSize = sizeof(CHARFORMAT);
  149. return (DWORD)::SendMessage(m_hWnd, EM_GETCHARFORMAT, 0, (LPARAM)&cf);
  150. }
  151. DWORD CRichEditCtrl::GetSelectionCharFormat(CHARFORMAT &cf) const
  152. {
  153. ASSERT(::IsWindow(m_hWnd));
  154. cf.cbSize = sizeof(CHARFORMAT);
  155. return (DWORD)::SendMessage(m_hWnd, EM_GETCHARFORMAT, 1, (LPARAM)&cf);
  156. }
  157. DWORD CRichEditCtrl::GetParaFormat(PARAFORMAT &pf) const
  158. {
  159. ASSERT(::IsWindow(m_hWnd));
  160. pf.cbSize = sizeof(PARAFORMAT);
  161. return (DWORD)::SendMessage(m_hWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  162. }
  163. BOOL CRichEditCtrl::SetParaFormat(PARAFORMAT &pf)
  164. {
  165. ASSERT(::IsWindow(m_hWnd));
  166. pf.cbSize = sizeof(PARAFORMAT);
  167. return (BOOL)::SendMessage(m_hWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  168. }
  169. #endif //!_AFX_NO_RICHEDIT_SUPPORT
  170. /////////////////////////////////////////////////////////////////////////////
  171. #pragma warning(disable: 4074)
  172. #pragma init_seg(lib)
  173. #ifndef _AFX_NO_RICHEDIT_SUPPORT
  174. PROCESS_LOCAL(_AFX_RICHEDIT_STATE, _afxRichEditState)
  175. #endif
  176. /////////////////////////////////////////////////////////////////////////////