Что нового

[Элементы GUI] _GuiCtrlRichEdit_SetSel - как выделить по слову ?

Ranos1982

Новичок
Сообщения
130
Репутация
1
Не подскажите,как выделить слово не по координатам (начало и конец слова),а по...слову?Например там есть слово error -вот мне то и нужно его выделить и потом придать красный цвет....(простите,если не так выразился,но надеюсь поймете) . Как цвет придать выделенному - уже знаю как.А вот как выделить по слову....все же голову ломаю :whistle:

ф-ция выделения по (!) координатам:

Код:
_GuiCtrlRichEdit_SetSel
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Ranos1982 [?]
как выделить слово не по координатам (начало и конец слова),а по...слову?
Найти его сначала :smile:

Код:
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIRichEdit.au3>

Global $iLast_Pos = 0
Global $sLast_Search = ""

$hGUI = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)

$hRichEdit = _GUICtrlRichEdit_Create($hGUI, "This is a test, no errors here.", 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
$nSearch_Bttn = GUICtrlCreateButton("Search", 140, 310, 70, 30)
$nSearchNext_Bttn = GUICtrlCreateButton("Search Next", 220, 310, 90, 30)

GUISetState()

While 1
	$iMsg = GUIGetMsg()
	
	Switch $iMsg
		Case $GUI_EVENT_CLOSE
			GUIDelete()
			Exit
		Case $nSearch_Bttn
			$sLast_Search = InputBox("Search in RichEdit", "Enter search request:", $sLast_Search, "", -1, -1, Default, Default, 0, $hGUI)
			If Not @error Then ContinueCase
		Case $nSearchNext_Bttn
			If $sLast_Search = "" Then ContinueLoop
			
			_GUICtrlRichEdit_SetSel($hRichEdit, $iLast_Pos, $iLast_Pos)
			
			$iAnchor = _GUICtrlRichEdit_FindText($hRichEdit, $sLast_Search)
			$iActive = $iAnchor + StringLen($sLast_Search)
			
			_GUICtrlRichEdit_SetSel($hRichEdit, $iAnchor, $iActive)
			
			$iLast_Pos = $iActive
			
			If $iLast_Pos = 0 Or $iAnchor = -1 Then
				$iAsk = MsgBox(52, 'Attention', 'End of text, serach from the begining?', 0, $hGUI)
				
				If $iAsk = 6 Then
					ControlClick($hGUI, "", $nSearchNext_Bttn)
				EndIf
			EndIf
	EndSwitch
WEnd
 
Автор
R

Ranos1982

Новичок
Сообщения
130
Репутация
1
Не понимаю,в чем ошибка :whistle:

Пытаюсь сделать ф-цию :

Код:
Func findSetSel ($hRichEdit,$stringSearch)

		   Local $iLast_Pos = 0

           If $stringSearch = "" Then

            _GUICtrlRichEdit_SetSel($hRichEdit, $iLast_Pos, $iLast_Pos)
            $iAnchor = _GUICtrlRichEdit_FindText($hRichEdit, $stringSearch)
            $iActive = $iAnchor + StringLen($stringSearch)
            _GUICtrlRichEdit_SetSel($hRichEdit, $iAnchor, $iActive)
			
		EndIf

EndFunc


Как таковой ошибки нет,как и собственно результата.
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Ranos1982 [?]
В том что условие поставлено неверно, нужно проверять «If $stringSearch <> "" Then» а не через «= ""».
 
Автор
R

Ranos1982

Новичок
Сообщения
130
Репутация
1
Изменил - чтото тоже не работает...ищу дальше , там* то - работает (в твоем скрипте) ...просто не хочу убивать не один час :(

Код:
Func findSetSel ($hRichEdit,$stringSearch)
   
		   Local $iLast_Pos = 0

            If $stringSearch <> ""Then

            _GUICtrlRichEdit_SetSel($hRichEdit, $iLast_Pos, $iLast_Pos)            ; устанавливаем начальную позицию
            $iAnchor = _GUICtrlRichEdit_FindText($hRichEdit, $stringSearch)  ; возвращаем координаты начала слова
            $iActive = $iAnchor + StringLen($stringSearch)                                   ; возвращаем координаты конца слова слова 
            _GUICtrlRichEdit_SetSel($hRichEdit, $iAnchor, $iActive)                    ; выделяем ..

		EndIf

EndFunc
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Ranos1982 [?]
Изменил - чтото тоже не работает
“Не работает” это слишком смутно, нужно конкретно определиться что значит работает, а что нет.

У меня “работает”:

Код:
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIRichEdit.au3>

Global $sLast_Search = ""

$hGUI = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)

$hRichEdit = _GUICtrlRichEdit_Create($hGUI, "This is a test, no errors here.", 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
$nSearch_Bttn = GUICtrlCreateButton("Search", 140, 310, 70, 30)

GUISetState()

While 1
    $iMsg = GUIGetMsg()
    
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
        Case $nSearch_Bttn
            $sLast_Search = InputBox("Search in RichEdit", "Enter search request:", $sLast_Search, "", -1, -1, Default, Default, 0, $hGUI)
            If @error Or $sLast_Search = "" Then ContinueLoop
			
			_GUICtrlRichEdit_FindAndSelText($hRichEdit, $sLast_Search)
    EndSwitch
WEnd

Func _GUICtrlRichEdit_FindAndSelText($hRichEdit, $sText)
	If $sText = "" Then
		Return SetError(1)
	EndIf
	
	Local $iAnchor, $iActive
	
	_GUICtrlRichEdit_SetSel($hRichEdit, 0, 0)
	
	$iAnchor = _GUICtrlRichEdit_FindText($hRichEdit, $sText)
	$iActive = $iAnchor + StringLen($sText)
	
	Return _GUICtrlRichEdit_SetSel($hRichEdit, $iAnchor, $iActive)
EndFunc
 
Автор
R

Ranos1982

Новичок
Сообщения
130
Репутация
1
Прошу прощения , все работает , проблема у меня :smile: :whistle:
 
Автор
R

Ranos1982

Новичок
Сообщения
130
Репутация
1
Не получается сделать вот что ....чтобы выделялись ВСЕ слова.В том примере работает,а вот в виде ф-ции - не получается... :(

Код:
Func findSetSel($hRichEdit, $searchSring)

			_GUICtrlRichEdit_SetSel($hRichEdit, 0, 0)
			$iAnchor = _GUICtrlRichEdit_FindText($hRichEdit, $searchSring)
			$iActive = $iAnchor + StringLen($searchSring)
			 $resultSetSel = _GUICtrlRichEdit_SetSel($hRichEdit, $iAnchor, $iActive)
			_GUICtrlRichEdit_SetCharColor($hRichEdit, 0xFF0000)
			Return $resultSetSel

EndFunc   ;==>findSetSel
:whistle:

Все,решил уже..)
К счастью тему можно считать решеной!)
 
Верх