How do I use Win32 APIs to return selected RICH text (not plain text or all text) from a RichEdit control?

Robert Gustafson 606 Reputation points
2023-02-10T07:23:43.2966667+00:00

WHAT I HAVE:

Visual Basic 2019, WinForms, .NET Framework 4+

MY PROBLEM:

I've found that when it comes to using RichTextBox.SelectedRtf, hidden (invisible) rich text isn't always returned in the string, depending on the .NET version and where in a selection text is hidden. (This isn't a problem when returning rich text for the entire document via Rtf.) When it comes to getting selected plain text, I can get the entire selection, including all hidden text, using the non-public property of WindowText, and the non-public fields of curSelStart and curSelEnd; this doesn't work for ferreting out an RTF string of the selection!

Can I use Win32APIs--particularly the EM_GETTEXTEX, EM_GETTEXTLENGTHEX, and/or EM_GETSELTEXT messages, and/or the GETTEXTEX and GETTEXTLENGTHEX types--to get a more fine-tuned version of the RTF (not plain) text for a selection? In any case, I would like to have the values for the necessary constants and data types in VB.NET to do such a thing. I'm not altogether fluent with converting C++ type-names to VB.NET ones, and there seems to be scant documentation on richedit.h constants' values. (I did find one website with the necessary constant and type definitions, but I still need to know how to express it in VB.NET, particularly when it comes to assigning the right [angle-bracket] attributes to the necessary types, fields, and declares; handling interop between managed and unmanaged stuff isn't my specialty, since I don't use Win32 APIs regularly. BTW, I don't need code for all the rich-edit stuff--just those needed for the aforementioned task at hand.)

Please give me answers ASAP, in VB.NET, and as simply as possible. (BTW, if this posts' content seems a little familiar, it's because I'm not sure if I've asked the question the right way before, and I haven't gotten any info on this specific issue--using Win32 API to get selected RTF text--yet.)

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,924 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,749 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.