#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=StickyNotes.au3.ico #AutoIt3Wrapper_UseUpx=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include #include #include #include #include #include #include #include <_Color.au3> #include #include <_Misc.au3> #include #include ; Main script start If Not _MySingleton('therks AutoIt StickyNotes', 1) Then ControlSetText('StickyNotes Parent', '', 1, 'restore') $iMsgBox = MsgBox(0x42144, 'StickyNotes', 'StickyNotes is already running.' & @CRLF & 'Would you like to close the program?', 10) If $iMsgBox = 6 Then ControlSetText('StickyNotes Parent', '', 1, 'exit') Exit EndIf Global $sSaveData = @AppDataDir & '\StickyNotesData.txt' If $CmdLine[0] Then $hTestWrite = FileOpen($CmdLine[1], 1) If $hTestWrite <> -1 Then FileClose($hTestWrite) $sSaveData = $CmdLine[1] EndIf EndIf Opt('GUIOnEventMode', 1) Opt('TrayOnEventMode', 1) Opt('TrayAutoPause', 1) Opt('TrayMenuMode', 1+2) Global $DEBUG = 1 If @Compiled Then $DEBUG = 0 Global Const $_WM_EXITSIZEMOVE = 0x232 Global Const $GUI_STYLE_EDITING = BitOR($WS_POPUP, $WS_SIZEBOX) Global Const $GUI_STYLE_DISPLAY = $WS_POPUP Global Const $EDIT_MAX_LIMIT = 2^31-1 ; 0x7fffffff, 2147483647 Global Enum _ $IDX_LB_TITLE, $IDX_BT_CLOSE, $IDX_BT_SAVE, $IDX_BT_OPTIONS, $IDX_ED_NOTE, _ $IDX_LB_NOTE, $IDX_PI_BACK, $IDX_LB_BACK, $IDX_ME_MAIN, $IDX_ME_OPT, _ $IDX_RESIZE, $IDX_HWND, $IDX_ID, $IDX_OPAC, $IDX_ZPOS, _ $IDX_COLORS, $IDX_FONT, $IDX_UBOUND Global Enum $FNT_NAME, $FNT_SIZE, $FNT_WEIGHT, $FNT_STYLE Global $aFontDefault[4] = [ 'Arial', 14, 400, 0 ] Global $sColorsDefault = '0|0xFFFF80|0' Global $aColorsDefault = StringSplit($sColorsDefault, '|', 2) Global $aCtrlsEditor[5] = [ $IDX_LB_TITLE, $IDX_BT_CLOSE, $IDX_BT_SAVE, $IDX_BT_OPTIONS, $IDX_ED_NOTE ] Global $aCtrlsDisplay[4] = [ $IDX_LB_NOTE, $IDX_LB_BACK, $IDX_PI_BACK, $IDX_RESIZE ] Global $aNoteTracker[1][3] ; [0] = note id, 1 = note hWnd, 2 = note data array Global $aTabStops4Letter[1] = [ 16 ] Global $tmp Main() Func Main() $hParent = _AutoItWinGetHandle('StickyNotes Parent') _LoadNotes() TraySetClick(8) TrayCreateItem('&New note') TrayItemSetOnEvent(-1, '_NewNote') TrayItemSetState(-1, $TRAY_DEFAULT) $tm_AllNotes = TrayCreateMenu('&All Notes') TrayCreateItem('Hide', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllHide') TrayCreateItem('Unhide', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllShow') TrayCreateItem('Edit Mode', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllEdit') TrayCreateItem('Position: On &top', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllSetZPos') TrayCreateItem('Position: On &bottom', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllSetZPos') TrayCreateItem('Position: &Neutral', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllSetZPos') TrayCreateItem('Delete All', $tm_AllNotes) TrayItemSetOnEvent(-1, '_TrayAllDelete') TrayCreateItem('Edit Data File') TrayItemSetOnEvent(-1, '_TrayEditData') TrayCreateItem('Locate Notes') TrayItemSetOnEvent(-1, '_TrayFindNotes') TrayCreateItem('') TrayCreateItem('E&xit') TrayItemSetOnEvent(-1, '_Exit') GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND') GUIRegisterMsg($WM_ACTIVATE, 'WM_ACTIVATE') GUIRegisterMsg($_WM_EXITSIZEMOVE, 'WM_EXITSIZEMOVE') TraySetState(1) ;~ Main loop While 1 $sParentText = ControlGetText($hParent, '', 1) Switch $sParentText Case 'exit' _Exit() Case 'restore' ControlSetText($hParent, '', 1, '') TraySetState(2) Sleep(500) TraySetState(1) EndSwitch TraySetToolTip('StickyNotes (' & $aNoteTracker[0][0] & ')') Sleep(100) WEnd EndFunc ; Messages ################################################################ Func WM_ACTIVATE($hWnd, $iMsg, $iWParam, $iLParam) ; Re-set windows to bottom most if they are touched If Not _IsEditing($hWnd) Then $aNoteData = _GetNoteData($hWnd) If Not @error And $aNoteData[$IDX_ZPOS] = -1 Then _WinSendToBottom($hWnd) EndIf Return $GUI_RUNDEFMSG EndFunc Func WM_EXITSIZEMOVE($hWnd, $iMsg = 0, $iWParam = 0, $iLParam = 0) ; Save window size/position whenever it's resized/moved $aNoteData = _GetNoteData($hWnd) If Not @error Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Pos', _WinPosToString($hWnd)) EndIf Return $GUI_RUNDEFMSG EndFunc Func WM_COMMAND($hWnd, $iMsg, $iWParam, $iLParam) ; Double-click enters edit mode. $wNotifyCode = _WinAPI_HiWord($iWParam) If $wNotifyCode = 1 Then $aNoteData = _GetNoteData($hWnd) If Not @error Then $wID = _WinAPI_LoWord($iWParam) If $wID = $aNoteData[$IDX_LB_NOTE] Then _EditNote($hWnd) EndIf EndIf EndIf Return $GUI_RUNDEFMSG EndFunc ; Event funcs ################################################################ Func _NewNote() ; Also used for GUI accelerator Ctrl+N $iLimit = $aNoteTracker[0][0]+1 ReDim $aNoteTracker[$iLimit+1][3] $iNewNoteID = TimerInit() $aReturn = _MakeNoteWindow($iNewNoteID) $aNoteTracker[$iLimit][0] = $iNewNoteID $aNoteTracker[$iLimit][1] = $aReturn[0] $aNoteTracker[$iLimit][2] = $aReturn[1] $aNoteTracker[0][0] += 1 _NoteSetOpac($aReturn[0]) _NoteSetZPos($aReturn[0]) _NoteSetColors($aReturn[0]) _NoteSetFont($aReturn[0]) _ToggleEdit($aReturn[0], $aNoteTracker[$iLimit][2], True) GUISetState(@SW_SHOW, $aReturn[0]) EndFunc Func _TrayAllHide() For $i = 1 To $aNoteTracker[0][0] GUISetState(@SW_HIDE, $aNoteTracker[$i][1]) Next _TimedToolTip('Notes have been hidden. Use the tray menu to show them again.', 2000, Default, Default, '', 0, 4) EndFunc Func _TrayAllShow() For $i = 1 To $aNoteTracker[0][0] GUISetState(@SW_SHOW, $aNoteTracker[$i][1]) Next EndFunc Func _TrayAllEdit() For $i = 1 To $aNoteTracker[0][0] _EditNote($aNoteTracker[$i][1]) Next EndFunc Func _TrayAllSetZPos() $sMenuItem = TrayItemGetText(@TRAY_ID) If StringInStr($sMenuItem, 'top') Then For $i = 1 To $aNoteTracker[0][0] _NoteSetZPos($aNoteTracker[$i][1], 1) Next _TimedToolTip('All note windows set to top most position.', 1500, Default, Default, '', 0, 4) ElseIf StringInStr($sMenuItem, 'bottom') Then For $i = 1 To $aNoteTracker[0][0] _NoteSetZPos($aNoteTracker[$i][1], -1) Next _TimedToolTip('All note windows set to bottom most position.', 1500, Default, Default, '', 0, 4) Else For $i = 1 To $aNoteTracker[0][0] _NoteSetZPos($aNoteTracker[$i][1], 0) Next _TimedToolTip('All note windows set to neutral position.', 1500, Default, Default, '', 0, 4) EndIf EndFunc Func _TrayAllDelete() If MsgBox(0x42134, 'Delete ALL Notes', 'Are you sure you want to delete ALL your notes?') = 6 Then $hGUITopWindow = GUICreate('') WinSetOnTop($hGUITopWindow, '', 1) $sConfirm = InputBox('Confirm Delete All', 'Please type "yes" in the box to confirm deletion of all notes.', 'No! Don''t do it!', '', 200, 150, Default, Default, 15, $hGUITopWindow) GUIDelete($hGUITopWindow) If $sConfirm = 'yes' Or $sConfirm = '"yes"' Then For $i = 1 To $aNoteTracker[0][0] _DelNoteData($aNoteTracker[$i][1]) Next Global $aNoteTracker[1][3] MsgBox(0x42040, 'StickyNotes', 'All notes have been deleted.') EndIf EndIf EndFunc Func _TrayEditData() If FileExists($sSaveData) Then ShellExecute($sSaveData) Else MsgBox(0x42040, 'StickyNotes', 'No StickyNotes data has been saved yet so the file does not exist.') EndIf EndFunc Func _TrayFindNotes() For $i = 1 To $aNoteTracker[0][0] $aNoteData = $aNoteTracker[$i][2] WinActivate($aNoteTracker[$i][1]) WinSetOnTop($aNoteTracker[$i][1], '', 1) $aWP = WinGetPos($aNoteTracker[$i][1]) MouseMove($aWP[0] + $aWP[2]/2, $aWP[1] + $aWP[3]/2, 5) ToolTip('Note ' & $i & ' of ' & $aNoteTracker[0][0]) Sleep(500) _NoteSetZPos($aNoteTracker[$i][1], $aNoteData[$IDX_ZPOS]) ToolTip('') Next EndFunc Func _Exit() ToolTip('Exiting StickyNotes.') TraySetState(2) For $i = 1 To $aNoteTracker[0][0] GUIDelete($aNoteTracker[$i][1]) Next If $DEBUG Then ConsoleWrite('Good bye') Exit EndFunc ; GUI event funcs ################################################################ Func _ResizeNote() Local $aWinPos = WinGetPos(@GUI_WinHandle) Local $aMouseStart = MouseGetPos() Do $aMouse = MouseGetPos() WinMove(@GUI_WinHandle, '', Default, Default, $aWinPos[2] + $aMouse[0] - $aMouseStart[0], $aWinPos[3] + $aMouse[1] - $aMouseStart[1]) $gc = GUIGetCursorInfo(@GUI_WinHandle) Until Not $gc[2] WM_EXITSIZEMOVE(@GUI_WinHandle) EndFunc Func _SaveEdit() If _IsEditing(@GUI_WinHandle) Then $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') $sText = GUICtrlRead($aNoteData[$IDX_ED_NOTE]) If $sText = '' Then $iMsg = MsgBox(0x42124, 'StickyNotes', 'Note text is empty.' & @LF & 'Would you like to delete this note?') If $iMsg = 6 Then _DelNoteData(@GUI_WinHandle) _TimedToolTip('Note deleted.', 1000, Default, Default, '', 0, 4) Return EndIf EndIf GUICtrlSetData($aNoteData[$IDX_LB_NOTE], $sText) _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Text', _StringFormatEscape($sText)) _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Pos', _WinPosToString(@GUI_WinHandle)) _ToggleEdit(@GUI_WinHandle, $aNoteData, False) _TimedToolTip('Note saved.', 1000, Default, Default, '', 0, 4) EndIf EndFunc Func _CancelEdit() If _IsEditing(@GUI_WinHandle) Then $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If _IsEmpty($aNoteData[$IDX_ID]) Then _DelNoteData(@GUI_WinHandle) Else _ToggleEdit(@GUI_WinHandle, $aNoteData, False) GUICtrlSetData($aNoteData[$IDX_ED_NOTE], '') EndIf EndIf EndFunc Func _DeleteNote() $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If Not _IsEmpty($aNoteData[$IDX_ID]) Then If MsgBox(0x42134, 'Delete Note', 'Are you sure you want to delete this note?') <> 6 Then Return False EndIf _DelNoteData(@GUI_WinHandle) _TimedToolTip('Note deleted.', 1000, Default, Default, '', 0, 4) EndFunc Func _EditNote($hWnd = 0) ; Overloaded func If Not IsDeclared('hWnd') Then $hWnd = @GUI_WinHandle ; Hack to allow overload If Not _IsEditing($hWnd) Then $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') GUICtrlSetData($aNoteData[$IDX_ED_NOTE], GUICtrlRead($aNoteData[$IDX_LB_NOTE])) _ToggleEdit($hWnd, $aNoteData, True) EndIf EndFunc Func _CopyNote() $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') ClipPut(GUICtrlRead($aNoteData[$IDX_LB_NOTE])) EndFunc Func _OptionsButton() $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') $aWP = WinGetPos(GUICtrlGetHandle(@GUI_CtrlId)) _TrackPopupMenu(@GUI_WinHandle, $aNoteData[$IDX_ME_OPT], $aWP[0], $aWP[1] + $aWP[3]) EndFunc Func _SetOpac() $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') $iOpac = Int(GUICtrlRead(@GUI_CtrlId, 1)) _TimedToolTip('Note at ' & $iOpac & '% opacity.', 500, Default, Default, '', 0, 4) _NoteSetOpac(@GUI_WinHandle, $iOpac) EndFunc Func _SetZPos() $aNoteData = _GetNoteData(@GUI_WinHandle) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') $sMenuItem = GUICtrlRead(@GUI_CtrlId, 1) If StringInStr($sMenuItem, 'top') Then _TimedToolTip('Note set to top most window.', 500, Default, Default, '', 0, 4) _NoteSetZPos(@GUI_WinHandle, 1) ElseIf StringInStr($sMenuItem, 'bottom') Then _TimedToolTip('Note set to bottom most window.', 500, Default, Default, '', 0, 4) _NoteSetZPos(@GUI_WinHandle, -1) Else _TimedToolTip('Note set to neutral position', 500, Default, Default, '', 0, 4) _NoteSetZPos(@GUI_WinHandle, 0) EndIf EndFunc Func _TabKey() If _IsEditing(@GUI_WinHandle) Then $hCtrl = ControlGetHandle(@GUI_WinHandle, '', ControlGetFocus(@GUI_WinHandle)) If _WinAPI_GetClassName($hCtrl) = 'EDIT' Then _GUICtrlEdit_ReplaceSel($hCtrl, @TAB, True) EndIf EndIf EndFunc Func _SelectAll() If _IsEditing(@GUI_WinHandle) Then $hCtrl = ControlGetHandle(@GUI_WinHandle, '', ControlGetFocus(@GUI_WinHandle)) If _WinAPI_GetClassName($hCtrl) = 'EDIT' Then _GUICtrlEdit_SetSel($hCtrl, 0, -1) EndIf EndIf EndFunc ; Indirect funcs ################################################################ Func _LoadNotes() $aIniNotes = IniReadSectionNames($sSaveData) If @error Then Local $aIniNotes[1] = [ 0 ] Global $aNoteTracker[$aIniNotes[0]+1][3] $aNoteTracker[0][0] = $aIniNotes[0] $bHasEmpties = False For $i = 1 To $aIniNotes[0] If _IsEmpty($aIniNotes[$i]) Then $bHasEmpties = True ContinueLoop EndIf $aReturn = _MakeNoteWindow($aIniNotes[$i], _ StringSplit(_IniReadWrap($sSaveData, $aIniNotes[$i], 'Pos', ''), '|', 2), _ StringFormat(_IniReadWrap($sSaveData, $aIniNotes[$i], 'Text', ''))) $aNoteTracker[$i][0] = $aIniNotes[$i] $aNoteTracker[$i][1] = $aReturn[0] $aNoteTracker[$i][2] = $aReturn[1] _NoteSetOpac($aReturn[0], Int(_IniReadWrap($sSaveData, $aIniNotes[$i], 'Opac', 100)), False) _NoteSetColors($aReturn[0], _IniReadWrap($sSaveData, $aIniNotes[$i], 'Colors', ''), False) _NoteSetBGPic($aReturn[0], _IniReadWrap($sSaveData, $aIniNotes[$i], 'Pic', '-1'), False) _NoteSetFont($aReturn[0], _IniReadWrap($sSaveData, $aIniNotes[$i], 'Font', ''), False) ; Little explanation: ; If we don't set the ZPos AFTER showing the window, it stays on top until focused. ; If we called _ToggleEdit AFTER showing the window, it flashes briefly (ugly). ; _ToggleEdit calls _NoteSetZPos, which writes to the config, so the _IniReadWrap would give us the default (0) ; if we tried to set from that after the _ToggleEdit, so we read the config first, save the ZPos to a var, ; then use that to set the ZPos after we toggle and show the window. Simple, no? (No). $iZPos = Int(_IniReadWrap($sSaveData, $aIniNotes[$i], 'ZPos', 0)) ; Read the ZPos before it's altered _ToggleEdit($aReturn[0], $aNoteTracker[$i][2], False) ; Toggle edit mode, which alters ZPos GUISetState(@SW_SHOWNA, $aReturn[0]) ; Show the window _NoteSetZPos($aReturn[0], $iZPos, False) ; Set the ZPos to the preloaded state Next If $bHasEmpties Then For $i = 1 To $aIniNotes[0] If _IsEmpty($aIniNotes[$i]) Then _IniDeleteWrap($sSaveData, $aIniNotes[$i]) EndIf Next EndIf EndFunc Func _MakeNoteWindow($iID, $vWinPos = -1, $sNote = '') ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #region - Window create $hGUINoteWin = GUICreate('StickyNoteWin', 200, 200, Default, Default, $GUI_STYLE_EDITING, $WS_EX_TOOLWINDOW) GUISetOnEvent($GUI_EVENT_CLOSE, '_CancelEdit') $dm_NewNote = GUICtrlCreateDummy() GUICtrlSetOnEvent(-1, '_NewNote') $dm_Tab = GUICtrlCreateDummy() GUICtrlSetOnEvent(-1, '_TabKey') $dm_SelAll = GUICtrlCreateDummy() GUICtrlSetOnEvent(-1, '_SelectAll') $dm_Exit = GUICtrlCreateDummy() GUICtrlSetOnEvent(-1, '_Exit') $lb_Title = GUICtrlCreateLabel(' Editing note...', 0, 0, 185, 15, Default, $GUI_WS_EX_PARENTDRAG) GUICtrlSetColor(-1, _ColorCorrect(_WinAPI_GetSysColor($COLOR_CAPTIONTEXT))) GUICtrlSetBkColor(-1, _ColorCorrect(_WinAPI_GetSysColor($COLOR_ACTIVECAPTION))) GUICtrlSetResizing(-1, BitOR($GUI_DOCKMENUBAR, $GUI_DOCKLEFT, $GUI_DOCKRIGHT)) $bt_Close = GUICtrlCreateButton('x', 185, 0, 15, 15) GUICtrlSetFont(-1, 8) GUICtrlSetTip(-1, 'Cancel edit (Escape)') GUICtrlSetResizing(-1, BitOR($GUI_DOCKMENUBAR, $GUI_DOCKWIDTH, $GUI_DOCKRIGHT)) GUICtrlSetOnEvent(-1, '_CancelEdit') $bt_Save = GUICtrlCreateButton('&Save', 0, 185, 100, 15) GUICtrlSetTip(-1, 'Save edit (Ctrl+S, Ctrl+Enter)') GUICtrlSetResizing(-1, $GUI_DOCKSTATEBAR) GUICtrlSetOnEvent(-1, '_SaveEdit') $bt_Options = GUICtrlCreateButton('&Options', 100, 185, 100, 15) GUICtrlSetResizing(-1, $GUI_DOCKSTATEBAR) GUICtrlSetOnEvent(-1, '_OptionsButton') $ed_Note = GUICtrlCreateEdit($sNote, 0, 15, 200, 170, BitOR($ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL)) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) _GUICtrlEdit_SetTabStops($ed_Note, $aTabStops4Letter) _GUICtrlEdit_SetLimitText($ed_Note, $EDIT_MAX_LIMIT) $lb_Resize = GUICtrlCreateLabel('', 190, 190, 10, 10) GUICtrlSetCursor(-1, 12) GUICtrlSetResizing(-1, BitOR($GUI_DOCKSIZE, $GUI_DOCKBOTTOM, $GUI_DOCKRIGHT)) GUICtrlSetOnEvent(-1, '_ResizeNote') $pi_Background = GUICtrlCreatePic('', 1, 1, 198, 198) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) $lb_Background = GUICtrlCreateLabel('', 1, 1, 198, 198, Default, $GUI_WS_EX_PARENTDRAG) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) $lb_Note = GUICtrlCreateLabel($sNote, 5, 5, 190, 190, BitOR($SS_NOTIFY, $SS_LEFT, $SS_NOPREFIX), $GUI_WS_EX_PARENTDRAG) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) $cm_Main = GUICtrlCreateContextMenu($lb_Note) $hMainMenu = GUICtrlGetHandle($cm_Main) $mi_Edit = GUICtrlCreateMenuItem('&Edit Ctrl+E', $cm_Main) GUICtrlSetOnEvent(-1, '_EditNote') $mi_Copy = GUICtrlCreateMenuItem('&Copy Text', $cm_Main) GUICtrlSetOnEvent(-1, '_CopyNote') $me_Options = GUICtrlCreateMenu('&Options', $cm_Main) $hOptionsMenu = GUICtrlGetHandle($me_Options) $mi_Delete = GUICtrlCreateMenuItem('&Delete Ctrl+Del', $me_Options) GUICtrlSetOnEvent(-1, '_DeleteNote') $mi_Appearance = GUICtrlCreateMenuItem('&Appearance Ctrl+P', $me_Options) GUICtrlSetOnEvent(-1, '_AppearanceDialog') $me_Pos = GUICtrlCreateMenu('&Position', $me_Options) $mi_Top = GUICtrlCreateMenuItem('Always on &top Ctrl+T', $me_Pos) GUICtrlSetOnEvent(-1, '_SetZPos') $mi_Bot = GUICtrlCreateMenuItem('Always on &bottom Ctrl+B', $me_Pos) GUICtrlSetOnEvent(-1, '_SetZPos') $mi_Neutral = GUICtrlCreateMenuItem('Ne&utral Ctrl+U', $me_Pos) GUICtrlSetOnEvent(-1, '_SetZPos') $me_Opac = GUICtrlCreateMenu('&Opacity', $me_Options) $mi_Opac10 = GUICtrlCreateMenuItem('10% Ctrl+&1', $me_Opac) GUICtrlSetOnEvent(-1, '_SetOpac') For $i = 2 To 9 GUICtrlCreateMenuItem(($i*10) & '% Ctrl+&' & $i & '', $me_Opac) GUICtrlSetOnEvent(-1, '_SetOpac') Next GUICtrlCreateMenuItem('100% Ctrl+&0', $me_Opac) GUICtrlSetOnEvent(-1, '_SetOpac') #endregion <- Window create If UBound($vWinPos) >= 4 Then WinMove($hGUINoteWin, '', $vWinPos[0], $vWinPos[1], $vWinPos[2], $vWinPos[3]) EndIf Local $aGUIAccel[22][2] = [ _ [ '{TAB}', $dm_Tab ], [ '^n', $dm_NewNote ], _ [ '^a', $dm_SelAll ], _ [ '^{ENTER}', $bt_Save ], [ '^s', $bt_Save ], _ [ '^e', $mi_Edit ], [ '^{DEL}', $mi_Delete ], _ [ '^p', $mi_Appearance ], [ '^t', $mi_Top ], _ [ '^b', $mi_Bot ], [ '^u', $mi_Neutral ], _ [ '^1', $mi_Opac10 ], [ '^2', $mi_Opac10+1 ], _ [ '^3', $mi_Opac10+2 ], [ '^4', $mi_Opac10+3 ], _ [ '^5', $mi_Opac10+4 ], [ '^6', $mi_Opac10+5 ], _ [ '^7', $mi_Opac10+6 ], [ '^8', $mi_Opac10+7 ], _ [ '^9', $mi_Opac10+8 ], [ '^0', $mi_Opac10+9 ], _ [ '^!x', $dm_Exit ] _ ] GUISetAccelerators($aGUIAccel, $hGUINoteWin) Local $aData[$IDX_UBOUND] = [ _ $lb_Title, $bt_Close, $bt_Save, $bt_Options, $ed_Note, _ $lb_Note, $pi_Background, $lb_Background, $hMainMenu, $hOptionsMenu, _ $lb_Resize, $hGUINoteWin, $iID ] Local $aReturn[2] = [ $hGUINoteWin, $aData ] Return $aReturn EndFunc Func _ToggleEdit($hWnd, $aNoteData, $bSwitch = False) If $bSwitch Then GUISetState(@SW_LOCK, $hWnd) WinSetTitle($hWnd, '', 'StickyNoteWin - Editing') GUISetStyle($GUI_STYLE_EDITING, Default, $hWnd) For $i = 0 To UBound($aCtrlsEditor)-1 GUICtrlSetState($aNoteData[$aCtrlsEditor[$i]], BitOR($GUI_SHOW, $GUI_ENABLE)) Next For $i = 0 To UBound($aCtrlsDisplay)-1 GUICtrlSetState($aNoteData[$aCtrlsDisplay[$i]], BitOR($GUI_HIDE, $GUI_DISABLE)) Next _WinForceRepaint($hWnd) WinSetTrans($hWnd, '', 255) WinSetOnTop($hWnd, '', 1) GUISetState(@SW_UNLOCK, $hWnd) WinActivate($hWnd) ControlFocus($hWnd, '', $aNoteData[$IDX_ED_NOTE]) Else GUISetState(@SW_LOCK, $hWnd) WinSetTitle($hWnd, '', 'StickyNoteWin') GUISetStyle($GUI_STYLE_DISPLAY, Default, $hWnd) For $i = 0 To UBound($aCtrlsEditor)-1 GUICtrlSetState($aNoteData[$aCtrlsEditor[$i]], BitOR($GUI_HIDE, $GUI_DISABLE)) Next For $i = 0 To UBound($aCtrlsDisplay)-1 GUICtrlSetState($aNoteData[$aCtrlsDisplay[$i]], BitOR($GUI_SHOW, $GUI_ENABLE)) Next GUICtrlSetState($aNoteData[$IDX_LB_BACK], $GUI_DISABLE) GUICtrlSetState($aNoteData[$IDX_PI_BACK], $GUI_DISABLE) _WinForceRepaint($hWnd) _NoteSetOpac($hWnd, $aNoteData[$IDX_OPAC]) _NoteSetZPos($hWnd, $aNoteData[$IDX_ZPOS]) GUISetState(@SW_UNLOCK, $hWnd) EndIf EndFunc Func _AppearanceDialog($hWnd = 0, $iX = Default, $iY = Default) If Not IsDeclared('hWnd') Then $hWnd = @GUI_WinHandle $iX = MouseGetPos(0) $iY = MouseGetPos(1) EndIf $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') Opt('GUIOnEventMode', 0) GUISetState(@SW_DISABLE, $hWnd) $aFontData = StringSplit($aNoteData[$IDX_FONT], '|', 2) If UBound($aFontData) < 4 Then $aFontData = $aFontDefault $aColors = StringSplit($aNoteData[$IDX_COLORS], '|', 2) $sBGImage = _IniReadWrap($sSaveData, $aNoteData[$IDX_ID], 'Pic', '') ReDim $aColors[3] $hGUIColorDialog = GUICreate('Note Appearance', 225, 140, Default, Default, Default, $WS_EX_TOOLWINDOW, $hWnd) GUICtrlCreateGroup('Colors:', 5, 0, 110, 80) GUICtrlCreateLabel('&Font:', 10, 15, 65, 15, $SS_RIGHT) $bt_Font = GUICtrlCreateButton('', 80, 15, 30, 15) GUICtrlSetBkColor(-1, $aColors[0]) GUICtrlCreateLabel('&Background:', 10, 35, 65, 15, $SS_RIGHT) $bt_Back = GUICtrlCreateButton('', 80, 35, 30, 15) GUICtrlSetBkColor(-1, $aColors[1]) GUICtrlCreateLabel('Bor&der:', 10, 55, 65, 15, $SS_RIGHT) $bt_Bord = GUICtrlCreateButton('', 80, 55, 30, 15) GUICtrlSetBkColor(-1, $aColors[2]) GUICtrlCreateGroup('Misc.', 5, 80, 110, 55) GUICtrlCreateLabel('Font &settings:', 10, 95, 65, 15, $SS_RIGHT) $bt_FontSet = GUICtrlCreateButton('...', 80, 95, 30, 15) GUICtrlCreateLabel('&Picture:', 10, 115, 65, 15, $SS_RIGHT) $bt_Pic = GUICtrlCreateButton('...', 80, 115, 30, 15) GUICtrlCreateGroup('Preview:', 120, 0, 100, 110) $lb_BordEx = GUICtrlCreateLabel('', 125, 15, 90, 90) GUICtrlSetBkColor(-1, $aColors[2]) GUICtrlSetState(-1, $GUI_DISABLE) $pi_BackEx = GUICtrlCreatePic('', 126, 16, 88, 88) GUICtrlSetState(-1, $GUI_DISABLE) $lb_BackEx = GUICtrlCreateLabel('', 126, 16, 88, 88) GUICtrlSetBkColor($lb_BackEx, $aColors[1]) GUICtrlSetState(-1, $GUI_DISABLE) $lb_FontEx = GUICtrlCreateLabel('This is the note text', 131, 20, 80, 80) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetColor(-1, $aColors[0]) GUICtrlSetFont($lb_FontEx, $aFontData[$FNT_SIZE], $aFontData[$FNT_WEIGHT], $aFontData[$FNT_STYLE], $aFontData[$FNT_NAME]) $bt_Reset = GUICtrlCreateButton('&Reset', 125, 115, 50, 20) $bt_OK = GUICtrlCreateButton('OK', 180, 115, 40, 20) If $sBGImage Then GUICtrlSetImage($pi_BackEx, $sBGImage) GUICtrlSetBkColor($lb_BackEx, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetBkColor($bt_Back, $GUI_BKCOLOR_TRANSPARENT) EndIf $aWP = WinGetPos($hGUIColorDialog) If $iX + $aWP[2] > @DesktopWidth Then $iX -= $aWP[2] If $iY + $aWP[3] > @DesktopHeight Then $iY -= $aWP[3] WinMove($hGUIColorDialog, '', $iX, $iY) GUISetState() ControlFocus($hGUIColorDialog, '', $bt_OK) While 1 $gm = GUIGetMsg() Switch $gm Case $bt_Font $iColor = _ChooseColor(2, $aColors[0], 2, $hGUIColorDialog) If $iColor <> -1 Then $aColors[0] = $iColor GUICtrlSetBkColor($bt_Font, $iColor) GUICtrlSetColor($lb_FontEx, $iColor) EndIf Case $bt_Back $iColor = _ChooseColor(2, $aColors[1], 2, $hGUIColorDialog) If $iColor <> -1 Then $aColors[1] = $iColor GUICtrlSetBkColor($bt_Back, $iColor) GUICtrlSetBkColor($lb_BackEx, $iColor) GUICtrlSetImage($pi_BackEx, '') $sBGImage = '' EndIf Case $bt_Bord $iColor = _ChooseColor(2, $aColors[2], 2, $hGUIColorDialog) If $iColor <> -1 Then $aColors[2] = $iColor GUICtrlSetBkColor($bt_Bord, $iColor) GUICtrlSetBkColor($lb_BordEx, $iColor) EndIf Case $bt_FontSet $aFontChoice = _ChooseFontMod($aFontData[$FNT_NAME], $aFontData[$FNT_SIZE], 0, $aFontData[$FNT_WEIGHT], $aFontData[$FNT_STYLE], BitOR($CF_SCREENFONTS, $CF_EFFECTS, $CF_INITTOLOGFONTSTRUCT), $hGUIColorDialog) If $aFontChoice <> -1 Then $aFontData[$FNT_NAME] = $aFontChoice[2] $aFontData[$FNT_SIZE] = $aFontChoice[3] $aFontData[$FNT_WEIGHT] = $aFontChoice[4] $aFontData[$FNT_STYLE] = $aFontChoice[1] GUICtrlSetFont($lb_FontEx, $aFontData[$FNT_SIZE], $aFontData[$FNT_WEIGHT], $aFontData[$FNT_STYLE], $aFontData[$FNT_NAME]) EndIf Case $bt_Pic $sFile = FileOpenDialog('Background picture', '', 'Image files (*.gif;*.jpg;*.jpeg;*.bmp)', 1, '', $hGUIColorDialog) If Not @error Then If GUICtrlSetImage($pi_BackEx, $sFile) Then $sBGImage = $sFile GUICtrlSetBkColor($bt_Back, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetBkColor($lb_BackEx, $GUI_BKCOLOR_TRANSPARENT) Else MsgBox(0x42030, 'StickyNotes', 'Unable to set file as background.' & @LF & $sFile) EndIf EndIf Case $bt_Reset $sBGImage = '' GUICtrlSetImage($pi_BackEx, '') $aColors = $aColorsDefault GUICtrlSetBkColor($bt_Font, $aColors[0]) GUICtrlSetColor($lb_FontEx, $aColors[0]) GUICtrlSetBkColor($bt_Back, $aColors[1]) GUICtrlSetBkColor($lb_BackEx, $aColors[1]) GUICtrlSetBkColor($bt_Bord, $aColors[2]) GUICtrlSetBkColor($lb_BordEx, $aColors[2]) $aFontData = $aFontDefault GUICtrlSetFont($lb_FontEx, $aFontData[$FNT_SIZE], $aFontData[$FNT_WEIGHT], $aFontData[$FNT_STYLE], $aFontData[$FNT_NAME]) Case $bt_OK _NoteSetFont($hWnd, $aFontData) $sColors = _ArrayJoin($aColors) _NoteSetColors($hWnd, $sColors) If $sBGImage Then _NoteSetBGPic($hWnd, $sBGImage) Else _NoteSetBGPic($hWnd, -1) EndIf ContinueCase Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete($hGUIColorDialog) GUISetState(@SW_ENABLE, $hWnd) WinActivate($hWnd) Opt('GUIOnEventMode', 1) EndFunc Func _GetNoteData($vID) If IsHWnd($vID) Then For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][1] = $vID Then Return $aNoteTracker[$i][2] Next Else For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][0] = $vID Then Return $aNoteTracker[$i][2] Next EndIf Return SetError(1, 0, 0) EndFunc Func _SetNoteData($vID, $iIndex, $vData) $iIndex = Int($iIndex) If $iIndex < 0 Then Return False If IsHWnd($vID) Then For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][1] = $vID Then If $iIndex < UBound($aNoteTracker[$i][2]) Then $tmp = $aNoteTracker[$i][2] $tmp[$iIndex] = $vData $aNoteTracker[$i][2] = $tmp $tmp = 0 Return True EndIf EndIf Next Else For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][0] = $vID Then If $iIndex < UBound($aNoteTracker[$i][2]) Then $tmp = $aNoteTracker[$i][2] $tmp[$iIndex] = $vData $aNoteTracker[$i][2] = $tmp $tmp = 0 Return True EndIf EndIf Next EndIf Return False EndFunc Func _DelNoteData($vID) $iUB = $aNoteTracker[0][0] If IsHWnd($vID) Then For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][1] = $vID Then Return _EraseNoteByIndex($i) Next Else For $i = 1 To $aNoteTracker[0][0] If $aNoteTracker[$i][0] = $vID Then Return _EraseNoteByIndex($i) Next EndIf Return False EndFunc Func _EraseNoteByIndex($iIndex) ; Erases all traces of the note by it's index in the NoteTracker array _IniDeleteWrap($sSaveData, $aNoteTracker[$iIndex][0]) GUIDelete($aNoteTracker[$iIndex][1]) $iUB = $aNoteTracker[0][0] $aNoteTracker[$iIndex][0] = $aNoteTracker[$iUB][0] $aNoteTracker[$iIndex][1] = $aNoteTracker[$iUB][1] $aNoteTracker[$iIndex][2] = $aNoteTracker[$iUB][2] ReDim $aNoteTracker[$iUB][3] $aNoteTracker[0][0] -= 1 Return True EndFunc Func _NoteSetOpac($hWnd, $iOpac = 100, $bSave = 1) $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If $iOpac < 10 Then $iOpac = 10 If $iOpac > 100 Then $iOpac = 100 $iOpacity = Round($iOpac * 255 / 100) If $bSave Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Opac', $iOpac) _SetNoteData($hWnd, $IDX_OPAC, $iOpac) If Not _IsEditing($hWnd) Then WinSetTrans($hWnd, '', $iOpacity) EndFunc Func _NoteSetZPos($hWnd, $iZPos = 0, $bSave = 1) $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If $bSave Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'ZPos', $iZPos) _SetNoteData($hWnd, $IDX_ZPOS, $iZPos) If Not _IsEditing($hWnd) Then Switch $iZPos Case 1 WinSetOnTop($hWnd, '', 1) Case -1 _WinSendToBottom($hWnd) Case Else WinSetOnTop($hWnd, '', 0) EndSwitch EndIf EndFunc Func _NoteSetFont($hWnd, $vFontData = '', $bSave = 1) $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If Not IsArray($vFontData) Then $vFontData = StringSplit($vFontData, '|', 2) ; Accept non-arrays in FontData, and auto convert to array If UBound($vFontData) < 4 Then $vFontData = $aFontDefault ; If FontData array doesn't have the right number of elements it's invalid, so default $sFontData = _ArrayJoin($vFontData) ; Right back to a string again for storage purposes :P If $bSave Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Font', $sFontData) ; Save in settings _SetNoteData($hWnd, $IDX_FONT, $sFontData) ; Save in tracking array GUICtrlSetFont($aNoteData[$IDX_LB_NOTE], $vFontData[$FNT_SIZE], $vFontData[$FNT_WEIGHT], $vFontData[$FNT_STYLE], $vFontData[$FNT_NAME]) ; Magic time! EndFunc Func _NoteSetColors($hWnd, $sColors = '', $bSave = 1) $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') $aColors = StringSplit($sColors, '|', 2) If UBound($aColors) < 3 Then $sColors = $sColorsDefault $aColors = $aColorsDefault EndIf GUICtrlSetColor($aNoteData[$IDX_LB_NOTE], $aColors[0]) GUICtrlSetBkColor($aNoteData[$IDX_LB_BACK], $aColors[1]) GUISetBkColor($aColors[2], $hWnd) If $bSave Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Colors', $sColors) _SetNoteData($hWnd, $IDX_COLORS, $sColors) _NoteSetBGPic($hWnd, _IniReadWrap($sSaveData, $aNoteData[$IDX_ID], 'Pic', -1)) EndFunc Func _NoteSetBGPic($hWnd, $sFile, $bSave = 1) $aNoteData = _GetNoteData($hWnd) If @error Then Return MsgBox(0x42010, 'StickyNotes Error', 'Unable to read note data.') If $sFile = -1 Then $aColors = StringSplit($aNoteData[$IDX_COLORS], '|', 2) ReDim $aColors[3] GUICtrlSetBkColor($aNoteData[$IDX_LB_BACK], $aColors[1]) GUICtrlSetImage($aNoteData[$IDX_PI_BACK], '') _IniDeleteWrap($sSaveData, $aNoteData[$IDX_ID], 'Pic') Return True ElseIf GUICtrlSetImage($aNoteData[$IDX_PI_BACK], $sFile) Then GUICtrlSetBkColor($aNoteData[$IDX_LB_BACK], $GUI_BKCOLOR_TRANSPARENT) If $bSave Then _IniWriteWrap($sSaveData, $aNoteData[$IDX_ID], 'Pic', $sFile) _WinForceRepaint($hWnd) Return True Else Return False EndIf EndFunc Func _IsEditing($hWnd) If StringInStr(WinGetTitle($hWnd), 'Editing') Then Return True Return False EndFunc Func _IsEmpty($iID) _IniReadWrap($sSaveData, $iID, 'Text', '') If @error Then Return True Return False EndFunc ; Utility funcs Func _StringFormatEscape($sString) $sString = StringReplace($sString, '%', '%%') $sString = StringReplace($sString, '\', '\\') $sString = StringReplace($sString, @CR, '\r') $sString = StringReplace($sString, @LF, '\n') $sString = StringReplace($sString, @TAB, '\t') Return $sString EndFunc Func _WinPosToString($hWnd) $wp = WinGetPos($hWnd) Return _ArrayJoin($wp) EndFunc Func _TrackPopupMenu($hWnd, $hMenu, $x, $y) DllCall('user32.dll', 'int', 'TrackPopupMenuEx', 'hwnd', $hMenu, 'int', 0, 'int', $x, 'int', $y, 'hwnd', $hWnd, 'ptr', 0) EndFunc Func _WinSendToBottom($hWnd) ;~ Return _WinAPI_SetWindowPos($hWnd, $HWND_BOTTOM, 0,0,0,0, BitOR($SWP_NOSIZE, $SWP_NOMOVE, $SWP_NOACTIVATE)) $aResult = DllCall("User32.dll", "int", "SetWindowPos", _ "hwnd", $hWnd, _ "hwnd", $HWND_BOTTOM, _ "int", 0, "int", 0, _ "int", 0, "int", 0, _ "int", BitOR($SWP_NOSIZE, $SWP_NOMOVE, $SWP_NOACTIVATE)) Return $aResult[0] <> 0 EndFunc Func _WinForceRepaint($hWnd, $l = @ScriptLineNumber) Return ConsoleWrite($l & ' Forcerepaint' & @CRLF) $aWP = WinGetPos($hWnd) WinMove($hWnd, '', Default, Default, $aWP[2], $aWP[3]+1) WinMove($hWnd, '', Default, Default, $aWP[2], $aWP[3]) EndFunc Func _ArrayJoin(ByRef $aArray, $sDelim = '|', $iLBound = 0, $iUBound = Default) If $iUBound = Default Then $iUBound = UBound($aArray)-1 Local $sOut = $aArray[$iLBound] For $i = $iLBound+1 To $iUBound $sOut &= $sDelim & $aArray[$i] Next Return $sOut EndFunc ; Ini wrappers for debugging Func _IniWriteWrap($sFilename, $sSection, $sKey, $sValue, $LINE = @ScriptLineNumber) Local $CW, $vResult = IniWrite($sFilename, $sSection, $sKey, $sValue) $CW = StringFormat('+ %d %d IniWrite("%s", "%s", "%s", "%s") = %s\r\n', _ $LINE, _ @MSEC, _ $sFilename, _ $sSection, _ $sKey, _ $sValue, _ $vResult) If $DEBUG Then ConsoleWrite($CW) Return $vResult EndFunc Func _IniReadWrap($sFilename, $sSection, $sKey, $sDefault, $LINE = @ScriptLineNumber) Local $iError, $CW, $vResult = IniRead($sFilename, $sSection, $sKey, @CRLF) If $vResult = @CRLF Then $vResult = $sDefault $iError = 1 EndIf $CW = StringFormat('> %d %d IniRead("%s", "%s", "%s", "%s") = %s\r\n', _ $LINE, _ @MSEC, _ $sFilename, _ $sSection, _ $sKey, _ $sDefault, _ $vResult) If $DEBUG Then ConsoleWrite($CW) Return SetError($iError, 0, $vResult) EndFunc Func _IniDeleteWrap($sFilename, $sSection, $sKey = Default, $LINE = @ScriptLineNumber) Local $vResult, $CW If $sKey = Default Then $vResult = IniDelete($sFilename, $sSection) $CW = StringFormat('- %d %d IniDelete("%s", "%s") = %s\r\n', _ $LINE, _ @MSEC, _ $sFilename, _ $sSection, _ $vResult) Else $vResult = IniDelete($sFilename, $sSection, $sKey) $CW = StringFormat('- %d %d IniDelete("%s", "%s", "%s") = %s\r\n', _ $LINE, _ @MSEC, _ $sFilename, _ $sSection, _ $sKey, _ $vResult) EndIf If $DEBUG Then ConsoleWrite($CW) Return $vResult EndFunc Func _AutoItWinGetHandle($sNewTitle = Default) Local $sRand, $hWnd If $sNewTitle = Default Then $sNewTitle = AutoItWinGetTitle() Do $sRand = String(Random()) Until Not WinExists($sRand) AutoItWinSetTitle($sRand) $hWnd = WinGetHandle($sRand) AutoitWinSetTitle($sNewTitle) Return $hWnd EndFunc