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
#Debug Error On 'catch array/pointer errors - OFF in production
#Debug Display On 'display untrapped errors - OFF in production
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%IDC_Toolbar = 501
%IDM_Sep = 601
%IDM_History = 603
'reserve next MaxHistory ID #'s
%IDT_Open = 700
Global hDlg,hToolbar,hPopupHistory As Dword
Global CFN, History() As String, MaxHistory As Long
Function PBMain() As Long
Dialog New Pixels, 0, "MRU Demonstration",500,300,500,200, %WS_OverlappedWindow To hDlg
Control Add Toolbar, hDlg, %IDC_Toolbar,"", 0,0,0,0, %TbStyle_Tooltips Or %TbStyle_Flat Or %WS_Border Or %WS_Child
Control Handle hDlg, %IDC_Toolbar To hToolbar
SendMessage hToolbar, %TB_SETEXTENDEDSTYLE, 0, %TBSTYLE_EX_DRAWDDARROWS
Toolbar Add Button hDlg, %IDC_Toolbar, 1, %IDT_Open, %TbStyle_Button Or %TbStyle_DropDown, "Open"
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local nmtb As TBNOTIFY Ptr, rc As RECT
Local P As ToolTipText Ptr, temp$, pt As Point, pLVDI As LV_DISPINFO Ptr
Select Case Cb.Msg
Case %WM_InitDialog
Settings_INI "get"
Menu New PopUp To hPopupHistory
ManageMRU
Case %WM_Command
Select Case Cb.Ctl
Case %IDT_Open
SelectCFN : ManageMRU
Case %IDM_History To %IDM_History + MaxHistory
CFN = History(Cb.Ctl - %IDM_History) : ManageMRU : ? PathName$(Namex,CFN)
End Select
Case %WM_Notify
Select Case Cb.NmId
Case %IDC_Toolbar
Select Case Cb.NmCode
Case %TBN_DropDown
nmtb = Cb.LParam
Select Case @nmtb.iItem
Case %IDT_Open
Call SendMessage(@nmtb.hdr.hwndFrom, %TB_GETRECT, @nmtb.iItem, VarPtr(rc))
Call MapWindowPoints(@nmtb.hdr.hwndFrom, %HWND_Desktop, ByVal VarPtr(rc), 2)
Call TrackPopupMenu (hPopUpHistory, 0, rc.nLeft, rc.nBottom, 0, CbHndl, ByVal %Null)
End Select
End Select
End Select
Case %WM_Destroy
Settings_INI "save"
End Select
End Function
Sub SelectCFN
Local title$, startfolder$, filter$, startfile$, defaultext$, flags&, filevar$, countvar&
title$ = "Open File"
filter$ = "All Files" + $Nul + "*.*" + $Nul
startfolder$ = CFN
startfile$ = CFN
defaultext$ = ""
flags& = %OFN_Explorer Or %OFN_FileMustExist Or %OFN_HideReadOnly
Display Openfile hDlg, 100, 100, title$, startfolder$, filter$, startfile$, defaultext$, flags& To filevar$, countvar&
If Len(filevar$) Then CFN = LCase$(filevar$)
End Sub
Sub Settings_INI(Task$)
Local i,x,y As Long, xResult, yResult, tempz, INIFileName As Asciiz*%Max_Path
'defines INI file name
INIFileName = EXE.Path$ + EXE.Name$ + ".ini"
If Task$ = "get" Then
'get dialog top/left from INI file and use to set Dialog location
GetPrivateProfileString "All", "Left", "300", xResult, %Max_Path, INIFileName
GetPrivateProfileString "All", "Top", "300", yResult, %Max_Path, INIFileName
Dialog Set Loc hDlg, Val(xResult$), Val(yResult$) 'left/top
'get dialog width/height from INI file and use to set Dialog size
GetPrivateProfileString "All", "Width", "300", xResult, %Max_Path, INIFileName
GetPrivateProfileString "All", "Height", "200", yResult, %Max_Path, INIFileName
Dialog Set Size hDlg,Val(xResult$), Val(yResult$) 'width/height
'get value for numeric variable
GetPrivateProfileString "All", "MaxHistory", "4", tempz, %Max_Path, INIFileName : MaxHistory = Val(tempz)
ReDim History(MaxHistory)
'get value for string variables
For i = 0 To MaxHistory
GetPrivateProfileString "All", "History"+Trim$(Str$(i)), "", tempz, %Max_Path, INIFileName : History(i) = tempz
Next i
GetPrivateProfileString "All", "CFN" , "", tempz, %Max_Path, INIFileName : CFN = tempz
ElseIf Task$ = "save" Then
If IsFile(INIFileName) Then Kill INIFileName
'save dialog size/location unless minimized or maximized
If IsFalse(IsIconic(hDlg) Or IsZoomed(hDlg)) Then
Dialog Get Loc hDlg To x,y
WritePrivateProfileString "All", "Left", Str$(x), INIFileName
WritePrivateProfileString "All", "Top", Str$(y), INIFileName
Dialog Get Size hDlg To x,y
WritePrivateProfileString "All", "Width", Str$(x), INIFileName
WritePrivateProfileString "All", "Height", Str$(y), INIFileName
End If
'save numeric variable
WritePrivateProfileString "All", "MaxHistory", (Str$(MaxHistory)), INIFileName
'save string variables
WritePrivateProfileString "All", "CFN", (CFN), INIFileName
For i = 0 To UBound(History)
WritePrivateProfileString "All", "History"+Trim$(Str$(i)), (History(i)), INIFileName
Next i
End If
End Sub
Sub ManageMRU
Local i, Flag As Long, temp$
For i = %IDM_History To %IDM_History + MaxHistory : Menu Delete hPopupHistory, ByCmd i : Next i 'remove menu items
For i = 0 To UBound(History)
If CFN = History(i) Then Array Delete History(i), CFN : Flag = 1 : Exit For 'remove match, if found, add CFN to end
Next i
If Flag = 0 Then Array Delete History(0), CFN
For i = MaxHistory To 0 Step - 1 'display in reverse order
If Len(Dir$(History(i))) Then
Menu Add String, hPopupHistory, PathName$(Namex,History(i)), %IDM_History+i, %MF_Enabled
End If
Next i
End Sub
'gbs_00748
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm