TStringGridのInplaceEditorのImeModeを設定する

TInplaceEditのImeModeプロパティはprotectedなので、外から操作できません。
そこで、TInplaceEditを継承したクラスを作成して、ImeModeプロパティを公開します。

class TMyEdit : public TInplaceEdit
{
public:
  __fastcall virtual TMyEdit(TComponent* AOwner) : TInplaceEdit(AOwner) {}
__published:
  __property ImeMode;
};

作成したTMyEditを使用するには、TStringGridを継承したクラスを作成して、CreateEditorメソッドでTMyEditを返します。

class TMyGrid : public TStringGrid
{
public:
  __fastcall TMyGrid(TComponent* AOwner) : TStringGrid(AOwner) {}
protected:
  TInplaceEdit* __fastcall CreateEditor();
};

TInplaceEdit* __fastcall TMyGrid::CreateEditor()
{
  //TInplaceEditのかわりにTMyEditを生成する
  TMyEdit* edit = new TMyEdit(this);
  //ここでImeModeを設定しても良い
  //ここではIMEの初期状態を全角カタカナにする
  edit->ImeMode = imKata;
  return edit;
}

セルによってImeModeプロパティを設定する場合は、GetEditTextメソッドで設定します。

class TMyGrid : public TStringGrid
{
public:
  __fastcall TMyGrid(TComponent* AOwner) : TStringGrid(AOwner) {}
protected:
  TInplaceEdit* __fastcall CreateEditor();
  DYNAMIC UnicodeString __fastcall GetEditText(int ACol, int ARow);
};

TInplaceEdit* __fastcall TMyGrid::CreateEditor()
{
  return new TMyEdit(this);
}
UnicodeString __fastcall TMyGrid::GetEditText(int ACol, int ARow)
{
  //列によってImeModeプロパティの値を変更する
  TMyEdit* edit = dynamic_cast<TMyEdit*>(this->InplaceEditor);
  switch (ACol) {
  case 1:
    edit->ImeMode = imHira;
    break;
  case 2:
    edit->ImeMode = imSAlpha;
    break;
  case 3:
    edit->ImeMode = imAlpha;
    break;
  }
  return TStringGrid::GetEditText(ACol, ARow);
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください