Unit1.cpp
上传用户:daoqigc
上传日期:2021-04-20
资源大小:2795k
文件大小:3k
源码类别:

RichEdit

开发平台:

Delphi

  1. /*******************************************************}
  2. {                                                       }
  3. {       RichView                                        }
  4. {       AutoComplete demo
  5. {                                                       }
  6. {       Copyright (c) Sergey Tkachenko                  }
  7. {       svt@trichview.com                               }
  8. {       http://www.trichview.com                        }
  9. {                                                       }
  10. {*******************************************************/
  11. //---------------------------------------------------------------------------
  12. #include <vclvcl.h>
  13. #pragma hdrstop
  14. #include "Unit1.h"
  15. #include "RVGetText.hpp"
  16. //---------------------------------------------------------------------------
  17. #pragma link "RVEdit"
  18. #pragma link "RichView"
  19. #pragma link "RVScroll"
  20. #pragma link "RVStyle"
  21. #pragma link "RVGetText"
  22. #pragma resource "*.dfm"
  23. TForm1 *Form1;
  24. //---------------------------------------------------------------------------
  25. __fastcall TForm1::TForm1(TComponent* Owner)
  26. : TForm(Owner)
  27. {
  28. #if __BORLANDC__>0x0550
  29.   PopupMenu1->AutoHotkeys = maManual;
  30. #endif
  31.   RichViewEdit1->Clear();
  32.   RichViewEdit1->AddNL("Type a word and press Ctrl+Space for suggestions",0,0);
  33.   RichViewEdit1->Format();
  34. }
  35. //---------------------------------------------------------------------------
  36. void TForm1::AddPopupMenuItem(AnsiString s)
  37. {
  38.   TMenuItem*mi = new TMenuItem(PopupMenu1);
  39.   mi->Caption = s;
  40.   mi->OnClick = AutoCompletePopupMenuClick;
  41.   PopupMenu1->Items->Add(mi);
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TForm1::AutoCompletePopupMenuClick(TObject* Sender)
  45. {
  46.   RichViewEdit1->SelectCurrentWord();
  47.   RichViewEdit1->InsertText(((TMenuItem*)Sender)->Caption, false);
  48. }
  49. void __fastcall TForm1::RichViewEdit1KeyDown(TObject *Sender, WORD &Key,
  50. TShiftState Shift)
  51. {
  52.   if (Key==VK_SPACE && Shift.Contains(ssCtrl))
  53.   {
  54.     AnsiString s = GetCurrentWord(RichViewEdit1);
  55.     if (s!="")
  56.     {
  57.       while (PopupMenu1->Items->Count>0)
  58.       {
  59.         TMenuItem* item = PopupMenu1->Items->Items[0];
  60.         delete item;
  61.       }
  62.       AddPopupMenuItem("super"+s);
  63.       AddPopupMenuItem("mega"+s);
  64.       AddPopupMenuItem("ultra"+s);
  65.       AddPopupMenuItem("grand"+s);
  66.       TPoint pt;
  67.       GetCaretPos(&pt);
  68.       pt = RichViewEdit1->TopLevelEditor->ClientToScreen(pt);
  69.       PopupMenu1->Popup(pt.x, pt.y+10);
  70.     }
  71.     Key = 0;
  72.   }
  73. }
  74. //---------------------------------------------------------------------------