● チェックを黒ポチに変える ●

ちょっとオシャレかも?

Public Type MENUITEMINFO
    cbSize As Long
    fMask As Long
    fType As Long
    fState As Long
    wID As Long
    hSubMenu As Long
    hbmpChecked As Long
    hbmpUnchecked As Long
    dwItemData As Long
    dwTypeData As String
    cch As Long
End Type

'メニューのハンドルを得る
Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long

'サブメニューのハンドルを取得する
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

'メニューに関する情報を設定する
Public Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long

Public Const MFT_RADIOCHECK = &H200&
Public Const MIIM_TYPE = &H10

'-----------------------------------------------------------------------
' 関数名 : SetCheckMenu
' 機能   : メニューを黒ポチに変更する
' 引数   : (in) Menu … メニュー
'           (in) MnuParentPos … 親メニュー位置
'           (in) MnuChildPos … 子メニュー位置
' 戻り値 : なし
'-----------------------------------------------------------------------
Public Sub SetCheckMenu(ByVal Menu As Menu, ByVal MnuParentPos As Long, _
                        ByVal MnuChildPos As Long)

    Dim udtMnuInfo As MENUITEMINFO
    Dim hParentMenu As Long
    Dim hChildMenu As Long

    '親メニューのハンドルを取得
    hParentMenu = GetMenu(Menu.Parent.hwnd)

    '子メニューのハンドルを取得
    '第2引数には親メニューの位置を指定する
    '左から1番目であったら 0、3番目であったら 2 となる
    hChildMenu = GetSubMenu(hParentMenu, MnuParentPos)

    'MENUITEMINFOの各種設定
    With udtMnuInfo
        .cbSize = Len(udtMnuInfo)
        .fType = MFT_RADIOCHECK
        .fMask = MIIM_TYPE
        .dwTypeData = Menu.Caption & Chr$(0)
    End With

    'メニューのチェックを黒ポチに変える
    Call SetMenuItemInfo(hChildMenu, MnuChildPos, True, udtMnuInfo)

End Sub

1.んじゃ、以下のようにメニューを作成しよう

 
 キャプション(P 名前(M インデックス(X チェック(C
親メニュー
mnuMain
なし
False
子メニュー1
mnuChild
0
True
子メニュー2
mnuChild
1
False
子メニュー3
mnuChild
2
False

2.んで、以下をコーディング

 
Private Sub Form_Load()

    Call SetCheckMenu(mnuChild(0), 0, 0)
    Call SetCheckMenu(mnuChild(1), 0, 1)
    Call SetCheckMenu(mnuChild(2), 0, 2)

End Sub

Private Sub mnuChild_Click(Index As Integer)

    Static preIndex As Long

    mnuChild(preIndex).Checked = False
    mnuChild(Index).Checked = True
    preIndex = Index

End Sub


戻る