//---------------------------------------------------------------- // // MlsGrid - MultiLine StringGrid Component // //---------------------------------------------------------------- unit MlsGrid; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls; type // TMLInplaceEditの宣言 ---------------------- TMLInplaceEdit = class(TInplaceEdit) private { Private 宣言 } protected { Protected 宣言 } procedure CreateParams(var Params:TCreateParams); override; procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure KeyPress(var Key:Char); override; public { Public 宣言 } published { Published 宣言 } end; // TMLStringGridの宣言------------------------ TMLStringGrid = class(TStringGrid) private { Private 宣言 } FWordWrap: Boolean; procedure SetWordWrap(Value: Boolean); protected { Protected 宣言 } procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override; procedure KeyDown(var Key: Word; Shift:TShiftState); override; procedure KeyPress(var Key:Char); override; function CreateEditor: TInplaceEdit; override; public { Public 宣言 } published { Published 宣言 } property WordWrap : Boolean read FWordWrap write SetWordWrap default False; end; procedure Register; implementation // TMLInplaceEdit ----------------------------- procedure TMLInplaceEdit.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); // スタイルに複数行エディットを追加 Params.Style := Params.Style or ES_AUTOHSCROLL or ES_AUTOVSCROLL; end; procedure TMLInplaceEdit.KeyDown(var Key:Word; Shift: TShiftState); var StartPos, EndPos: Integer; begin case Key of // 矢印キーの動作変更 VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT : // シフトなしは編集に使用 if Shift = [] then Exit // Ctrlでセル移動 else if Shift = [ssCtrl] then begin // CtrlなしでのStringGridの動作 TMLStringGrid(Grid).KeyDown(Key, []); // 選択を解除 Deselect; Key := 0; end; VK_RETURN: // Ctrl + Enterで編集終了 if (Shift = [ssCtrl]) then begin TMLStringGrid(Grid).HideEditor; Key := 0; end; end; inherited KeyDown(Key, Shift); end; procedure TMLInplaceEdit.KeyPress(var Key:Char); begin // 改行コードを ^J に変えて入力させる if Key = #13 then Key := ^J; inherited KeyPress(Key); end; // TMLStringGrid ------------------------------ function TMLStringGrid.CreateEditor: TInplaceEdit; begin // TInplaceEditのかわりに // TMLInplaceEditを生成する Result := TInplaceEdit(TMLInplaceEdit.Create(Self)); end; procedure TMLStringGrid.KeyDown(var Key: Word; Shift: TShiftState); begin inherited KeyDown(Key, Shift); // 編集開始時に選択状態を解除 if (Key = VK_F2) and (InplaceEditor<>nil) then InplaceEditor.Deselect; end; procedure TMLStringGrid.KeyPress(var Key:Char); begin if Key = #13 then begin // 編集開始時に選択状態を解除 inherited KeyPress(Key); if (InplaceEditor<>nil) then InplaceEditor.Deselect; end else inherited KeyPress(Key); end; procedure TMLStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); procedure DrawCellText; var S: String; Options: Integer; begin S := Cells[ACol, ARow]; ARect.Left := ARect.Left + 2; ARect.Top := ARect.Top + 2; // メモの表示で描画 Options := DT_EDITCONTROL; if FWordWrap then Options := Options or DT_WORDBREAK; DrawTextEx(Canvas.Handle, PChar(S), Length(S), ARect, Options, nil); end; var StoreDef: Boolean; begin StoreDef := DefaultDrawing; DefaultDrawing := False; if StoreDef then DrawCellText; inherited DrawCell(ACol, ARow, ARect, AState); DefaultDrawing := StoreDef; end; procedure TMLStringGrid.SetWordWrap(Value: Boolean); begin if Value <> FWordWrap then begin FWordWrap := Value; Invalidate; end; end; //--------------------------------------------- procedure Register; begin RegisterComponents('Samples', [TMLStringGrid]); end; end.