home *** CD-ROM | disk | FTP | other *** search
- unit RichEditUtils;
-
- interface
- uses StrUtils,ComCtrls,WinTypes,Messages,Forms,Dialogs,SysUtils;
-
- function LinesShowing(Red: TRichEdit): integer;
- function GetLineNum( Red: TRichEdit; selIndex : longint ) : LongInt;
- function GetCurrLineNum( Red: TRichEdit ) : LongInt;
- procedure GetCurrLineAndColNum( Red : TRichEdit; var LinePos, ColPos : LongInt );
- function GetLineSelStartIndex( Red: TRichEdit; LineNum: LongInt ) : LongInt;
- procedure HideSelection( Red: TRichEdit );
- procedure ShowSelection( Red: TRichEdit );
-
- // The following function is used in this unit but is not made public
- // function GetColNum( Red: TRichEdit; SelIndex, LineNum : longint ) : LongInt;
-
- implementation
-
- function LinesShowing(Red: TRichEdit): integer;
- // This function is taken from the Delphi Knowledgebase: tip #2980
- // N.B. This assumes that the same font is used throughout the RichEdit box
- Var
- Oldfont: HFont; {the old font}
- DC: THandle; {Device context handle}
- Tm: TTextMetric; {text metric structure}
- TheRect: TRect;
- begin
- DC := GetDC(Red.Handle); {Get the memo's device context}
- try
- {Select the font}
- OldFont := SelectObject(DC, Red.Font.Handle);
- try
- GetTextMetrics(DC, Tm); {Get the text metric info}
- Red.Perform(EM_GETRECT, 0, longint(@TheRect));
- Result := (TheRect.Bottom - TheRect.Top) div
- (Tm.tmHeight + Tm.tmExternalLeading);
- finally
- SelectObject(DC, Oldfont); {Select the old font}
- end;
- finally
- ReleaseDC(Red.Handle, DC); {Release the device context}
- end;
- end;
-
- function GetLineNum( Red: TRichEdit; SelIndex : longint ) : LongInt;
- { return index of Line in RichEdit }
- begin
- result := SendMessage(Red.Handle, EM_LINEFROMCHAR, SelIndex ,0);
- end;
-
- function GetCurrLineNum( Red: TRichEdit ) : LongInt;
- { return index of currently selected Line in RichEdit }
- begin
- result := GetLineNum( Red, Red.SelStart );
- end;
-
- function GetColNum( Red: TRichEdit; SelIndex, LineNum : longint ) : LongInt;
- { return index of column (i.e. char pos in Line) in RichEdit }
- begin
- result := SelIndex -(SendMessage(Red.Handle, EM_LINEINDEX, linenum, 0)) + 1;
- end;
-
- procedure GetCurrLineAndColNum( Red : TRichEdit; var LinePos, ColPos : LongInt );
- { Assign the current Line Number and Column Number of the cursor to the
- arguments: LinePos and ColPos }
- begin
- LinePos := GetLineNum(Red, Red.SelStart ) ;
- ColPos := GetColNum(Red, Red.SelStart, LinePos );
- end;
-
-
-
- function GetLineSelStartIndex( Red: TRichEdit; LineNum: LongInt ) : LongInt;
- { return Selection (char num from start of RichEdit) index of 1st character in
- the Line specified by LineNum }
- begin
- result := SendMessage(Red.Handle, EM_LINEINDEX, LineNum,0);
- end;
-
- procedure HideSelection( Red: TRichEdit );
- begin
- //!! API - turn on Highlighting again
- SendMessage(Red.Handle,WM_USER+63{ EM_HIDESELECTION}, 1,0);
- end;
-
- procedure ShowSelection( Red: TRichEdit );
- begin
- //!! API - turn on Highlighting again
- SendMessage(Red.Handle,WM_USER+63{ EM_HIDESELECTION}, 0,0);
- end;
-
-
- end.
-