閉じる「×」ボタンの無効化とメニューチェック          <TOP>


GetSystemMenu システムメニューのハンドルを取得

GetMenuItemInfo メニュー項目に関する情報を取得

SetMenuItemInfo メニュー項目に関する情報を変更

SendMessage ウィンドウにメッセージを送信

 

参照

http://support.microsoft.com/kb/184686/ja

 

'================================================================
'= 閉じる「×」ボタンの無効化とメニューチェック
'=    (SetmenuItemInfo.bas)
'================================================================
#include "Windows.bi"

#define SC_CLOSE &HF060                 '閉じる
#define MIIM_STATE &H1                  'fStateメンバをセット
#define MIIM_ID &H2                     'wIDメンバをセット
#define MFS_GRAYED &H3
#define MFS_CHECKED &H8
#define WM_NCACTIVATE &H86

Type MENUITEMINFO
    cbSize        As Long               '構造体のバイト数
    fMask         As Long               '取得する情報を指定する定数の組み合わせ
    fType         As Long               'メニュー項目のタイプを指定する定数の組み合わせ
    fState        As Long               'メニューの状態を指定する定数の組み合わせ
    wID           As Long               'ユーザー定義のメニュー項目のID
    hSubMenu      As Long               '指定のメニュー項目と関連するサブメニューのハンドル
    hbmpChecked   As Long               'チェックマーク用のビットマップのハンドル
    hbmpUnchecked As Long               '未チェック時のときのビットマップハンドル
    dwItemData    As Long               'メニュー項目と関連するユーザー定義の値
    dwTypeData    As Long               'メニュー項目のタイプ(fMaskにMIIM_Typeを指定したときのみ有効)
    cch           As Long               'メニュー項目のテキストのバイト数
End Type

' システムメニューのハンドル取得
Declare Function Api_GetSystemMenu& Lib "user32" Alias "GetSystemMenu" (ByVal hWnd&, ByVal bRevert&)

' メニュー項目に関する情報を取得
Declare Function Api_GetMenuItemInfo& Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu&, ByVal uItem&, ByVal fByPosition&, lpMInfo As MENUITEMINFO)

' メニュー項目に関する情報を変更
Declare Function Api_SetMenuItemInfo& Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu&, ByVal uItem&, ByVal fByPosition&, lpMInfo As MENUITEMINFO)

' ウィンドウにメッセージを送信。この関数は、指定したウィンドウのウィンドウプロシージャが処理を終了するまで制御を返さない
Declare Function Api_SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hWnd&, ByVal wMsg&, ByVal wParam&, lParam As Any)

#define xSC_CLOSE -10
#define SwapID 1
#define ResetID 2

Var Shared Button(2) As Object
For i = 0 To 2
    Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i + 1)))
    Button(i).SetFontSize 14
Next    

Var Shared hMenu As Long
Var Shared mii As MENUITEMINFO

'================================================================
'=
'================================================================
Declare Function SetId(Action As Long) As Long
Function SetId(Action As Long) As Long
    Var MenuID As Long
    Var Ret As Long

    MenuID = mii.wID

    If mii.fState = (mii.fState Or MFS_GRAYED) Then
        If Action = SwapID Then
            mii.wID = SC_CLOSE
        Else
            mii.wID = xSC_CLOSE
        End If
    Else
        If Action = SwapID Then
            mii.wID = xSC_CLOSE
        Else
            mii.wID = SC_CLOSE
        End If
    End If

    mii.fMask = MIIM_ID
    Ret = Api_SetMenuItemInfo(hMenu, MenuID, False, mii)
    
    If Ret = 0 Then
        mii.wID = MenuID
    End If

    SetId = Ret
End Function

'================================================================
'=
'================================================================
Declare Sub SetButtons()
Sub SetButtons()
    If mii.fState = (mii.fState Or MFS_GRAYED) Then
        Button(0).SetWindowtext "有効に..."
    Else
        Button(0).SetWindowtext "無効に..."
    End If
    
    If mii.fState = (mii.fState Or MFS_CHECKED) Then
        Button(1).SetWindowtext "チェックなし"
    Else
        Button(1).SetWindowtext "チェック"
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Ret As Long

    hMenu = Api_GetSystemMenu(GethWnd, 0)
    mii.cbSize = Len(MII)
    mii.dwTypeData = StrAdr(String$(80, Chr$(0)))
    mii.cch = Len(mii.dwTypeData)
    mii.fMask = MIIM_STATE
    mii.wID = SC_CLOSE
    Ret = Api_GetMenuItemInfo(hMenu, mii.wID, False, mii)
    SetButtons
    Button(2).SetWindowtext "終了"
End Sub

'================================================================
'= 「×」ボタンの有効・無効
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Ret As Long

    Ret = SetId(SwapID)

    If Ret <> 0 Then
        If mii.fState = (mii.fState Or MFS_GRAYED) Then
            mii.fState = mii.fState - MFS_GRAYED
        Else
            mii.fState = (mii.fState Or MFS_GRAYED)
        End If

        mii.fMask = MIIM_STATE
        
        Ret = Api_SetMenuItemInfo(hMenu, mii.wID, False, mii)
        
        If Ret = 0 Then
            Ret = SetId(ResetID)
        End If

        Ret = Api_SendMessage(GethWnd, WM_NCACTIVATE, True, 0)
        SetButtons
    End If
End Sub

'================================================================
'= 閉じる「×」ボタンの無効化とチェック
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Var Ret As Long

    If mii.fState = (mii.fState Or MFS_CHECKED) Then
        mii.fState = mii.fState - MFS_CHECKED
    Else
        mii.fState = (mii.fState Or MFS_CHECKED)
    End If

    mii.fMask = MIIM_STATE
    
    Ret = Api_SetMenuItemInfo(hMenu, mii.wID, False, mii)
    SetButtons
End Sub

'================================================================
'=
'================================================================
Declare Sub Button3_on edecl ()
Sub Button3_on()
    End
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End