1 #pragma once 2 3 #include "../scintilla/include/SciLexer.h" 4 #include "../scintilla/include/Scintilla.h" 5 6 // CScintillaEditView 视图 7 8 #define SCINTILLA_MARGIN_LINENUMBER 0 9 #define MARGIN_FOLD_INDEX 1 10 11 class CScintillaEditView : public CView 12 { 13 DECLARE_DYNCREATE(CScintillaEditView) 14 15 protected: 16 CScintillaEditView(); // 动态创建所使用的受保护的构造函数 17 virtual ~CScintillaEditView(); 18 19 public: 20 struct KeepCurrentLine 21 { 22 KeepCurrentLine(CScintillaEditView* view) 23 : m_view(view) 24 { 25 //保存当前行 26 current_line = m_view->GetFirstVisibleLine(); 27 } 28 ~KeepCurrentLine() 29 { 30 //恢复当前行 31 m_view->SetFirstVisibleLine(current_line); 32 } 33 34 CScintillaEditView* m_view{}; 35 int current_line{}; 36 }; 37 38 virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 39 #ifdef _DEBUG 40 virtual void AssertValid() const; 41 #ifndef _WIN32_WCE 42 virtual void Dump(CDumpContext& dc) const; 43 #endif 44 #endif 45 46 void SetText(const wstring& text); 47 void GetText(wstring& text); 48 const wchar_t* GetText(int& size); //获取文本(返回字符串指针,需要自行释放内存) 49 const char* GetTextUtf8(int& size); //获取UTF8格式文本(返回字符串指针,需要自行释放内存) 50 void SetFontFace(const wchar_t* font_face); 51 void SetFontSize(int font_size); 52 void SetTabSize(int tab_size); 53 void SetSel(int start, int end, const wstring& edit_str); //设置选中范围(位置以字符为单位) 54 void GetSel(int& start, int& end); //获取选中范围(位置以字符为单位) 55 void SetBackgroundColor(COLORREF color); 56 void SetReadOnly(bool read_only); 57 bool IsReadOnly(); 58 59 void Undo(); 60 void Redo(); 61 void Cut(); 62 void Copy(); 63 void Paste(); 64 void SelectAll(); 65 void EmptyUndoBuffer(); //清空撤销缓存 66 67 void SetWordWrap(bool word_wrap); 68 69 bool IsEditChangeNotificationEnable(); 70 71 bool CanUndo(); 72 bool CanRedo(); 73 bool CanPaste(); 74 bool IsSelectionEmpty(); 75 bool IsModified(); 76 void SetSavePoint(); 77 78 void SetLineNumberWidth(int width); 79 void ShowLineNumber(bool show); 80 void SetLineNumberColor(COLORREF color); 81 82 int GetZoom(); 83 void SetZoom(int zoom); 84 85 enum eEolMode 86 { 87 EOL_CRLF, 88 EOL_CR, 89 EOL_LF 90 }; 91 void SetEolMode(eEolMode eolMode); 92 eEolMode GetEolMode(); 93 94 void ConvertEolMode(eEolMode eolMode); 95 96 void SetViewEol(bool show); 97 98 int GetFirstVisibleLine(); 99 void SetFirstVisibleLine(int line); 100 101 //语法解析 102 void SetLexer(int lexer); 103 void SetKeywords(int id, const char* keywords); 104 void SetSyntaxColor(int id, COLORREF color); 105 void SetSyntaxFontStyle(int id, bool bold, bool italic); 106 void SetLexerNormalText(); 107 108 static eEolMode JudgeEolMode(const wstring& str); 109 static int CharactorPosToBytePos(int pos, const wchar_t* str, size_t size); //将字符的位置转换成字节的位置(使用UTF8编码) 110 static int BytePosToCharactorPos(int pos, const char* str, size_t size); //将字节的位置转换成字符的位置(使用UTF8编码) 111 112 void SetContextMenu(CMenu* pMenu, CWnd* pMenuOwner); 113 114 void SetLexerLyric(); //设置LRC语法解析 115 116 private: 117 118 private: 119 bool m_change_notification_enable = true; //如果为false,则不响应文本改变消息 120 int m_line_number_width = 36; 121 COLORREF m_line_number_color{}; 122 COLORREF m_background_color{ RGB(255,255,255) }; 123 124 CMenu* m_pMenu{}; 125 CWnd* m_pContextMenuOwner{}; 126 127 protected: 128 DECLARE_MESSAGE_MAP() 129 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); 130 public: 131 afx_msg void OnPaint(); 132 virtual void PreSubclassWindow(); 133 virtual void OnInitialUpdate(); 134 afx_msg void OnRButtonUp(UINT nFlags, CPoint point); 135 }; 136 137 138