PowerTools SPREAD for Windows Forms 8.0J
入力マップとアクションマップ

コントロールは、ワークブックを表すSpreadViewオブジェクトを保持します。SpreadViewオブジェクトの内部処理では、キー入力を処理するために、アクションの割り当てとペアになった入力マップを使用します。入力マップ(InputMapオブジェクト)は、キー入力(KeyStroke オブジェクト)を、アクションを識別するオブジェクトに変換するために使用されます。このオブジェクトをアクションに変換するために、アクションマップ(ActionMapオブジェクト)が使用されます。後述のサンプルを参照してください。

サンプルコード

次のサンプルコードは、KeyDownイベントを処理する内部処理の一部です。入力マップを使用して、キー入力をアクションを識別するオブジェクトに変換します。そして、このオブジェクトをアクションマップを使用してアクションに変換し、実行します。

C#
コードのコピー
object actionMapKey = GetInputMap(InputMapMode.WhenFocused).Get(new Keystroke(e.KeyCode, e.Modifiers));
if (actionMapKey != null)
{
    Action action = GetActionMap().Get(actionMapKey);
    if (action != null)
    {
        action.PerformAction(this);
        e.Handled = true;
    }
}
Visual Basic
コードのコピー
Dim actionMapKey As Object = GetInputMap(InputMapMode.WhenFocused).Get(New Keystroke(e.KeyCode, e.Modifiers))
If Not (actionMapKey Is Nothing) Then
    Dim action As Action = GetActionMap().Get(actionMapKey)
    If Not (action Is Nothing) Then
        action.PerformAction(Me)
        e.Handled = True
    End If
End If

次のサンプルコードは、選択範囲のに含まれる行を非表示、および再表示するアクションを独自に作成します。

Excelでは、[Ctrl]+[9]、および[Ctrl]+[Shift]+[9]を使用して、行の非表示および再表示を行います。作成したアクションを使用して、コントロールに同じ機能を実装できます。

C#
コードのコピー
private class HideRowAction : Action
{
    public override void PerformAction(object source)
    {
        if (source is SpreadView)
        {
            SpreadView spread = (SpreadView)source;
            SheetView sheet = spread.Sheets[spread.ActiveSheetIndex];
            if (sheet.SelectionCount > 0)
            {
                for (int i = 0; i < sheet.SelectionCount; i++)
                {
                    CellRange range = sheet.GetSelection(i);
                    if (range.Row == -1)
                        sheet.Rows[0, sheet.RowCount - 1].Visible = false;
                    else
                        sheet.Rows[range.Row, range.Row + range.RowCount - 1].Visible = false;
                }
            }
            else
            {
                sheet.Rows[sheet.ActiveRowIndex].Visible = false;
            }
        }
    }
}
private class UnhideRowAction : Action
{
    public override void PerformAction(object source)
    {
        if (source is SpreadView)
        {
            SpreadView spread = (SpreadView)source;
            SheetView sheet = spread.Sheets[spread.ActiveSheetIndex];
            if (sheet.SelectionCount > 0)
            {
                for (int i = 0; i < sheet.SelectionCount; i++)
                {
                    CellRange range = sheet.GetSelection(i);
                    if (range.Row == -1)
                        sheet.Rows[0, sheet.RowCount - 1].Visible = true;
                    else
                        sheet.Rows[range.Row, range.Row + range.RowCount - 1].Visible = true;
                }
            }
            else
            {
                sheet.Rows[sheet.ActiveRowIndex].Visible = true;
            }
        }
    }
}
//コードビハインドに以下を記述してアクションを割り当てます
InputMap im = spread.GetInputMap(InputMapMode.WhenFocused);
ActionMap am = spread.GetActionMap();
im.Put(new Keystroke(Keys.D9, Keys.Control), "HideRow");
im.Put(new Keystroke(Keys.D9, Keys.Control | Keys.Shift), "UnhideRow");
am.Put("HideRow", new HideRowAction());
am.Put("UnhideRow", new UnhideRowAction());
Visual Basic
コードのコピー
Private Class HideRowAction
    Inherits Action
    Public Overrides Sub PerformAction([source] As Object)
        If TypeOf [source] Is SpreadView Then
            Dim spread As SpreadView = CType([source], SpreadView)
            Dim sheet As SheetView = spread.Sheets(spread.ActiveSheetIndex)
            If sheet.SelectionCount > 0 Then
                Dim i As Integer
                For i = 0 To sheet.SelectionCount - 1
                    Dim range As CellRange = sheet.GetSelection(i)
                    If range.Row = - 1 Then
                        sheet.Rows(0, sheet.RowCount - 1).Visible = False
                    Else
                        sheet.Rows(range.Row, range.Row + range.RowCount - 1).Visible = False    
                    End If
                Next i
            Else
                sheet.Rows(sheet.ActiveRowIndex).Visible = False
            End If
        End If
    End Sub
End Class
Private Class UnhideRowAction
    Inherits Action
    Public Overrides Sub PerformAction([source] As Object)
        If TypeOf [source] Is SpreadView Then
            Dim spread As SpreadView = CType([source], SpreadView)
            Dim sheet As SheetView = spread.Sheets(spread.ActiveSheetIndex)
            If sheet.SelectionCount > 0 Then
                Dim i As Integer
                For i = 0 To sheet.SelectionCount - 1
                    Dim range As CellRange = sheet.GetSelection(i)
                    If range.Row = - 1 Then
                        sheet.Rows(0, sheet.RowCount - 1).Visible = True
                    Else
                        sheet.Rows(range.Row, range.Row + range.RowCount - 1).Visible = True
                    End If
                Next i
            Else
                sheet.Rows(sheet.ActiveRowIndex).Visible = True
            End If
        End If
    End Sub
End Class
'コードビハインドに以下を記述してアクションを割り当てます
Dim im As InputMap = spread.GetInputMap(InputMapMode.WhenFocused)
Dim am As ActionMap = spread.GetActionMap()
im.Put(New Keystroke(Keys.D9, Keys.Control), "HideRow")
im.Put(New Keystroke(Keys.D9, Keys.Control Or Keys.Shift), "UnhideRow")
am.Put("HideRow", New HideRowAction())
am.Put("UnhideRow", New UnhideRowAction())
関連トピック

 

 


© 2004-2015, GrapeCity inc. All rights reserved.