<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Get selected text CPP]]></title><description><![CDATA[<p dir="auto">HI!</p>
<p dir="auto">I am trying to select text using c++, i used to work with c# and i used the following:</p>
<p dir="auto">int length = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, 0);<br />
IntPtr ptrToText = Marshal.AllocHGlobal(length + 1);<br />
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, length, ptrToText);<br />
string textAnsi = Marshal.PtrToStringAnsi(ptrToText);</p>
<p dir="auto">the first part using c++ will look like:<br />
int length = SendMessage(nppData._scintillaMainHandle, SCI_GETSELTEXT, 0, 0 );</p>
<p dir="auto">But I don’t know how to “translate” the rest… is there an easy way to get/set the current selected text using c++?</p>
<p dir="auto">Thanks in advance.</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/19520/get-selected-text-cpp</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Jul 2026 00:33:21 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/19520.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 06 Jun 2020 05:20:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Get selected text CPP on Sat, 06 Jun 2020 14:31:16 GMT]]></title><description><![CDATA[<p dir="auto">Thank you :)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54734</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54734</guid><dc:creator><![CDATA[NanyaRuriros]]></dc:creator><pubDate>Sat, 06 Jun 2020 14:31:16 GMT</pubDate></item><item><title><![CDATA[Reply to Get selected text CPP on Sat, 06 Jun 2020 14:04:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanyaruriros" aria-label="Profile: NanyaRuriros">@<bdi>NanyaRuriros</bdi></a> said in <a href="/post/54729">Get selected text CPP</a>:</p>
<blockquote>
<p dir="auto">I want to edit the current selected text as a string</p>
</blockquote>
<p dir="auto">Well…go ahead with that. :-)</p>
<p dir="auto">So, from your code you have the string in a “C-style” string, which is a memory buffer that has a zero-byte at the end.  You can manipulate that buffer.</p>
<p dir="auto">But I sense that isn’t what you’re looking for.  As a C# programmer, you want everything “easy”.  Well, in the C/C++ world, things are often not so easy. :-)<br />
You could certainly get your data into a <code>std::string</code> or a <code>std::wstring</code>, to gain more “easiness”. :-)</p>
<p dir="auto">I notice that your buffer is a <code>char *</code> one.<br />
Depending upon what you’re doing, that <em>may</em> be fine.<br />
But it certainly precludes any powerful Unicode handling of the data you are retrieving.<br />
The example code I pointed you to earlier is more appropriate for that kind (Unicode) of handling.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54730</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54730</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sat, 06 Jun 2020 14:04:15 GMT</pubDate></item><item><title><![CDATA[Reply to Get selected text CPP on Sat, 06 Jun 2020 13:42:15 GMT]]></title><description><![CDATA[<p dir="auto">HI!<br />
Thanks for your response, it was pretty interesting to check the source code of n++ :O, currently  I am trying the following:</p>
<pre><code>int selectionEnd = SendMessage(nppData._scintillaMainHandle, SCI_GETSELECTIONEND, 0, 0);
int selectionStart = SendMessage(nppData._scintillaMainHandle, SCI_GETSELECTIONSTART, 0, 0);

char* selection;
if ((selectionEnd &gt; selectionStart))
{
	selection = new char [selectionEnd - selectionStart +1];
	SendMessage(nppData._scintillaMainHandle,SCI_GETSELTEXT,0, reinterpret_cast&lt;LPARAM&gt;(selection));	
}
</code></pre>
<p dir="auto">I think is enough to obtain the current selected test, but also I need the last part:</p>
<p dir="auto">string textAnsi = Marshal.PtrToStringAnsi(ptrToText);</p>
<p dir="auto">I want to edit the current selected text as a string.<br />
Thanks!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54729</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54729</guid><dc:creator><![CDATA[NanyaRuriros]]></dc:creator><pubDate>Sat, 06 Jun 2020 13:42:15 GMT</pubDate></item><item><title><![CDATA[Reply to Get selected text CPP on Sat, 06 Jun 2020 11:43:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanyaruriros" aria-label="Profile: NanyaRuriros">@<bdi>NanyaRuriros</bdi></a></p>
<blockquote>
<p dir="auto">is there an easy way to get/set the current selected text using c++?</p>
</blockquote>
<p dir="auto">A good reference for calling Scintilla functions and working with the results is … drum roll … the Notepad++ source code itself.</p>
<p dir="auto">For what you’re asking about, maybe see <a href="https://github.com/notepad-plus-plus/notepad-plus-plus/blob/5e262c8122cba71aec34edc9cde8994356a2ea46/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp#L2958-L2982" rel="nofollow ugc">HERE</a>, which shows an example of getting the selected text and then setting it to something else.<br />
This specific example is from Notepad++'s <em>Edit</em> menu &gt; <em>Convert Case to</em> &gt; … commands.<br />
Those commands get the selected text from Scintilla, convert the case in local C++ buffers, and then hand the data back to Scintilla by setting the text.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54724</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54724</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sat, 06 Jun 2020 11:43:27 GMT</pubDate></item></channel></rss>