Delphi Programming / Object Pascal
サイトのトップページへ リンクのページへ ロゴマーク
[掲載 2009年06月11日] [更新 2013年12月02日] ワード操作フォームEx
   
ワード操作フォームEx   参考コード類
ショートカットキーの操作
動作確認等 Windows 7 U64(SP1) + Delphi XE Pro + Word 2010







コピー [ Ctrl ] + [ C ] の無効・有効化操作


[Ctrl] + [C],つまりコピーのアクセレータキー (ショートカット) を無効にする例です.設定を元に戻しておかないと設定がワード全体で無効になったままになってしまいます.


リスト1
[ Ctrl ] + [ C ] の無効・有効化操作
uses に Word2000, Clipbrd が必要
//=============================================================================
//  指定したアクセレータキー(ショートカット)を無効にする
//  ここでは[Ctrl]+[C]のコピー機能を対象
//  BuildKeyCodeメソッドでは最大4つのキーの組合せの指定が可能(そんなに必要か?)
//=============================================================================
procedure TplWordFormEx2.Button1Click(Sender: TObject);
var
  pKeyCode  : Integer;
  pKeyCode2 : OleVariant;
  Arg1      : OleVariant;
  Arg2      : OleVariant;
  AKeyBind  : Word2000.KeyBinding;
  strText   : String;
begin
  //キーの指定
  Arg1 := wdKeyControl;   //[Ctrl]
  Arg2 := wdKeyC;         //[C]

  //FindKeyの引数設定
  pKeyCode  := wdfWordApp.BuildKeyCode(Arg1, Arg2);
  pKeyCode2 := EmptyParam;

  //KeyBindingオブジェクトを取得してキーの組合せを表示
  AKeyBind := wdfWordApp.FindKey[pKeyCode, pKeyCode2];
  if AKeyBind.Command = '' then begin
    strText := AKeyBind.KeyString + ' は既に無効になっています.';
    MessageBox(Handle, PChar(strText), '情報', MB_ICONINFORMATION);
  end else begin
    //指定したキーバインドを無効にする
    AKeyBind.Disable;
    strText := AKeyBind.KeyString + ' ' + AKeyBind.Command + ' を無効にしました.';
    MessageBox(Handle, PChar(strText), '情報', MB_ICONINFORMATION);
  end;

  //テストのためにクリップボードをクリア
  //クリップボードにデータが残っていると前のデータが貼り付けされてしまう
  Clipboard.Clear;
  wdfSetFocusWord;
end;

//=============================================================================
//  指定したアクセレータキー(ショートカット)を元に戻す
//  ここでは[Ctrl]+[C]のコピー機能を対象
//=============================================================================
procedure TplWordFormEx2.Button2Click(Sender: TObject);
var
  pKeyCode  : Integer;
  pKeyCode2 : OleVariant;
  Arg1      : OleVariant;
  Arg2      : OleVariant;
  AKeyBind  : Word2000.KeyBinding;
begin
  //キーの指定
  Arg1 := wdKeyControl;  //[Ctrl]
  Arg2 := wdKeyC;        //[C]

  //FindKeyの引数設定
  pKeyCode  := wdfWordApp.BuildKeyCode(Arg1, Arg2);
  pKeyCode2 := EmptyParam;

  //KeyBindingオブジェクトを取得
  AKeyBind := wdfWordApp.FindKey[pKeyCode, pKeyCode2];
  //指定したキーバインドをデフォルトの動作に戻す
  AKeyBind.Clear;
  wdfSetFocusWord;
end;