Imports System.Windows.Forms
Imports System.Drawing

<ToolboxBitmap(GetType(OfficeMenu), "OfficeMenu.bmp")> _
Public Class OfficeMenu
    Inherits System.Windows.Forms.MainMenu

    'Clickイベントを定義するデリゲートを宣言
    Public Delegate Sub ClickEventHandler _
        (ByVal sender As Object, ByVal e As EventArgs)

    '各メニューのイベントの宣言
    Public Event File_Click As ClickEventHandler
    Public Event FileNew_Click As ClickEventHandler
    Public Event FileOpen_Click As ClickEventHandler
    Public Event FileClose_Click As ClickEventHandler
    Public Event FileOverSave_Click As ClickEventHandler
    Public Event FileSave_Click As ClickEventHandler
    Public Event FileQuit_Click As ClickEventHandler
    Public Event Edit_Click As ClickEventHandler
    Public Event EditCut_Click As ClickEventHandler
    Public Event EditCopy_Click As ClickEventHandler
    Public Event EditPaste_Click As ClickEventHandler
    Public Event EditDelete_Click As ClickEventHandler

    Public Sub New()

        MyBase.New()

        SetInitialMenuItem()

    End Sub

    Private Sub SetInitialMenuItem()

        'リソースとSystem.Reflection.Assemblyを
        '指定してResourceManagerを作成
        Dim rm As New _
           System.Resources.ResourceManager _
            ("OfficeMenu.Image", Me.GetType.Assembly)

        'ルートメニューを作成
        Dim fmi As ImageMenuItem = _
                New ImageMenuItem("ファイル(&F)")
        Dim emi As ImageMenuItem = _
                New ImageMenuItem("編集(&E)")

        Me.MenuItems.Add(fmi)
        Me.MenuItems.Add(emi)

        Dim ico As Icon = CType(rm.GetObject("New"), System.Drawing.Icon)

        'ファイル メニューを作成
        Dim smi As ImageMenuItem = _
                New ImageMenuItem("新規作成(&N)", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlN, ico.ToBitmap)
        fmi.MenuItems.Add(smi)
        ico = CType(rm.GetObject("Open"), System.Drawing.Icon)
        smi = New ImageMenuItem("開く(&O)...", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlO, ico.ToBitmap)
        fmi.MenuItems.Add(smi)
        smi = New ImageMenuItem("閉じる(&C)", _
                New EventHandler(AddressOf Menu_Click))
        fmi.MenuItems.Add(smi)
        smi = New ImageMenuItem("-")
        fmi.MenuItems.Add(smi)
        ico = CType(rm.GetObject("Save"), System.Drawing.Icon)
        smi = New ImageMenuItem("上書き保存(&S)...", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlS, ico.ToBitmap)
        fmi.MenuItems.Add(smi)
        smi = New ImageMenuItem("名前を付けて保存(&A)...", _
                New EventHandler(AddressOf Menu_Click))
        fmi.MenuItems.Add(smi)
        smi = New ImageMenuItem("-")
        fmi.MenuItems.Add(smi)
        smi = New ImageMenuItem("終了(&X)", _
                New EventHandler(AddressOf Menu_Click))
        fmi.MenuItems.Add(smi)

        '編集 メニューを作成
        ico = CType(rm.GetObject("Cut"), System.Drawing.Icon)
        smi = New ImageMenuItem("切り取り(&T)", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlX, ico.ToBitmap)
        emi.MenuItems.Add(smi)
        ico = CType(rm.GetObject("Copy"), System.Drawing.Icon)
        smi = New ImageMenuItem("コピー(&C)", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlC, ico.ToBitmap)
        emi.MenuItems.Add(smi)
        ico = CType(rm.GetObject("Paste"), System.Drawing.Icon)
        smi = New ImageMenuItem("貼り付け(&P)", _
                New EventHandler(AddressOf Menu_Click), Shortcut.CtrlV, ico.ToBitmap)
        emi.MenuItems.Add(smi)
        smi = New ImageMenuItem("-")
        emi.MenuItems.Add(smi)
        smi = New ImageMenuItem("削除(&D)", _
                New EventHandler(AddressOf Menu_Click), Shortcut.Del)
        emi.MenuItems.Add(smi)

    End Sub

    'メニューのClickイベントを一括して受け取るイベントモジュール
    Private Sub Menu_Click(ByVal sender As Object, ByVal e As EventArgs)

        'senderをImageMenuItemにキャスト
        Dim mi As ImageMenuItem = _
            DirectCast(sender, ImageMenuItem)

        'テキストで分岐し、個別のイベントを発生させる
        Select Case mi.Text
            Case "ファイル(&F)"
                RaiseEvent File_Click(sender, e)
            Case "新規作成(&N)"
                RaiseEvent FileNew_Click(sender, e)
            Case "開く(&O)..."
                RaiseEvent FileOpen_Click(sender, e)
            Case "閉じる(&C)"
                RaiseEvent FileClose_Click(sender, e)
            Case "上書き保存(&S)..."
                RaiseEvent FileOverSave_Click(sender, e)
            Case "名前を付けて保存(&A)..."
                RaiseEvent FileSave_Click(sender, e)
            Case "終了(&X)"
                RaiseEvent FileQuit_Click(sender, e)
            Case "編集(&E)"
                RaiseEvent Edit_Click(sender, e)
            Case "切り取り(&T)"
                RaiseEvent EditCut_Click(sender, e)
            Case "コピー(&C)"
                RaiseEvent EditCopy_Click(sender, e)
            Case "貼り付け(&P)"
                RaiseEvent EditPaste_Click(sender, e)
            Case "削除(&D)"
                RaiseEvent EditDelete_Click(sender, e)
        End Select

    End Sub

End Class
