MainFrm.pas
上传用户: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. unit MainFrm;
  12. interface
  13. {$I RV_Defs.inc}
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  16.   Dialogs, Menus, RVStyle, RVScroll, RichView, RVEdit, RVGetText;
  17. type
  18.   TfrmMain = class(TForm)
  19.     RichViewEdit1: TRichViewEdit;
  20.     RVStyle1: TRVStyle;
  21.     PopupMenu1: TPopupMenu;
  22.     procedure RichViewEdit1KeyDown(Sender: TObject; var Key: Word;
  23.       Shift: TShiftState);
  24.     procedure FormCreate(Sender: TObject);
  25.   private
  26.     procedure AddPopupMenuItem(s: String);
  27.     procedure AutoCompletePopupMenuClick(Sender: TObject);
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32. var
  33.   frmMain: TfrmMain;
  34. implementation
  35. {$R *.dfm}
  36. procedure TfrmMain.FormCreate(Sender: TObject);
  37. begin
  38.   {$IFDEF RICHVIEWDEF6}
  39.   PopupMenu1.AutoHotkeys := maManual;
  40.   {$ENDIF}
  41.   RichViewEdit1.Clear;
  42.   RichViewEdit1.AddNL('Type a word and press Ctrl+Space for suggestions',0,0);
  43.   RichViewEdit1.Format;
  44. end;
  45. procedure TfrmMain.AddPopupMenuItem(s: String);
  46. var mi: TMenuItem;
  47. begin
  48.    mi := TMenuItem.Create(PopupMenu1);
  49.    mi.Caption := s;
  50.    mi.OnClick := AutoCompletePopupMenuClick;
  51.    PopupMenu1.Items.Add(mi);
  52. end;
  53. procedure TfrmMain.AutoCompletePopupMenuClick(Sender: TObject);
  54. begin
  55.   RichViewEdit1.SelectCurrentWord;
  56.   RichViewEdit1.InsertText((Sender as TMenuItem).Caption, False);
  57. end;
  58. procedure TfrmMain.RichViewEdit1KeyDown(Sender: TObject; var Key: Word;
  59.   Shift: TShiftState);
  60. var s: String;
  61.     pt: TPoint;
  62. begin
  63.   if (Key=VK_SPACE) and (ssCtrl in Shift) then begin
  64.     s := GetCurrentWord(RichViewEdit1);
  65.     if s<>'' then begin
  66.       while PopupMenu1.Items.Count>0 do
  67.         PopupMenu1.Items[0].Free;
  68.       AddPopupMenuItem('super'+s);
  69.       AddPopupMenuItem('mega'+s);
  70.       AddPopupMenuItem('ultra'+s);
  71.       AddPopupMenuItem('grand'+s);
  72.       GetCaretPos(pt);
  73.       pt := RichViewEdit1.TopLevelEditor.ClientToScreen(pt);
  74.       PopupMenu1.Popup(pt.X, pt.Y+10);
  75.     end;
  76.     Key := 0;
  77.   end;
  78. end;
  79. end.