<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=17760</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17760&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Получить текст диапазона в Notepad++ с помощью Scintilla».]]></description>
		<lastBuildDate>Thu, 27 Apr 2023 04:43:10 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157944#p157944</link>
			<description><![CDATA[<p>Конечно, спасибо, вместо<br /></p><div class="codebox"><pre><code>SendMessage, SCI_GETCURLINE,, remoteBuff.ptr</code></pre></div><p>должно быть<br /></p><div class="codebox"><pre><code>SendMessage, SCI_GETCURLINE, charCount, remoteBuff.ptr</code></pre></div><p>Но странно, что в старой версии Notepad++ с этой ошибкой работало...</p>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Thu, 27 Apr 2023 04:43:10 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157944#p157944</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157943#p157943</link>
			<description><![CDATA[<p>Так смотрите описание SCI_GETCURLINE, там первый параметр должен быть.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Wed, 26 Apr 2023 23:27:26 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157943#p157943</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157941#p157941</link>
			<description><![CDATA[<p>Перешел на Notepad++ 8.5.2. Все нужные мне команды Scintilla работают из AHK, кроме SCI_GETCURLINE — получить текст текущей строки <a href="https://www.scintilla.org/ScintillaDoc.html#SCI_GETCURLINE">https://www.scintilla.org/ScintillaDoc. … GETCURLINE</a>.<br />В Notepad++ 8.2.1 пользовался Вашим кодом <a href="https://forum.script-coding.com/viewtopic.php?pid=112088#p112088">https://forum.script-coding.com/viewtop … 88#p112088</a>, просто поменяв SCI_GETSELTEXT на SCI_GETCURLINE (см. ниже код). И все работало.</p><p>В Notepad++ 8.5.2 команда SCI_GETSELTEXT работает, а SCI_GETCURLINE возвращает пустую строку. Вроде бы SCI_GETCURLINE никак не связана с изменениями структур.</p><p>Что могло случиться?</p><div class="codebox"><pre><code>ТекстТекущейСтроки := ТекстТекущейCтрокиNotepad(WinExist(&quot;ahk_class Notepad++&quot;)) ;получаем текст текущей строки в Notepad++. См. C:\!!Work\AutoHotkey.docx{[Текст_текущей_строки]}
MsgBox ТекстТекущейСтроки: &#039;%ТекстТекущейСтроки%&#039;

ТекстТекущейCтрокиNotepad(hNPP)  {
;Получить текст текущей строки Notepad++

   if !WinExist(&quot;ahk_id&quot; hNPP)  {
      MsgBox, Окно Notepad++ не найдено
      Return
   }
   WinGet, PID, PID
   ControlGetFocus, focused
   if InStr(focused, &quot;Scintilla&quot;)
      ControlGet, hScintilla, hwnd,, % focused
   else
      hScintilla := GetCurrentScintilla(PID)
   
   if !hScintilla  {
      MsgBox, Не удалось определить текущее поле редактирования
      Return
   }
   WinWait, ahk_id %hScintilla%
   ; SendMessage, SCI_GETSELTEXT := 2161 ;заменил
   SendMessage, SCI_GETCURLINE := 2027
      
   charCount := ErrorLevel
   if (charCount &lt; 2)
      Return
   
   remoteBuff := new RemoteBuffer(PID, charCount)
   ; SendMessage, SCI_GETSELTEXT,, remoteBuff.ptr  ;заменил
   SendMessage, SCI_GETCURLINE,, remoteBuff.ptr
   
   VarSetCapacity(localBuff, charCount, 0)
   remoteBuff.Read(&amp;localBuff, charCount)
   SendMessage, SCI_GETCODEPAGE := 2137
   Return StrGet(&amp;localBuff, &quot;CP&quot; . ErrorLevel)
}

GetCurrentScintilla(PID)  {
   remoteBuff := new RemoteBuffer(PID, 4)
   SendMessage, NPPM_GETCURRENTSCINTILLA := 2028,, remoteBuff.ptr
   VarSetCapacity(localBuff, 4, 0)
   remoteBuff.Read(&amp;localBuff, 4)
   
   if handles := GetScintillaHandles()
      Return handles[ NumGet(localBuff, &quot;Int&quot;) + 1 ]
}


GetScintillaHandles()  {
   WinGet, list, ControlList
   WinGet, listHwnd, ControlListHwnd
   arrClassNN := StrSplit(list, &quot;`n&quot;)
   arrHwnd := StrSplit(listHwnd, &quot;`n&quot;)
   for k, v in arrClassNN  {
      if InStr(prevClass, &quot;Scintilla&quot;) &amp;&amp; InStr(v, &quot;Scintilla&quot;)
         Return [ arrHwnd[k - 1], arrHwnd[k] ]
      prevClass := v
   }
}


class RemoteBuffer
{
   __New(PID, size)  {
      static PROCESS_VM_OPERATION := 0x8, PROCESS_VM_WRITE := 0x20, PROCESS_VM_READ := 0x10
           , MEM_COMMIT := 0x1000, PAGE_READWRITE := 0x4
         
      if !(this.hProc := DllCall(&quot;OpenProcess&quot;, UInt, PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE, Int, 0, UInt, PID, Ptr))
         Return
      
      if !(this.ptr := DllCall(&quot;VirtualAllocEx&quot;, UInt, this.hProc, UInt, 0, UInt, size, UInt, MEM_COMMIT, UInt, PAGE_READWRITE, Ptr))
         Return, &quot;&quot;, DllCall(&quot;CloseHandle&quot;, Ptr, this.hProc)
   }
   
   __Delete()  {
      DllCall(&quot;VirtualFreeEx&quot;, Ptr, this.hProc, Ptr, this.ptr, UInt, 0, UInt, MEM_RELEASE := 0x8000)
      DllCall(&quot;CloseHandle&quot;, Ptr, this.hProc)
   }
   
   Read(pLocalBuff, size, offset = 0)  {
      if !DllCall(&quot;ReadProcessMemory&quot;, Ptr, this.hProc, Ptr, this.ptr + offset, Ptr, pLocalBuff, UInt, size, UInt, 0)
         Return, 0, DllCall(&quot;MessageBox&quot;, Ptr, 0, Str, &quot;Не удалось прочитать данные`nОшибка &quot;&quot;&quot; A_LastError &quot;&quot;&quot;&quot;, Str, &quot;&quot;, UInt, 0)
   }
}
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Wed, 26 Apr 2023 22:57:48 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157941#p157941</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157895#p157895</link>
			<description><![CDATA[<p>Да, помню, сначала тоже какие-то плагины отказались работать, потом обновились.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Tue, 25 Apr 2023 18:48:42 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157895#p157895</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157894#p157894</link>
			<description><![CDATA[<p>Обновил Notepad++ до v. 8.5.2, Ваш код работает. И спасибо за инфу, что структура Sci_TextRange изменилась, заодно обновил плагин.</p>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Tue, 25 Apr 2023 18:43:48 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157894#p157894</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157876#p157876</link>
			<description><![CDATA[<div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>Хотя, возможно, у вас старый Notepad++, примерно год назад структура Sci_TextRange изменилась, а как раз старый вариант начал крашить.</p></blockquote></div><p>Точно, у меня Notepad++ v. 8.2.1. В новых версиях что-то не заработало и я остался на старой. Буду бороться с прошлым <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" />.<br />Еще раз большое Вам спасибо и плюс.</p>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Mon, 24 Apr 2023 21:38:33 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157876#p157876</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157875#p157875</link>
			<description><![CDATA[<div class="quotebox"><cite>Vadus пишет:</cite><blockquote><p>Первый способ крашит Notepad++</p></blockquote></div><p>У меня вроде не крашит, работает на Windows 7 и 10. На 11 не тестировал.<br />Хотя, возможно, у вас старый Notepad++, примерно год назад структура Sci_TextRange изменилась, а как раз старый вариант начал крашить.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Mon, 24 Apr 2023 21:28:19 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157875#p157875</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157874#p157874</link>
			<description><![CDATA[<p>Спасибо <strong>teadrinker</strong>!<br />Первый способ крашит Notepad++ без сообщения об ошибке в большинстве случаев. Но один раз вылезла такая:<br /></p><div class="codebox"><pre><code>Error:  Can&#039;t read data from remote buffer
A_LastError: 299

	Line#
	046: {
	047: DllCall(&quot;VirtualFreeEx&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr, &quot;UInt&quot;, 0, &quot;UInt&quot;, MEM_RELEASE := 0x8000)  
	048: DllCall(&quot;CloseHandle&quot;, &quot;Ptr&quot;, this.hProc)  
	049: }
	050: {
	051: VarSetCapacity(localBuff, size, 0)  
	052: if !DllCall(&quot;ReadProcessMemory&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr + offset, &quot;Ptr&quot;, &amp;localBuff, &quot;Ptr&quot;, size, &quot;PtrP&quot;, bytesRead)  
---&gt;	053: Throw,Exception(&quot;Can&#039;t read data from remote buffer
A_LastError: &quot; . A_LastError, &quot;RemoteBuffer.Read&quot;)
	054: Return,bytesRead
	055: }
	056: {
	057: if !res := DllCall(&quot;WriteProcessMemory&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr + offset, &quot;Ptr&quot;, pData, &quot;Ptr&quot;, size, &quot;PtrP&quot;, bytesWritten)  
	058: Throw,Exception(&quot;Can&#039;t write data to remote buffer
A_LastError: &quot; . A_LastError, &quot;RemoteBuffer.Write&quot;)
	059: Return,bytesWritten
	060:\ }
</code></pre></div><p>Использую AHK v. 1.1.33.10.</p><p>Второй вариант настолько гениально прост, что кажется подозрительным. Но работает <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" />.</p>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Mon, 24 Apr 2023 20:53:42 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157874#p157874</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157866#p157866</link>
			<description><![CDATA[<p>Попробуйте так:<br /></p><div class="codebox"><pre><code>ControlGet, hSci, hwnd,, Scintilla1, ahk_exe notepad++.exe
if !hSci
   throw &quot;Не удалось получить хэндл контрола Scintilla&quot;
MsgBox % Sci_GetTextRange(5, 10, hSci)

Sci_GetTextRange(start, end, hSci) {
   static SCI_GETTEXTRANGE := 2162, SCI_GETCODEPAGE := 2137
   WinExist(&quot;ahk_id &quot; . hSci)
   WinGet, PID, PID
   VarSetCapacity(Sci_TextRange, size := A_PtrSize*3, 0)
   RB := new RemoteBuffer(PID, size + end - start + 1)
   NumPut(start, Sci_TextRange)
   NumPut(end, Sci_TextRange, A_PtrSize)
   NumPut(RB.ptr + size, Sci_TextRange, A_PtrSize*2)
   RB.Write(&amp;Sci_TextRange, size)
   SendMessage, SCI_GETTEXTRANGE,, RB.ptr
   textSize := ErrorLevel + 1
   RB.Read(buff, textSize, size)
   SendMessage, SCI_GETCODEPAGE
   Return StrGet(&amp;buff, &quot;CP&quot; . ErrorLevel)
}

class RemoteBuffer
{
   __New(PID, size) {
      static flags := (PROCESS_VM_OPERATION := 0x8) | (PROCESS_VM_WRITE := 0x20) | (PROCESS_VM_READ := 0x10)
			  , Params := [&quot;UInt&quot;, MEM_COMMIT := 0x1000, &quot;UInt&quot;, PAGE_READWRITE := 0x4, &quot;Ptr&quot;]
         
      if !this.hProc := DllCall(&quot;OpenProcess&quot;, &quot;UInt&quot;, flags, &quot;Int&quot;, 0, &quot;UInt&quot;, PID, &quot;Ptr&quot;)
         throw Exception(&quot;Can&#039;t open remote process PID = &quot; . PID . &quot;`nA_LastError: &quot; . A_LastError, &quot;RemoteBuffer.__New&quot;)
      
      if !this.ptr := DllCall(&quot;VirtualAllocEx&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, 0, &quot;Ptr&quot;, size, Params*) {
         DllCall(&quot;CloseHandle&quot;, &quot;Ptr&quot;, this.hProc)
         throw Exception(&quot;Can&#039;t allocate memory in remote process PID = &quot; . PID . &quot;`nA_LastError: &quot; . A_LastError, &quot;RemoteBuffer.__New&quot;)
      }
   }
   __Delete() {
      DllCall(&quot;VirtualFreeEx&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr, &quot;UInt&quot;, 0, &quot;UInt&quot;, MEM_RELEASE := 0x8000)
      DllCall(&quot;CloseHandle&quot;, &quot;Ptr&quot;, this.hProc)
   }  
   Read(ByRef localBuff, size, offset := 0) {
      VarSetCapacity(localBuff, size, 0)
      if !DllCall(&quot;ReadProcessMemory&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr + offset, &quot;Ptr&quot;, &amp;localBuff, &quot;Ptr&quot;, size, &quot;PtrP&quot;, bytesRead)
         throw Exception(&quot;Can&#039;t read data from remote buffer`nA_LastError: &quot; . A_LastError, &quot;RemoteBuffer.Read&quot;)
      Return bytesRead
   }
   Write(pData, size, offset := 0) {
      if !res := DllCall(&quot;WriteProcessMemory&quot;, &quot;Ptr&quot;, this.hProc, &quot;Ptr&quot;, this.ptr + offset, &quot;Ptr&quot;, pData, &quot;Ptr&quot;, size, &quot;PtrP&quot;, bytesWritten)
         throw Exception(&quot;Can&#039;t write data to remote buffer`nA_LastError: &quot; . A_LastError, &quot;RemoteBuffer.Write&quot;)
      Return bytesWritten
   }
}</code></pre></div><p>Хотя можно и так:<br /></p><div class="codebox"><pre><code>ControlGetText, text, Scintilla1, ahk_exe notepad++.exe
MsgBox % SubStr(text, 5, 5)</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Mon, 24 Apr 2023 20:14:55 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157866#p157866</guid>
		</item>
		<item>
			<title><![CDATA[AHK: Получить текст диапазона в Notepad++ с помощью Scintilla]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=157862#p157862</link>
			<description><![CDATA[<p>Всем добрый!<br />Хочу получить текст из диапазона Notepad++ с помощью Scintilla-команды SCI_GETTARGETTEXT <a href="https://www.scintilla.org/ScintillaDoc.html#SCI_GETTARGETTEXT">https://www.scintilla.org/ScintillaDoc. … TARGETTEXT</a>.<br />Не нашел работающего кода, самому не осилить, прошу помощи.</p><p>Есть попытка на AHK, но недопиленная: <a href="https://www.autohotkey.com/boards/viewtopic.php?f=76&amp;t=13428&amp;hilit=SCI_GETTEXTRANGE">https://www.autohotkey.com/boards/viewt … TTEXTRANGE</a>.</p><p>В плагине на С++ нормально работает такой код:<br /></p><div class="codebox"><pre><code>//--- Получаем текст из заданного диапазона TextRange
LPSTR text = (LPSTR)new char[TextLength + 1]; //задаем текстовую переменную, в которую поместим в дальнейшем текст диапазона
Sci_TextRange tr; //создаем объект TextRange — структуру текстового диапазона
	
//Задаем начало диапазона
tr.chrg.cpMin = (int)SelectionEnd; //задаем начало диапазона — конец выделения

//Задаем конец диапазона
tr.chrg.cpMax = (int)SelectionEnd + 1; //задаем конец диапазона — конец выделения + 1

//Задаем начальный текст диапазона, сначала пустой
tr.lpstrText = text;
	
::SendMessage(curScintilla, SCI_GETTEXTRANGE, 0, reinterpret_cast&lt;LPARAM&gt;(&amp;tr)); //получаем текст диапазона и помещаем его в переменную tr

MessageBoxA(NULL, tr.lpstrText, &quot;Текст диапазона&quot;, MB_OK); //выводит текст диапазона
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Vadus)]]></author>
			<pubDate>Mon, 24 Apr 2023 19:43:13 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=157862#p157862</guid>
		</item>
	</channel>
</rss>
