home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue158 / delphi / RichEditTest / RichEditUtils.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-13  |  3.0 KB  |  94 lines

  1. unit RichEditUtils;
  2.  
  3. interface
  4. uses StrUtils,ComCtrls,WinTypes,Messages,Forms,Dialogs,SysUtils;
  5.  
  6. function LinesShowing(Red: TRichEdit): integer;
  7. function GetLineNum( Red: TRichEdit; selIndex : longint ) : LongInt;
  8. function GetCurrLineNum( Red: TRichEdit ) : LongInt;
  9. procedure GetCurrLineAndColNum( Red : TRichEdit; var LinePos, ColPos : LongInt );
  10. function GetLineSelStartIndex( Red: TRichEdit; LineNum: LongInt ) : LongInt;
  11. procedure HideSelection( Red: TRichEdit );
  12. procedure ShowSelection( Red: TRichEdit );
  13.  
  14. // The following function is used in this unit but is not made public
  15. // function GetColNum( Red: TRichEdit; SelIndex, LineNum : longint ) : LongInt;
  16.  
  17. implementation
  18.  
  19. function LinesShowing(Red: TRichEdit): integer;
  20. // This function is taken from the Delphi Knowledgebase: tip #2980
  21. // N.B. This assumes that the same font is used throughout the RichEdit box
  22. Var
  23.   Oldfont: HFont;  {the old font}
  24.   DC: THandle;     {Device context handle}
  25.   Tm: TTextMetric; {text metric structure}
  26.   TheRect: TRect;
  27. begin
  28.   DC := GetDC(Red.Handle); {Get the memo's device context}
  29.   try
  30.    {Select the font}
  31.     OldFont := SelectObject(DC, Red.Font.Handle);
  32.     try
  33.       GetTextMetrics(DC, Tm); {Get the text metric info}
  34.       Red.Perform(EM_GETRECT, 0, longint(@TheRect));
  35.       Result := (TheRect.Bottom - TheRect.Top) div
  36.          (Tm.tmHeight + Tm.tmExternalLeading);
  37.     finally
  38.       SelectObject(DC, Oldfont); {Select the old font}
  39.     end;
  40.   finally
  41.     ReleaseDC(Red.Handle, DC); {Release the device context}
  42.   end;
  43. end;
  44.  
  45. function GetLineNum( Red: TRichEdit; SelIndex : longint ) : LongInt;
  46. { return index of Line in RichEdit }
  47. begin
  48.    result := SendMessage(Red.Handle, EM_LINEFROMCHAR, SelIndex ,0);
  49. end;
  50.  
  51. function GetCurrLineNum( Red: TRichEdit ) : LongInt;
  52. { return index of currently selected Line in RichEdit }
  53. begin
  54.    result := GetLineNum( Red, Red.SelStart );
  55. end;
  56.  
  57. function GetColNum( Red: TRichEdit; SelIndex, LineNum : longint ) : LongInt;
  58. { return index of column (i.e. char pos in Line) in RichEdit }
  59. begin
  60.    result := SelIndex -(SendMessage(Red.Handle, EM_LINEINDEX, linenum, 0)) + 1;
  61. end;
  62.  
  63. procedure GetCurrLineAndColNum( Red : TRichEdit; var LinePos, ColPos : LongInt );
  64. { Assign the current Line Number and Column Number of the cursor to the
  65.   arguments: LinePos and ColPos }
  66. begin
  67.    LinePos := GetLineNum(Red, Red.SelStart ) ;
  68.    ColPos := GetColNum(Red, Red.SelStart, LinePos );
  69. end;
  70.  
  71.  
  72.  
  73. function GetLineSelStartIndex( Red: TRichEdit; LineNum: LongInt ) : LongInt;
  74. { return Selection (char num from start of RichEdit) index of 1st character in
  75.   the Line specified by LineNum }
  76. begin
  77.    result := SendMessage(Red.Handle, EM_LINEINDEX, LineNum,0);
  78. end;
  79.  
  80. procedure HideSelection( Red: TRichEdit );
  81. begin
  82.     //!! API - turn on Highlighting again
  83.   SendMessage(Red.Handle,WM_USER+63{ EM_HIDESELECTION}, 1,0);
  84. end;
  85.  
  86. procedure ShowSelection( Red: TRichEdit );
  87. begin
  88.     //!! API - turn on Highlighting again
  89.   SendMessage(Red.Handle,WM_USER+63{ EM_HIDESELECTION}, 0,0);
  90. end;
  91.  
  92.  
  93. end.
  94.