Advertisement
SharkyEXE

Untitled

Nov 24th, 2022
1,740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EnableExplicit
  2.  
  3. Structure STRUC_DirInfo
  4.   sName.s
  5.   sFullName.s
  6. EndStructure
  7.  
  8. Structure opcode
  9.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  10.     mov.u
  11.   CompilerElse
  12.     mov.a
  13.   CompilerEndIf
  14.   addr.i
  15.   push.a
  16.   ret.a
  17. EndStructure
  18.  
  19. Structure hookstruct
  20.   addr.i
  21.   hook.opcode
  22.   orig.a[SizeOf(opcode)]
  23. EndStructure
  24.  
  25. Procedure Hook(*OldFunctionAddress, *NewFunctionAddress)
  26.   Protected *hook_ptr.hookstruct
  27.  
  28.   If *OldFunctionAddress = 0 Or *NewFunctionAddress = 0
  29.     ProcedureReturn #Null
  30.   EndIf
  31.  
  32.   *hook_ptr = AllocateMemory(SizeOf(hookstruct), #PB_Memory_NoClear)
  33.   *hook_ptr\addr = *OldFunctionAddress
  34.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  35.     *hook_ptr\hook\mov = $B848
  36.   CompilerElse
  37.     *hook_ptr\hook\mov = $B8
  38.   CompilerEndIf
  39.   *hook_ptr\hook\addr = *NewFunctionAddress
  40.   *hook_ptr\hook\push = $50
  41.   *hook_ptr\hook\ret = $C3
  42.  
  43.   CopyMemory(*OldFunctionAddress, @*hook_ptr\orig, SizeOf(opcode))
  44.   If WriteProcessMemory_(GetCurrentProcess_(), *OldFunctionAddress, @*hook_ptr\hook, SizeOf(opcode), #Null)=0
  45.     FreeMemory(*hook_ptr)
  46.     ProcedureReturn #Null
  47.   Else
  48.     ProcedureReturn *hook_ptr
  49.   EndIf
  50. EndProcedure
  51.  
  52. Procedure UnHook(*hook_ptr.hookstruct)
  53.   Protected retValue.i
  54.  
  55.   If *hook_ptr
  56.     If *hook_ptr\addr
  57.       If WriteProcessMemory_(GetCurrentProcess_(), *hook_ptr\addr, @*hook_ptr\orig, SizeOf(opcode), #Null)
  58.         retValue = *hook_ptr\addr
  59.         FreeMemory(*hook_ptr)
  60.         ProcedureReturn retValue
  61.       EndIf
  62.     EndIf
  63.   EndIf
  64.  
  65.   ProcedureReturn #Null
  66. EndProcedure
  67.  
  68. Procedure Block_FindFirstChangeNotification(lpPathName, bWatchSubtree, dwNotifyFilter.l)
  69.   ProcedureReturn #INVALID_HANDLE_VALUE
  70. EndProcedure
  71.  
  72. Procedure Event_ResizeWindow()
  73.   Protected Window = EventWindow()
  74.   Protected exlFiles = GetWindowData(Window)
  75.   SetGadgetItemAttribute(exlFiles, 0, #PB_Explorer_ColumnWidth, GadgetWidth(exlFiles) - 25)
  76. EndProcedure
  77.  
  78. Procedure FillDirList(Gadget, IconImg, sParentDir.s, List Dirs.STRUC_DirInfo())
  79.   Protected Dir, i, MaxIndex, ItemIndex, sCurDirPart.s, sDirPart.s, sName.s, Count, lvi.LV_ITEM
  80.  
  81.   If sParentDir And IsGadget(Gadget) And GadgetType(Gadget) = #PB_GadgetType_ListIcon
  82.     If Right(sParentDir, 1) <> "\" : sParentDir + "\" : EndIf
  83.     ClearList(Dirs())
  84.     SendMessage_(GadgetID(Gadget), #WM_SETREDRAW, 0, 0)
  85.     ClearGadgetItems(Gadget)
  86.     lvi\mask = #LVIF_INDENT
  87.    
  88.     MaxIndex = CountString(sParentDir, "\") - 1
  89.     For i = 0 To MaxIndex
  90.       sDirPart = StringField(sParentDir, i + 1, "\")
  91.       sCurDirPart + sDirPart + "\"
  92.       If AddElement(Dirs())
  93.         Dirs()\sFullName = sCurDirPart
  94.         If i = 0
  95.           AddGadgetItem(Gadget, -1, sCurDirPart, ImageID(IconImg))
  96.         Else
  97.           AddGadgetItem(Gadget, -1, sDirPart, ImageID(IconImg))
  98.         EndIf
  99.         lvi\iItem = i
  100.         SendMessage_(GadgetID(Gadget), #LVM_GETITEM, 0, lvi)
  101.         lvi\iIndent = i
  102.         SendMessage_(GadgetID(Gadget), #LVM_SETITEM, 0, lvi)
  103.       EndIf
  104.     Next
  105.     ;Get the child directories.
  106.     Dir = ExamineDirectory(#PB_Any, sParentDir, "*.*")
  107.     If Dir
  108.       While NextDirectoryEntry(Dir)
  109.         If DirectoryEntryType(Dir) = #PB_DirectoryEntry_Directory
  110.           sName = DirectoryEntryName(Dir)
  111.           If sName <> "." And sName <> ".."
  112.             If AddElement(Dirs())
  113.               Dirs()\sName = sName
  114.               Dirs()\sFullName = sParentDir + sName + "\"
  115.               Count + 1
  116.             EndIf
  117.           EndIf
  118.         EndIf
  119.       Wend
  120.       FinishDirectory(Dir)
  121.      
  122.       If Count > 0
  123.         i = MaxIndex + 1
  124.         Count = ListSize(Dirs()) - 1
  125.         SortStructuredList(Dirs(), #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(STRUC_DirInfo\sName), #PB_String, i, Count)
  126.         SelectElement(Dirs(), i)
  127.         Repeat
  128.           AddGadgetItem(Gadget, -1, Dirs()\sName, ImageID(IconImg))
  129.           lvi\iItem = i
  130.           SendMessage_(GadgetID(Gadget), #LVM_GETITEM, 0, lvi)
  131.           lvi\iIndent = MaxIndex + 1
  132.           SendMessage_(GadgetID(Gadget), #LVM_SETITEM, 0, lvi)
  133.           i + 1
  134.         Until NextElement(Dirs()) = 0
  135.       EndIf
  136.     EndIf
  137.     SetGadgetState(Gadget, MaxIndex)    ;Select the last directory item of "sParentDir" var.
  138.     SendMessage_(GadgetID(Gadget), #WM_SETREDRAW, 1, 0)
  139.     RedrawWindow_(GadgetID(Gadget), 0, 0, #RDW_ERASE | #RDW_INVALIDATE | #RDW_UPDATENOW)
  140.   EndIf
  141. EndProcedure
  142.  
  143. Procedure WndProc_ExplorerList(hWnd, uMsg, wParam, lParam)
  144.   Protected Gadget, old = GetProp_(hWnd, "OldWndProc")
  145.   Static IsRedrawingBlocked
  146.  
  147.   If uMsg = #WM_SETREDRAW
  148.     IsRedrawingBlocked = 1 - wParam
  149.    
  150.   ElseIf uMsg = #WM_ERASEBKGND
  151.     If IsRedrawingBlocked
  152.       Gadget = GetProp_(hWnd, "PB_ID")
  153.       If IsGadget(Gadget)
  154.         PostEvent(#PB_Event_Gadget, 0, Gadget, #PB_EventType_FirstCustomValue)
  155.       EndIf
  156.     Else
  157.       ProcedureReturn 1
  158.     EndIf
  159.    
  160.   ElseIf uMsg = #WM_NCDESTROY
  161.     RemoveProp_(hWnd, "OldWndProc")
  162.   EndIf
  163.   ProcedureReturn CallWindowProc_(old, hWnd, uMsg, wParam, lParam)
  164. EndProcedure
  165.  
  166. Procedure.s OpenSelectDirWindow(sInitDir.s, ParentWindow)
  167.   Protected Event, EventType, sXML.s, Xml, Dialog, Window, hWndMain, Img, sCurDir.s, sPrevDrive.s, sResult.s
  168.   Protected strPath, exlFiles, excDrive, liFolders, btnOK, btnCancel, hLiFolders, sfi.SHFILEINFO, Index
  169.   Protected *FindFirstChangeNotificationW, *FindFirstChangeNotificationA, LibKernel32
  170.   Protected NewList Dirs.STRUC_DirInfo()
  171.   sXML = "<dialogs>" +
  172.          "  <window name='SelDir' text='Select Directory' minwidth='330' minheight='350' flags='#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_Invisible'>" +
  173.          "    <gridbox columns='2' colexpand='yes' rowexpand='item:3'>" +
  174.          "      <text name='txtFolder' text='Folder name:' colspan='2' height='10'/>" +
  175.          "      <string name='strPath' colspan='2' flags='#PB_String_ReadOnly' height='10'/>" +
  176.          "      <listicon name='liFolders' text='Folders' width='150' flags='#PB_ListIcon_AlwaysShowSelection'/>" +
  177.          "      <vbox expand='item:2'>" +
  178.          "        <text name='txtType' text='File type: *.*' height='10'/>" +
  179.          "        <explorerlist name='exlFiles' height='150' flags='#PB_Explorer_FullRowSelect | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange | #PB_Explorer_NoDriveRequester | #PB_Explorer_NoMyDocuments | #PB_Explorer_AutoSort'/>" +
  180.          "        <text name='txtDrive' text='Drives:' height='10'/>" +
  181.          "        <explorercombo name='excDrive' height='22' flags='#PB_Explorer_DrivesOnly | #PB_Explorer_NoMyDocuments'/>" +
  182.          "      </vbox>" +
  183.          "      <empty/>" +
  184.          "      <singlebox expand='no' align='right' margin='left:0,right:0,horizontal:0'>" +
  185.          "        <hbox expand='equal' spacing='8' height='13'>" +
  186.          "          <button name='btnOK' text='OK' width='75' height='12'/>" +
  187.          "          <button name='btnCancel' text='Cancel' width='75' height='12'/>" +
  188.          "        </hbox>" +
  189.          "      </singlebox>" +
  190.          "    </gridbox>" +
  191.          "  </window>" +
  192.          "</dialogs>"
  193.   If sInitDir = "" : sInitDir = "C:\" : EndIf
  194.   If Right(sInitDir, 1) <> "\" : sInitDir + "\" : EndIf
  195.   If ParentWindow <> -1 : DisableWindow(ParentWindow, 1) : EndIf
  196.    
  197.   Xml = ParseXML(#PB_Any, sXML)
  198.   If Xml And XMLStatus(Xml) = #PB_XML_Success
  199.     Dialog = CreateDialog(#PB_Any)
  200.     If Dialog And OpenXMLDialog(Dialog, Xml, "SelDir", 0, 0, 500, 450)
  201.       strPath = DialogGadget(Dialog, "strPath")
  202.       btnOK = DialogGadget(Dialog, "btnOK")
  203.       btnCancel = DialogGadget(Dialog, "btnCancel")
  204.       exlFiles = DialogGadget(Dialog, "exlFiles")
  205.       excDrive = DialogGadget(Dialog, "excDrive")
  206.       liFolders = DialogGadget(Dialog, "liFolders")
  207.      
  208.       ;Remove the window icon.
  209.       Window = DialogWindow(Dialog)
  210.       hWndMain = WindowID(Window)
  211.       SendMessage_(hWndMain, #WM_SETICON, 0, 0)   ;for the old version of Windows.
  212.       SetWindowLongPtr_(hWndMain, #GWL_STYLE, GetWindowLong_(hWndMain, #GWL_STYLE) | #WS_CLIPCHILDREN)
  213.       SetWindowLongPtr_(hWndMain, #GWL_EXSTYLE, GetWindowLong_(hWndMain, #GWL_EXSTYLE) | #WS_EX_DLGMODALFRAME)
  214.       SetWindowData(Window, exlFiles)
  215.      
  216.       hLiFolders = GadgetID(liFolders)
  217.       SetWindowLongPtr_(hLiFolders, #GWL_STYLE, GetWindowLongPtr_(hLiFolders, #GWL_STYLE) | #LVS_NOSORTHEADER);#LVS_NOCOLUMNHEADER)
  218.       SetWindowPos_(hLiFolders, 0, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE | #SWP_NOZORDER | #SWP_FRAMECHANGED)
  219.      
  220.       For Index = 1 To 3
  221.         RemoveGadgetColumn(exlFiles, 1)
  222.       Next
  223.      
  224.       ;Get a directory icon.
  225.       Img = CreateImage(#PB_Any, 16, 16, 32, #PB_Image_Transparent)
  226.       If Img
  227.         If SHGetFileInfo_(GetTemporaryDirectory(), 0, sfi, SizeOf(SHFILEINFO), #SHGFI_ICON | #SHGFI_SMALLICON)
  228.           If StartDrawing(ImageOutput(Img))
  229.             DrawImage(sfi\hIcon, 0, 0)
  230.             StopDrawing()
  231.           EndIf
  232.           DestroyIcon_(sfi\hIcon)
  233.         EndIf
  234.       EndIf
  235.      
  236.       ;Block automatic updates of the ExplorerList gadget.
  237.       LibKernel32 = OpenLibrary(#PB_Any, "Kernel32.dll")
  238.       If LibKernel32
  239.         *FindFirstChangeNotificationW = Hook(GetFunction(LibKernel32, "FindFirstChangeNotificationW"), @Block_FindFirstChangeNotification())
  240.         *FindFirstChangeNotificationA = Hook(GetFunction(LibKernel32, "FindFirstChangeNotificationA"), @Block_FindFirstChangeNotification())
  241.       EndIf
  242.      
  243.       SetProp_(GadgetID(exlFiles), "OldWndProc", SetWindowLongPtr_(GadgetID(exlFiles), #GWLP_WNDPROC, @WndProc_ExplorerList()))
  244.      
  245.       ;Set the initial directory.
  246.       If FileSize(sInitDir) = -2
  247.         SetGadgetText(excDrive, sInitDir)
  248.         sPrevDrive = GetGadgetText(excDrive)
  249.         FillDirList(liFolders, Img, sInitDir, Dirs())
  250.         SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 0, 0)
  251.         SetGadgetText(exlFiles, sInitDir)
  252.         If Right(sInitDir, 2) <> ":\"
  253.           sInitDir = RTrim(sInitDir, "\")
  254.         EndIf
  255.         SetGadgetText(strPath, sInitDir)
  256.       EndIf
  257.      
  258.       BindEvent(#PB_Event_SizeWindow, @Event_ResizeWindow(), Window)
  259.       HideWindow(Window, 0)
  260.      
  261.       Repeat
  262.         Event = WaitWindowEvent()
  263.         If Event = #PB_Event_ActivateWindow
  264.           RedrawWindow_(hWndMain, 0, 0, #RDW_INVALIDATE | #RDW_UPDATENOW | #RDW_ERASE | #RDW_INTERNALPAINT | #RDW_ALLCHILDREN)
  265.          
  266.         ElseIf Event = #PB_Event_Gadget
  267.           EventType = EventType()
  268.           Select EventGadget()
  269.             Case excDrive
  270.               sCurDir = GetGadgetText(excDrive)
  271.               If sPrevDrive <> sCurDir       ;The ExplorerCombo gadget has no #PB_EventType_Change event.
  272.                 SetGadgetText(strPath, sCurDir)
  273.                 FillDirList(liFolders, Img, sCurDir, Dirs())
  274.                 SetGadgetText(exlFiles, sCurDir)
  275.                 sPrevDrive = sCurDir
  276.               EndIf
  277.              
  278.             Case exlFiles
  279.               If EventType = #PB_EventType_FirstCustomValue
  280.                 SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 1, 0)
  281.                 RedrawWindow_(GadgetID(exlFiles), 0, 0, #RDW_INVALIDATE | #RDW_UPDATENOW | #RDW_NOERASE | #RDW_INTERNALPAINT)
  282.               EndIf
  283.              
  284.             Case liFolders
  285.               If EventType = #PB_EventType_LeftDoubleClick
  286.                 Index = GetGadgetState(liFolders)
  287.                 If Index >= 0
  288.                   SelectElement(Dirs(), Index)
  289.                   sCurDir = Dirs()\sFullName
  290.                   FillDirList(liFolders, Img, sCurDir, Dirs())
  291.                   SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 0, 0)
  292.                   SetGadgetText(exlFiles, sCurDir)
  293.                   If Right(sCurDir, 2) <> ":\"
  294.                     sCurDir = RTrim(sCurDir, "\")
  295.                   EndIf
  296.                   SetGadgetText(strPath, sCurDir)
  297.                 EndIf
  298.               EndIf
  299.              
  300.             Case btnOK
  301.               sResult = GetGadgetText(strPath)
  302.               Break
  303.             Case btnCancel
  304.               Break
  305.           EndSelect
  306.         EndIf
  307.       Until Event = #PB_Event_CloseWindow And EventWindow() = Window
  308.      
  309.       If LibKernel32
  310.         UnHook(*FindFirstChangeNotificationW)
  311.         UnHook(*FindFirstChangeNotificationA)
  312.         CloseLibrary(LibKernel32)
  313.       EndIf
  314.      
  315.       CloseWindow(Window)
  316.      
  317.       While WindowEvent() : Wend
  318.     EndIf
  319.   EndIf
  320.   If ParentWindow <> -1 : DisableWindow(ParentWindow, 0) : SetActiveWindow(ParentWindow) : EndIf
  321.   ProcedureReturn sResult
  322. EndProcedure
  323.  
  324. Define e
  325. If OpenWindow(0, 0, 0, 222, 200, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  326.   ButtonGadget(0, 10, 10, 160, 23, "Show window")
  327.  
  328.   Repeat
  329.     e = WaitWindowEvent()
  330.     If e = #PB_Event_Gadget And EventGadget() = 0
  331.       Debug OpenSelectDirWindow("C:\windows\", 0)
  332.     EndIf
  333.   Until e = #PB_Event_CloseWindow
  334. EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement