Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDM_One = 500
%IDM_Two = 501
%IDM_Three = 502
%IDM_Exit = 503
Global hDlg, hContext, MenuValue As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_Popup Or %WS_ThickFrame To hDlg
Control Add Label, hDlg, 100, "Right-mouse click!", 10,10,100,20
CreateContextMenu
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x,y As Long
Select Case Cb.Msg
Case %WM_InitDialog
Setting_INI "get"
SetMenus_EnumeratedValues
Case %WM_Destroy
Setting_INI "save"
Case %WM_Command
Select Case Cb.Ctl
Case %IDM_One : MenuValue = 1 : SetMenus_EnumeratedValues
Case %IDM_Two : MenuValue = 2 : SetMenus_EnumeratedValues
Case %IDM_Three : MenuValue = 3 : SetMenus_EnumeratedValues
Case %IDM_Exit : Dialog End hDlg
End Select
Case %WM_ContextMenu
x = Lo(Integer,Cb.LParam) : y = Hi(Integer, Cb.LParam)
TrackPopupMenu hContext, %TPM_LeftAlign, x, y, 0, Cb.Hndl, ByVal 0
End Select
End Function
Sub CreateContextMenu
Menu New PopUp To hContext
Menu Add String, hContext, "One", %IDM_One, %MF_Enabled
Menu Add String, hContext, "Two", %IDM_Two, %MF_Enabled
Menu Add String, hContext, "Three", %IDM_Three, %MF_Enabled
Menu Add String, hContext, "Exit", %IDM_Exit, %MF_Enabled
End Sub
Sub SetMenus_EnumeratedValues
Local i,n as Long
'if values are 1-through-n
Menu Set State hContext, ByCmd %IDM_One, Choose(MenuValue, %MF_Checked, %MF_Enabled, %MF_Enabled)
Menu Set State hContext, ByCmd %IDM_Two, Choose(MenuValue, %MF_Enabled, %MF_Checked, %MF_Enabled)
Menu Set State hContext, ByCmd %IDM_Three, Choose(MenuValue, %MF_Enabled, %MF_Enabled, %MF_Checked)
For i = 1 To n
If i = MenuValue Then
Menu Set State hContext, ByCmd %IDM_Three, %MF_Checked 'checked
Else
Menu Set State hContext, ByCmd %IDM_Three, %MF_Enabled 'unchecked
End If
Next i
End Sub
Sub SetMenus_EnumeratedValues_Many
Local i,n As Long
'if there is a large number of menu items
For i = 1 To n
If i = MenuValue Then
Menu Set State hContext, ByCmd %IDM_Three, %MF_Checked 'checked
Else
Menu Set State hContext, ByCmd %IDM_Three, %MF_Enabled 'unchecked
End If
Next i
End Sub
Sub SetMenus_RandomValues
'if values are any numbers, not necessarily in sequence
Menu Set State hContext, ByCmd %IDM_One, %MF_Enabled 'unchecked
Menu Set State hContext, ByCmd %IDM_Two, %MF_Enabled 'unchecked
Menu Set State hContext, ByCmd %IDM_Three, %MF_Enabled 'unchecked
Select Case MenuValue
Case 1 : Menu Set State hContext, ByCmd %IDM_One, %MF_Checked 'checked
Case 2 : Menu Set State hContext, ByCmd %IDM_Two, %MF_Checked 'checked
Case 3 : Menu Set State hContext, ByCmd %IDM_Three, %MF_Checked 'checked
End Select
End Sub
Sub SetMenus_RandomValues_SequentialIDs
Local i As Long
'if values are any numbers, not necessarily in sequence & if ID values are in sequence
For i = %IDM_One To %IDM_Three
Menu Set State hContext, ByCmd i, %MF_Enabled 'unchecked
Next i
Select Case MenuValue
Case 1 : Menu Set State hContext, ByCmd %IDM_One, %MF_Checked 'checked
Case 2 : Menu Set State hContext, ByCmd %IDM_Two, %MF_Checked 'checked
Case 3 : Menu Set State hContext, ByCmd %IDM_Three, %MF_Checked 'checked
End Select
End Sub
Sub Setting_INI(Task$)
Local tempz, INIFileName As Asciiz*%Max_Path
INIFileName = EXE.Path$ + "app.ini"
If Task$ = "get" Then
GetPrivateProfileString "All", "MenuValue", "1", tempz, %Max_Path, INIFileName : MenuValue = Val(tempz)
ElseIf Task$ = "save" Then
WritePrivateProfileString "All", "MenuValue", (Str$(MenuValue)), INIFileName
End If
End Sub
'gbs_00727
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm