Skip to content

Instantly share code, notes, and snippets.

@tienloc1
Created October 6, 2022 19:02
Show Gist options
  • Save tienloc1/91ace4e010737d6e1bb49143bef07ab7 to your computer and use it in GitHub Desktop.
Save tienloc1/91ace4e010737d6e1bb49143bef07ab7 to your computer and use it in GitHub Desktop.
autoit - notepad lesson
#cs ----------------------------------------------------------------------------
Learn AutoIT - by JUNO_OKYO
From J2TEAM with love!!!
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
; GUI - Giao diện đồ họa người dùng (Graphic User Interface)
#include <File.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 636, 490, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
Global $MenuItem1 = GUICtrlCreateMenu("&File")
Global $MenuItem2 = GUICtrlCreateMenuItem("New"&@TAB&"Ctrl+N", $MenuItem1)
Global $MenuItem3 = GUICtrlCreateMenuItem("Open..."&@TAB&"Ctrl+O", $MenuItem1)
Global $MenuItem4 = GUICtrlCreateMenuItem("Save"&@TAB&"Ctrl+S", $MenuItem1)
Global $MenuItem5 = GUICtrlCreateMenuItem("", $MenuItem1)
Global $MenuItem6 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
Global $MenuItem7 = GUICtrlCreateMenu("&Help")
Global $MenuItem8 = GUICtrlCreateMenuItem("About Notepad", $MenuItem7)
Global $Edit1 = GUICtrlCreateEdit("", -1, -1, 635, 500)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
Global $Form1_AccelTable[2][2] = [["^!p", $MenuItem2], ["{CAPS LOCK}", $MenuItem2]]
GUISetAccelerators($Form1_AccelTable )
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $MenuItem2 ;New
; Nếu đang có nội dung, thì hỏi người dùng có muốn lưu không
; Không có nội dung, thì không có gì xảy ra
saveFile(True);
Case $MenuItem3 ;Open
Local $fp = FileSaveDialog('Save', @ScriptDir, 'Text file (*.txt)|All (*.*)', 1, '', $Form1);
If $fp Then
$fp2 = FileOpen($fp, 128);
Local $data = FileRead($fp2);
FileClose($fp2);
GUICtrlSetData($Edit1, $data);
; Đổi tiêu đề form
updateTitle($fp);
EndIf
Case $MenuItem4 ;Save
; Kiểm tra xem người dùng đã nhập gì chưa
; Nếu nhập rồi thì mới lưu
Local $fp = saveFile();
updateTitle($fp);
Case $MenuItem6 ;Exit
Exit
Case $MenuItem8 ;About
MsgBox(64+262144, 'About', 'Developed by JUNO_OKYO');
EndSwitch
WEnd
Func saveFile($resetForm = False);
Local $content = GUICtrlRead($Edit1);
If $content Then
Local $fp = FileSaveDialog('Save', @ScriptDir, 'Text file (*.txt)|All (*.*)', 2 + 16, '', $Form1);
If $fp Then
FileWrite($fp, $content);
FileClose($fp);
If $resetForm Then
GUICtrlSetData($Edit1, '');
EndIf
Return $fp;
EndIf
EndIf
EndFunc
Func updateTitle($fp)
Local $drive, $dir, $fileName, $ext
Local $arr = _PathSplit($fp, $drive, $dir, $fileName, $ext);
WinSetTitle($Form1, '', $fileName & $ext & ' - Notepad');
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment