Delphi XE2 で開発を行っていますがテキストを表示する「TMemo」のコンポーネントがあります。
テキストの表示は簡単ですが、残念ながら行番号を表示する機能はありません。
テキストを表示する「Memo1」と行番号を表示する「Memo2」を作成し、両者のスクロールを同期して対応します。
「TMemo」にスクロールイベントを追加しましたが、垂直のスクロールバーの四角のツマミでスクロールさせた時はイベントは発生しませんでした。
最終的にタイマーを追加してタイマーイベント(200ms)で両者の最上行を監視して、今回最上行が前回最上行と異なったら、反対側をスクロールする処理で対応しました。
private
IntTopLineMemo: Integer;
IntTopLineNumber: Integer;
procedure TFormMain.Timer6Timer(Sender: TObject);
var
iLine5: Integer;
iLine6: Integer;
iLine7: Integer;
iLine8: Integer;
begin
if Memo1.Lines.Count > 0 then
begin
//画面の最上行
iLine5 := Memo1.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
//前回と違う
iLine5 <> IntTopLineMemo then
begin
//画面の最上行
iLine6 := Memo2.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
//移動
Memo2.Perform(EM_LINESCROLL, 0, iLine5 - iLine6);
end;
IntTopLineMemo := iLine5;
end;
if Memo2.Lines.Count > 0 then
begin
//画面の最上行
iLine7 := Memo2.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
前回と違う
if iLine7 <> IntTopLineNumber then
begin
//画面の最上行
iLine8 := Memo1.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
//移動
Memo1.Perform(EM_LINESCROLL, 0, iLine7 - iLine8);
//フォーカス設定
Memo1.SetFocus;
end;
IntTopLineNumber := iLine7;
end;
end;