Antivoyager Posted March 20 Share Posted March 20 I found this code a long time ago and i don't know hwo is the author: I made some changes and would like to make "Text Viewer" load text files from a folder and modify if need. First of all, I admit that I am not an expert in writing scripts and that is why I am looking for your help/advise here. So I would like to use the 'SAVE' button to save the edited/modified text file from _GUICtrlRichEdit_Create window. How do i make it to work, help me please ... expandcollapse popup#include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiMenu.au3> #include <GuiRichEdit.au3> Local $gui010 = GUICreate('Text Viewer', 600, 520) GUISetBkColor(0xE5E4E2) $SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25) Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE) _GUICtrlTreeView_SetTextColor($hTV, 0x000000) _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000) _GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC) Local $hImage = _GUIImageList_Create(16, 16, 5, 3) _GUIImageList_AddIcon($hImage, "shell32.dll", 3) _GUICtrlTreeView_SetNormalImageList($hTV, $hImage) $edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _ BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) local $FileChange = guictrlcreatedummy() local $SourcePath = @ScriptDir & "\" local $files = filefindfirstfile($SourcePath & '*.txt'), $file if $files = -1 then msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath) Exit endif while 1 $file = FileFindNextFile($files) if @error then exitloop _guictrltreeview_add($hTV,0,$file,0,0) wend GUISetState() GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") While 1 Switch GUIGetMsg() Case $gui_event_close Exit case $FileChange _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV)))) EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTV If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $TVN_SELCHANGEDW GUICtrlSendtodummy($FileChange) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc Link to comment Share on other sites More sharing options...
Andreik Posted March 21 Share Posted March 21 expandcollapse popup#include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiMenu.au3> #include <GuiRichEdit.au3> Local $gui010 = GUICreate('Text Viewer', 600, 520) GUISetBkColor(0xE5E4E2) $SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25) Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE) _GUICtrlTreeView_SetTextColor($hTV, 0x000000) _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000) _GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC) Local $hImage = _GUIImageList_Create(16, 16, 5, 3) _GUIImageList_AddIcon($hImage, "shell32.dll", 3) _GUICtrlTreeView_SetNormalImageList($hTV, $hImage) $edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _ BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) local $FileChange = guictrlcreatedummy() local $SourcePath = @ScriptDir & "\" local $files = filefindfirstfile($SourcePath & '*.txt'), $file if $files = -1 then msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath) Exit endif while 1 $file = FileFindNextFile($files) if @error then exitloop _guictrltreeview_add($hTV,0,$file,0,0) wend GUISetState() GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") While 1 Switch GUIGetMsg() Case $gui_event_close _GUICtrlRichEdit_Destroy($edt010) Exit case $FileChange _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV)))) Case $SaveButton $sPath = FileSaveDialog('Save file', @ScriptDir, 'Text (*.txt)') If $sPath Then FileWrite($sPath, _GUICtrlRichEdit_GetText($edt010)) EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTV If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $TVN_SELCHANGEDW GUICtrlSendtodummy($FileChange) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 Andreik Thanks! It works fine but i need another way to save txt files. Sorry, I didn't describe the problem in detail. I need just save the same file without FileSaveDialog and after saving just dialog message (something like "saved") Another question: is it possible to add an action to open a text file by double-clicking from the Treeview? Thank you ! Link to comment Share on other sites More sharing options...
Andreik Posted March 21 Share Posted March 21 If you know that save path then replace FileSaveDialog() with your file path. expandcollapse popup#include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiMenu.au3> #include <GuiRichEdit.au3> Local $gui010 = GUICreate('Text Viewer', 600, 520) GUISetBkColor(0xE5E4E2) $SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25) Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE) _GUICtrlTreeView_SetTextColor($hTV, 0x000000) _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000) _GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC) Local $hImage = _GUIImageList_Create(16, 16, 5, 3) _GUIImageList_AddIcon($hImage, "shell32.dll", 3) _GUICtrlTreeView_SetNormalImageList($hTV, $hImage) $edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _ BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) local $FileChange = guictrlcreatedummy() local $SourcePath = @ScriptDir & "\" local $files = filefindfirstfile($SourcePath & '*.txt'), $file if $files = -1 then msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath) Exit endif while 1 $file = FileFindNextFile($files) if @error then exitloop _guictrltreeview_add($hTV,0,$file,0,0) wend GUISetState() GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") While 1 Switch GUIGetMsg() Case $gui_event_close _GUICtrlRichEdit_Destroy($edt010) Exit case $FileChange _GUICtrlRichEdit_SetText($edt010, fileread($sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV)))) Case $SaveButton FileWrite("<Your path here>", _GUICtrlRichEdit_GetText($edt010)) EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTV If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_DBLCLK ShellExecute($SourcePath & _GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hTV))) Case $TVN_SELCHANGEDW GUICtrlSendtodummy($FileChange) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 1 hour ago, Andreik said: If you know that save path then replace FileSaveDialog() with your file path. Can't add there any path because i have more than 100 txt files in the same location with different names. I tried like this but it doesn't work FileWrite("$file", _GUICtrlRichEdit_GetText($edt010)) A while ago did exactly the same action using AMS with Input and ListBox text = Input.GetText("Input1"); tSelected = ListBox.GetSelected("ListBox1"); if tSelected then TextFile.WriteFromString(ListBox.GetItemData("ListBox1", tSelected[1]), text, false); end Dialog.TimedMessage("TXT FILE", "Text file has been saved!", 2000, MB_ICONNONE); and it worked well Using Autoit it seems to be more complicated. Thank you Anyway Link to comment Share on other sites More sharing options...
Nine Posted March 21 Share Posted March 21 32 minutes ago, Antivoyager said: Can't add there any path because i have more than 100 txt files in the same location with different names. That does not make much sense. In any language (AMS included), you absolutely need to specify a path and filename. Both can be implicit, but it has to be specified to Windows. Filename can be random, or based on date-time, or other method, but you need to provide a filename. Now when you go to that folder containing 100 txt files, what do you see as filename ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 12 minutes ago, Nine said: That does not make much sense. In any language (AMS included), you absolutely need to specify a path and filename. Both can be implicit, but it has to be specified to Windows. Filename can be random, or based on date-time, or other method, but you need to provide a filename. Now when you go to that folder containing 100 txt files, what do you see as filename ? Yes, I agree with you, it is necessary to assign a path and file name. However, how can I assign a *.txt file name to the code when I see hundreds of txt files in the folder, I can’t figure out. Link to comment Share on other sites More sharing options...
Andreik Posted March 21 Share Posted March 21 It's up to you to decide the path and the file name. If you don't really care about the file name you can generate a random file name and check if it not used already. But as @Nine said, it doesn't make any sense what are you talking. Tell us what do you really want to do. Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 (edited) 23 minutes ago, Andreik said: It's up to you to decide the path and the file name. If you don't really care about the file name you can generate a random file name and check if it not used already. But as @Nine said, it doesn't make any sense what are you talking. Tell us what do you really want to do. When you select some .txt file name from Treeview, add something and simple save changes from used file name. Edited March 21 by Antivoyager Link to comment Share on other sites More sharing options...
Andreik Posted March 21 Share Posted March 21 Do you want to overwrite the original file? Link to comment Share on other sites More sharing options...
argumentum Posted March 21 Share Posted March 21 (edited) 14 hours ago, Antivoyager said: I made some changes and would like to make "Text Viewer" load text files from a folder and modify if need. Notepad does that. Notepad++ is nicer to use. 1 hour ago, Antivoyager said: Can't add there any path because i have more than 100 txt files in the same location with different names. Save the old file adding a timestamp and save the new file with the original name to save/have both ? 1 hour ago, Antivoyager said: A while ago did exactly the same action using AMS what is AMS ? Would you give me a link ? Edit: you all type faster than me 😓 Edited March 21 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 Of cource overwrite Link to comment Share on other sites More sharing options...
Solution Andreik Posted March 21 Solution Share Posted March 21 expandcollapse popup#include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiMenu.au3> #include <GuiRichEdit.au3> Global $sPath Local $gui010 = GUICreate('Text Viewer', 600, 520) GUISetBkColor(0xE5E4E2) $SaveButton = GUICtrlCreateButton("SAVE",250,480,100,25) Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) Local $hTV = _GUICtrlTreeView_Create($gui010, 10, 10, 580, 300, $iStyle, $WS_EX_CLIENTEDGE) _GUICtrlTreeView_SetTextColor($hTV, 0x000000) _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000) _GUICtrlTreeView_SetBkColor($hTV, 0xEFEEEC) Local $hImage = _GUIImageList_Create(16, 16, 5, 3) _GUIImageList_AddIcon($hImage, "shell32.dll", 3) _GUICtrlTreeView_SetNormalImageList($hTV, $hImage) $edt010 = _GUICtrlRichEdit_Create($gui010, "", 10, 310, 580, 160, _ BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) local $FileChange = guictrlcreatedummy() local $SourcePath = @ScriptDir & "\" local $files = filefindfirstfile($SourcePath & '*.txt'), $file if $files = -1 then msgbox(0,'*** FileFindFirst ***','No files found for path = ' & $SourcePath) Exit endif while 1 $file = FileFindNextFile($files) if @error then exitloop _guictrltreeview_add($hTV,0,$file,0,0) wend GUISetState() GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") While 1 Switch GUIGetMsg() Case $gui_event_close _GUICtrlRichEdit_Destroy($edt010) Exit case $FileChange $sPath = $sourcepath & _guictrltreeview_gettext($hTV,_guictrltreeview_getselection($hTV)) _GUICtrlRichEdit_SetText($edt010, fileread($sPath)) Case $SaveButton Local $hFile = FileOpen($sPath, 2) FileWrite($hFile, _GUICtrlRichEdit_GetText($edt010)) FileClose($hFile) MsgBox(0, '', 'The file as been saved.') EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTV If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_DBLCLK ShellExecute($SourcePath & _GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hTV))) Case $TVN_SELCHANGEDW GUICtrlSendtodummy($FileChange) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc Antivoyager 1 Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 Andreik You did it exactly what i am looking for! Thank you very much ! Whish you all the best! Link to comment Share on other sites More sharing options...
argumentum Posted March 21 Share Posted March 21 ...what is AMS ?!! Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Antivoyager Posted March 21 Author Share Posted March 21 16 minutes ago, argumentum said: what is AMS ? Would you give me a link ? Autoplay Media Studio https://forums.indigorose.com argumentum 1 Link to comment Share on other sites More sharing options...
Andreik Posted March 21 Share Posted March 21 Took me a while to understand what are you trying to do but I'm glad it works as you expected. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now