Как добавить всплывающие подсказки на поля формы? Topic is solved

Помощь в написании скриптов
marusa122
Posts: 135
Joined: 22 Mar 2023, 07:48

Как добавить всплывающие подсказки на поля формы?

25 Aug 2023, 10:18

Здравствуйте. Есть скрипт Autohotkey. В нем - основной скрипт и пользовательская форма для его настройки. Нужно, чтобы при наведении курсора на поля, где указываются %Ed_CordRefx% и %Ed_CordAutoRefx%, возле курсора появлялась всплывающая подсказка "Координата X" (которая исчезала бы или через 3 секунды, если курсор остановился в пределах этого поля, или сразу, если курсор с этого поля убрать). То же самое надо сделать для полей, где указываются %Ed_CordRefy% и %Ed_CordAutoRefy%, текст всплывающей подсказки "Координата Y". Как это реализовать?

Code: Select all

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%

ProgramName := "Program1"

; Читаем настройки
GoSub ReadSettingsFromIni

MsgBox, 4, %ProgramName%, Вы хотите изменить настройки запуска? `n%ProgramName% пропустит этот шаг через несколько секунд..., 5
	IfMsgBox Yes
	{
		GoSub ShowSettingsWindow
		Return
	}
	else
; Тут или прошло пять секунд, или пользователь нажал Нет.
	{
		GoTo OnButtonRun
		Return
	}

; ===========================
; Конец секции автовыполнения

; Чтение настроек из .ini-файла в переменные
; ==========================================
ReadSettingsFromIni:
{
	IniRead, HotKey_KeyAutoRef , %A_ScriptDir%\..\AppLauncher.ini, Settings, KeyAutoRef , F3
	IniRead, Ed_CordRefx , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefx , 1135
	IniRead, Ed_CordRefy , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefy , 95
	IniRead, Ed_CordAutoRefx , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefx , 1050
	IniRead, Ed_CordAutoRefy , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefy , 95
	Return
}


; Действия по созданию горячих клавиш
; ===================================
DisableCustomHotkeys:
{
	; Прячем ошибки в случае, если была задана некорректная клавиша.
	Try {
		HotKey, %HotKey_KeyAutoRef% , Off
	}
	Return
}

EnableCustomHotkeys:
{
	Try {
		HotKey, %HotKey_KeyAutoRef% , OnKey_AutoRef , On	

	} Catch err {
		GoSub DisableCustomHotkeys
		errorMessage := err.Message
		MsgBox, 48, %ProgramName%, Не удалось создать горячие клавиши!`nСовет: воспользуйтесь документацией по ссылке "Правописание"`n`n%errorMessage%, 5
	}
	
	Return
}


; Создание окна настроек
; ======================
ShowSettingsWindow:
{
	Gui, New
	Gui, Add, Text, Xm Y+m W70 R1 , Клавиша:
	Gui, Add, Edit, X+10 Yp W200 R1 vHotKey_KeyAutoRef , %HotKey_KeyAutoRef%
	Gui, Add, Text, Xm Y+m W280 R1 , Настройка обновлений списка:
	Gui, Add, Text, Xm+20 Y+m W160 R1 , Кнопка Обновить
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordRefx , %Ed_CordRefx%
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordRefy , %Ed_CordRefy%
	Gui, Add, Text, Xm+20 Y+m W160 R1 , Тумблер Автообновление
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordAutoRefx , %Ed_CordAutoRefx%
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordAutoRefy , %Ed_CordAutoRefy%
	
	Gui, Add, Button, Xm Y+m+10 W285 R3 gOnButtonRun , Начать работу
	Gui, Add, Button, Xm Y+m W140 R1 gOnButtonSave , Сохранить
	Gui, Add, Button, X+5 Yp W140 R1 gOnButtonExit , Выйти
	
	Gui, Show, , %ProgramName%
	Return
}


; Действия на кнопки в окне настроек
; ----------------------------------

; Кнопка сохранения настроек
OnButtonSave:
{
	GoSub DisableCustomHotkeys ; Отключаем старые горячие клавиши
	Gui, Submit, NoHide
	GoSub EnableCustomHotkeys ; Включаем новые горячие клавиши
	
	IniWrite, %HotKey_KeyAutoRef% , %A_ScriptDir%\..\AppLauncher.ini, Settings, KeyAutoRef
	IniWrite, %Ed_CordRefx% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefx
	IniWrite, %Ed_CordRefy% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefy
	IniWrite, %Ed_CordAutoRefx% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefx
	IniWrite, %Ed_CordAutoRefy% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefy
	FileSetAttrib, +H, %A_ScriptDir%\.AppLauncer.ini
	Sleep 100
	Msgbox, , %ProgramName%, Настройки успешно сохранены!, 3
	Return
}


; Кнопка запуска
OnButtonRun:
{
	GoSub DisableCustomHotkeys ; Отключаем старые горячие клавиши
	Gui, Submit
	GoSub EnableCustomHotkeys ; Назначаем новые горячие клавиши
	
	TrayTip, Запуск %ProgramName%, Программа %ProgramName% запущена! Для остановки щелкните ПКМ по значку %ProgramName% и выберите "Exit".
	Return
}

; При нажатии Esc, закрытии окна или нажатии кнопки Exit скрипт завершается.
GuiClose:
GuiEscape:
OnButtonExit:
	ExitApp

; Действия для настраиваемой горячей клавиши

OnKey_AutoRef:
{
	MouseMove, %Ed_CordRefx%, %Ed_CordRefy%
	Click, %Ed_CordAutoRefx%, %Ed_CordAutoRefy%
	Return
}
marusa122
Posts: 135
Joined: 22 Mar 2023, 07:48

Re: Как добавить всплывающие подсказки на поля формы?  Topic is solved

25 Aug 2023, 12:24

Вопрос снят, нашла решение здесь, большое спасибо Rseding91.
Скрипт получился таким:

Code: Select all

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%

ProgramName := "Program1"

; Читаем настройки
GoSub ReadSettingsFromIni

MsgBox, 4, %ProgramName%, Вы хотите изменить настройки запуска? `n%ProgramName% пропустит этот шаг через несколько секунд..., 5
	IfMsgBox Yes
	{
		GoSub ShowSettingsWindow
		Return
	}
	else
; Тут или прошло пять секунд, или пользователь нажал Нет.
	{
		GoTo OnButtonRun
		Return
	}

; ===========================
; Конец секции автовыполнения



; Чтение настроек из .ini-файла в переменные
; ==========================================
ReadSettingsFromIni:
{
	IniRead, HotKey_KeyAutoRef , %A_ScriptDir%\..\AppLauncher.ini, Settings, KeyAutoRef , F3
	IniRead, Ed_CordRefx , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefx , 1135
	IniRead, Ed_CordRefy , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefy , 95
	IniRead, Ed_CordAutoRefx , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefx , 1050
	IniRead, Ed_CordAutoRefy , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefy , 95
	Return
}


; Действия по созданию горячих клавиш
; ===================================
DisableCustomHotkeys:
{
	; Прячем ошибки в случае, если была задана некорректная клавиша.
	Try {
		HotKey, %HotKey_KeyAutoRef% , Off
	}
	Return
}

EnableCustomHotkeys:
{
	Try {
		HotKey, %HotKey_KeyAutoRef% , OnKey_AutoRef , On	

	} Catch err {
		GoSub DisableCustomHotkeys
		errorMessage := err.Message
		MsgBox, 48, %ProgramName%, Не удалось создать горячие клавиши!`nСовет: воспользуйтесь документацией по ссылке "Правописание"`n`n%errorMessage%, 5
	}
	
	Return
}


; Создание окна настроек
; ======================
ShowSettingsWindow:
{
	Gui, New
	Gui, Add, Text, Xm Y+m W70 R1 , Клавиша:
	Gui, Add, Edit, X+10 Yp W200 R1 vHotKey_KeyAutoRef , %HotKey_KeyAutoRef%
	Gui, Add, Text, Xm Y+m W280 R1 , Настройка обновлений списка:
	Gui, Add, Text, Xm+20 Y+m W160 R1 , Кнопка Обновить
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordRefx HwndField1Hwnd, %Ed_CordRefx%
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordRefy HwndField2Hwnd, %Ed_CordRefy%
	Gui, Add, Text, Xm+20 Y+m W160 R1 , Тумблер Автообновление
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordAutoRefx HwndField3Hwnd, %Ed_CordAutoRefx%
	Gui, Add, Edit, X+10 Yp W40 R1 vEd_CordAutoRefy HwndField4Hwnd, %Ed_CordAutoRefy%
	
	Gui, Add, Button, Xm Y+m+10 W285 R3 gOnButtonRun , Начать работу
	Gui, Add, Button, Xm Y+m W140 R1 gOnButtonSave , Сохранить
	Gui, Add, Button, X+5 Yp W140 R1 gOnButtonExit , Выйти
	
	AddToolTip(Field1Hwnd, "Координата X")
	AddToolTip(Field2Hwnd, "Координата Y")
	AddToolTip(Field3Hwnd, "Координата X")
	AddToolTip(Field4Hwnd, "Координата Y")

	Gui, Show, , %ProgramName%
	Return
}


; Действия на кнопки в окне настроек
; ----------------------------------

; Кнопка сохранения настроек
OnButtonSave:
{
	GoSub DisableCustomHotkeys ; Отключаем старые горячие клавиши
	Gui, Submit, NoHide
	GoSub EnableCustomHotkeys ; Включаем новые горячие клавиши
	
	IniWrite, %HotKey_KeyAutoRef% , %A_ScriptDir%\..\AppLauncher.ini, Settings, KeyAutoRef
	IniWrite, %Ed_CordRefx% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefx
	IniWrite, %Ed_CordRefy% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordRefy
	IniWrite, %Ed_CordAutoRefx% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefx
	IniWrite, %Ed_CordAutoRefy% , %A_ScriptDir%\..\AppLauncher.ini, Settings, CordAutoRefy
	FileSetAttrib, +H, %A_ScriptDir%\.AppLauncer.ini
	Sleep 100
	Msgbox, , %ProgramName%, Настройки успешно сохранены!, 3
	Return
}


; Кнопка запуска
OnButtonRun:
{
	GoSub DisableCustomHotkeys ; Отключаем старые горячие клавиши
	Gui, Submit
	GoSub EnableCustomHotkeys ; Назначаем новые горячие клавиши
	
	TrayTip, Запуск %ProgramName%, Программа %ProgramName% запущена! Для остановки щелкните ПКМ по значку %ProgramName% и выберите "Exit".
	Return
}
	

; При нажатии Esc, закрытии окна или нажатии кнопки Exit скрипт завершается.
GuiClose:
GuiEscape:
OnButtonExit:
	ExitApp



; Действия для настраиваемых горячих клавиш


OnKey_AutoRef:
{
	MouseMove, %Ed_CordRefx%, %Ed_CordRefy%
	Click, %Ed_CordAutoRefx%, %Ed_CordAutoRefy%
	Return
}

AddToolTip(_CtrlHwnd, _TipText, _Modify = 0)
{
	; Adds Multi-line ToolTips to any Gui Control
	; AHK basic, AHK ANSI, Unicode x86/x64 compatible
	;
	; Thanks Superfraggle & Art: http://www.autohotkey.com/forum/viewtopic.php?p=188241
	; Heavily modified by Rseding91 3/4/2014:
	; Version: 1.0
	;   * Fixed 64 bit support
	;   * Fixed multiple GUI support
	;   * Changed the _Modify parameter
	;           * blank/0/false:                                Create/update the tool tip.
	;           * -1:                                           Delete the tool tip.
	;           * any other value:                              Update an existing tool tip - same as blank/0/false
	;                                                            but skips unnecessary work if the tool tip already
	;                                                            exists - silently fails if it doesn't exist.
	;   * Added clean-up methods:
	;           * AddToolTip(YourGuiHwnd, "Destroy", -1):       Cleans up and erases the cached tool tip data created
	;                                                            for that GUI. Meant to be used in conjunction with
	;                                                            GUI, Destroy.
	;           * AddToolTip(YourGuiHwnd, "Remove All", -1):    Removes all tool tips from every control in the GUI.
	;                                                            Has the same effect as "Destroy" but first removes
	;                                                            every tool tip from every control. This is only used
	;                                                            when you want to remove every tool tip but not destroy
	;                                                            the entire GUI afterwords.
	;           * NOTE: Neither of the above are required if
	;                    your script is closing.
	;			
	; - 'Text' and 'Picture' Controls requires a g-label to be defined.
	; - 'ComboBox' = Drop-Down button + Edit (Get hWnd of the 'Edit'   control using "ControlGet" command).
	; - 'ListView' = ListView + Header       (Get hWnd of the 'Header' control using "ControlGet" command).
	Static TTHwnds, GuiHwnds, Ptr
	, LastGuiHwnd
	, LastTTHwnd
	, TTM_DELTOOLA := 1029
	, TTM_DELTOOLW := 1075
	, TTM_ADDTOOLA := 1028
	, TTM_ADDTOOLW := 1074
	, TTM_UPDATETIPTEXTA := 1036
	, TTM_UPDATETIPTEXTW := 1081
	, TTM_SETMAXTIPWIDTH := 1048
	, WS_POPUP := 0x80000000
	, BS_AUTOCHECKBOX = 0x3
	, CW_USEDEFAULT := 0x80000000
	
	Ptr := A_PtrSize ? "Ptr" : "UInt"
	
	
	; This is used to remove all tool tips from a given GUI and to clean up references used
	;  by those tool tips.
	;
	; This can be used if you want to remove every tool tip but not destroy the GUI
	;  and you don't know each individual control's Hwnd that has a tool tip.
	;
	; When a GUI is destroyed all Windows tool tip related data is cleaned up.
	;  The cached Hwnd's in this function will be removed automatically if the caching code
	;  ever matches them to a new GUI that doesn't actually own the Hwnd's.
	;
	; It's still possible that a new GUI could have the same Hwnd as a previously destroyed GUI
	;  with a child window that also has the same Hwnd as the previous destroyed GUIs tool tip window.
	;
	; If such an event occurred I have no idea what would happen. Either the tool tip
	;  wouldn't work, it may show something crazy or it may crash the script.
	;
	; To avoid that issue, do either of the following:
	;       * Don't destroy a GUI once created
	;       * Call AddToolTip(YourGuiHwnd, "Destroy", -1) directly before destroying the GUI
	;
	; NOTE: You do not need to do the above if you're exiting the script; Windows will clean up
	;  all tool tip related data and the cached Hwnd's in this function are lost when the script
	;  exits anyway.
	
	If (_TipText = "Destroy" Or _TipText = "Remove All" And _Modify = -1)
	{
		; Check if the GuiHwnd exists in the cache list of GuiHwnds
		; If it doesn't exist, no tool tips can exist for the GUI.
		;
		; If it does exist, find the cached TTHwnd for removal.
		Loop, Parse, GuiHwnds, |
			If (A_LoopField = _CtrlHwnd)
			{
				TTHwnd := A_Index
				, TTExists := True
				Loop, Parse, TTHwnds, |
					If (A_Index = TTHwnd)
						TTHwnd := A_LoopField
			}
		
		If (TTExists)
		{
			If (_TipText = "Remove All")
			{
				WinGet, ChildHwnds, ControlListHwnd, ahk_id %_CtrlHwnd%
			
				Loop, Parse, ChildHwnds, `n
					AddToolTip(A_LoopField, "", _Modify) ;Deletes the individual tooltip for a given control if it has one
				
				DllCall("DestroyWindow", Ptr, TTHwnd)
			}
			
			GuiHwnd := _CtrlHwnd
			; This sub removes 'GuiHwnd' and 'TTHwnd' from the cached list of Hwnds
			GoSub, RemoveCachedHwnd
		}
		
		Return
	}
	
	If (!GuiHwnd := DllCall("GetParent", Ptr, _CtrlHwnd, Ptr))
		Return "Invalid control Hwnd: """ _CtrlHwnd """. No parent GUI Hwnd found for control."
	
	; If this GUI is the same one as the potential previous one
	; else look through the list of previous GUIs this function
	; has operated on and find the existing TTHwnd if one exists.
	If (GuiHwnd = LastGuiHwnd)
		TTHwnd := LastTTHwnd
	Else
	{
		Loop, Parse, GuiHwnds, |
			If (A_LoopField = GuiHwnd)
			{
				TTHwnd := A_Index
				Loop, Parse, TTHwnds, |
					If (A_Index = TTHwnd)
						TTHwnd := A_LoopField
			}
	}
	
	; If the TTHwnd isn't owned by the controls parent it's not the correct window handle
	If (TTHwnd And GuiHwnd != DllCall("GetParent", Ptr, TTHwnd, Ptr))
	{
		GoSub, RemoveCachedHwnd
		TTHwnd := ""
	}
	
	; Create a new tooltip window for the control's GUI - only one needs to exist per GUI.
	; The TTHwnd's are cached for re-use in any subsequent calls to this function.
	If (!TTHwnd)
	{
		TTHwnd := DllCall("CreateWindowEx"
						, "UInt", 0                             ;dwExStyle
						, "Str", "TOOLTIPS_CLASS32"             ;lpClassName
						, "UInt", 0                             ;lpWindowName
						, "UInt", WS_POPUP | BS_AUTOCHECKBOX    ;dwStyle
						, "UInt", CW_USEDEFAULT                 ;x
						, "UInt", 0                             ;y
						, "UInt", 0                             ;nWidth
						, "UInt", 0                             ;nHeight
						, "UInt", GuiHwnd                       ;hWndParent
						, "UInt", 0                             ;hMenu
						, "UInt", 0                             ;hInstance
						, "UInt", 0)                            ;lpParam
		
		; TTM_SETWINDOWTHEME
		DllCall("uxtheme\SetWindowTheme"
					, Ptr, TTHwnd
					, Ptr, 0
					, Ptr, 0)
		
		; Record the TTHwnd and GuiHwnd for re-use in any subsequent calls.
		TTHwnds .= (TTHwnds ? "|" : "") TTHwnd
		, GuiHwnds .= (GuiHwnds ? "|" : "") GuiHwnd
	}
	
	; Record the last-used GUIHwnd and TTHwnd for re-use in any immediate future calls.
	LastGuiHwnd := GuiHwnd
	, LastTTHwnd := TTHwnd
	
	
	/*
		*TOOLINFO STRUCT*
		
		UINT        cbSize
		UINT        uFlags
		HWND        hwnd
		UINT_PTR    uId
		RECT        rect
		HINSTANCE   hinst
		LPTSTR      lpszText
		#if (_WIN32_IE >= 0x0300)
			LPARAM    lParam;
		#endif 
		#if (_WIN32_WINNT >= Ox0501)
			void      *lpReserved;
		#endif
	*/
	
	, TInfoSize := 4 + 4 + ((A_PtrSize ? A_PtrSize : 4) * 2) + (4 * 4) + ((A_PtrSize ? A_PtrSize : 4) * 4)
	, Offset := 0
	, Varsetcapacity(TInfo, TInfoSize, 0)
	, Numput(TInfoSize, TInfo, Offset, "UInt"), Offset += 4                         ; cbSize
	, Numput(1 | 16, TInfo, Offset, "UInt"), Offset += 4                            ; uFlags
	, Numput(GuiHwnd, TInfo, Offset, Ptr), Offset += A_PtrSize ? A_PtrSize : 4      ; hwnd
	, Numput(_CtrlHwnd, TInfo, Offset, Ptr), Offset += A_PtrSize ? A_PtrSize : 4    ; UINT_PTR
	, Offset += 16                                                                  ; RECT (not a pointer but the entire RECT)
	, Offset += A_PtrSize ? A_PtrSize : 4                                           ; hinst
	, Numput(&_TipText, TInfo, Offset, Ptr)                                         ; lpszText
	
	
	; The _Modify flag can be used to skip unnecessary removal and creation if
	; the caller follows usage properly but it won't hurt if used incorrectly.
	If (!_Modify Or _Modify = -1)
	{
		If (_Modify = -1)
		{
			; Removes a tool tip if it exists - silently fails if anything goes wrong.
			DllCall("SendMessage"
					, Ptr, TTHwnd
					, "UInt", A_IsUnicode ? TTM_DELTOOLW : TTM_DELTOOLA
					, Ptr, 0
					, Ptr, &TInfo)
			
			Return
		}
		
		; Adds a tool tip and assigns it to a control.
		DllCall("SendMessage"
				, Ptr, TTHwnd
				, "UInt", A_IsUnicode ? TTM_ADDTOOLW : TTM_ADDTOOLA
				, Ptr, 0
				, Ptr, &TInfo)
		
		; Sets the preferred wrap-around width for the tool tip.
		 DllCall("SendMessage"
				, Ptr, TTHwnd
				, "UInt", TTM_SETMAXTIPWIDTH
				, Ptr, 0
				, Ptr, A_ScreenWidth)
	}
	
	; Sets the text of a tool tip - silently fails if anything goes wrong.
	DllCall("SendMessage"
		, Ptr, TTHwnd
		, "UInt", A_IsUnicode ? TTM_UPDATETIPTEXTW : TTM_UPDATETIPTEXTA
		, Ptr, 0
		, Ptr, &TInfo)
	
	Return
	
	
	RemoveCachedHwnd:
		Loop, Parse, GuiHwnds, |
			NewGuiHwnds .= (A_LoopField = GuiHwnd ? "" : ((NewGuiHwnds = "" ? "" : "|") A_LoopField))
		
		Loop, Parse, TTHwnds, |
			NewTTHwnds .= (A_LoopField = TTHwnd ? "" : ((NewTTHwnds = "" ? "" : "|") A_LoopField))
		
		GuiHwnds := NewGuiHwnds
		, TTHwnds := NewTTHwnds
		, LastGuiHwnd := ""
		, LastTTHwnd := ""
	Return
}

Return to “Помощь”

Who is online

Users browsing this forum: No registered users and 67 guests