Hello
KháchQuaĐường
You can use the `SendMessageW` function to send a message to a RichEdit20W control in C#. This function sends the specified message to a window or windows and calls the window procedure for the specified window, not returning until the window procedure has processed the message¹.
Here is an example of how you might use `SendMessageW` to send a message containing Vietnamese text with accents to a RichEdit20W control:
```csharp
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const uint WM_SETTEXT = 0x000C;
// hWnd is the handle to the RichEdit20W control
string text = "Xin chào! Đây là một tin nhắn Unicode tiếng Việt có dấu";
IntPtr ptr = Marshal.StringToHGlobalUni(text);
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, ptr);
Marshal.FreeHGlobal(ptr);
```
This code uses the `WM_SETTEXT` message to set the text of the RichEdit20W control. The text is first converted to a pointer using `Marshal.StringToHGlobalUni` and then passed as the `lParam` parameter of the `SendMessage` function.
As for sending icons, you may need to use Rich Text Format (RTF) to insert images into the RichEdit20W control. You can find more information on how to do this in C# by searching online.
It seems that you are having trouble getting a RichEdit control to display unicode RTF text. One solution could be to use **UTF-8**, which is an 8-bit encoding but still can represent the full range of Unicode characters. You can get UTF-8 from a PWSTR via **WideCharToMultiByte()**¹. Once you have it in UTF-8, you can use the **EM_SETTEXTEX** parameter with the **codepage** set to **CP_UTF8**¹.
PWSTR stands for Pointer to Wide STRing and is used to represent a pointer to a null-terminated string of 16-bit Unicode characters.
WideCharToMultiByte() is a function that maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.
EM_SETTEXTEX is a message that sets the text of a rich edit control and specifies the character set and code page used to interpret the text.
RichEdit20A and RichEdit20W are two versions of the Rich Edit control. The A and W at the end of the names stand for ANSI and Unicode respectively. The main difference between the two is that RichEdit20A uses ANSI character encoding while RichEdit20W uses Unicode character encoding. This means that RichEdit20A can only display characters from the Windows character set while RichEdit20W can display characters from multiple character sets.
Here is an example of how you can get UTF-8 from a PWSTR via WideCharToMultiByte():
PWSTR WideString = /* Some string... */;
DWORD WideLength = wcslen(WideString) + 1;
PSTR Utf8;
DWORD Length;
INT ReturnedLength;
// A utf8 representation shouldn't be longer than 4 times the size
// of the utf16 one.
Length = WideLength * 4;
Utf8 = (PSTR)malloc(Length);
if (!Utf8) {
/* TODO: handle failure */
}
ReturnedLength = WideCharToMultiByte(
CP_UTF8,
0,
WideString,
WideLength - 1,
Utf8,
Length - 1,
NULL,
NULL
);
if (ReturnedLength) {
// Need to zero terminate...
Utf8[ReturnedLength] = 0;
} else {
/* TODO: handle failure */
}
Once you have it in UTF-8, you can do this:
SETTEXTEX TextInfo = {0};
TextInfo.flags = ST_SELECTION;
TextInfo.codepage = CP_UTF8;
SendMessage(hRichText, EM_SETTEXTEX, (WPARAM)&TextInfo, (LPARAM)Utf8);
Copy
And of course, don’t forget to free the memory allocated for Utf8:
free(Utf8);
PWSTR stands for Pointer to Wide STRing and is used to represent a pointer to a null-terminated string of 16-bit Unicode characters.
WideCharToMultiByte() is a function that maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.
EM_SETTEXTEX is a message that sets the text of a rich edit control and specifies the character set and code page used to interpret the text.
RichEdit20A and RichEdit20W are two versions of the Rich Edit control. The A and W at the end of the names stand for ANSI and Unicode respectively. The main difference between the two is that RichEdit20A uses ANSI character encoding while RichEdit20W uses Unicode character encoding. This means that RichEdit20A can only display characters from the Windows character set while RichEdit20W can display characters from multiple character sets.
(1) SendMessageW function (winuser.h) - Win32 apps | Microsoft Learn.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagew.
(2) c# - Sending Colored Text to richedit20w - Stack Overflow.
https://stackoverflow.com/questions/6746833/sending-colored-text-to-richedit20w.
(3) c# - Find control of specific class in another application - Stack Overflow.
https://stackoverflow.com/questions/28672384/find-control-of-specific-class-in-another-application.
(1) mfc - Unicode RTF text in RichEdit - Stack Overflow.
https://stackoverflow.com/questions/1782409/unicode-rtf-text-in-richedit.
(2) About Rich Edit Controls - Win32 apps | Microsoft Learn.
https://learn.microsoft.com/en-us/windows/win32/controls/about-rich-edit-controls.
(3) MS Rich Edit Control ("RICHEDIT50W") bug? Control does unnecessary font ....
https://learn.microsoft.com/en-us/answers/questions/1118485/ms-rich-edit-control-%28richedit50w%29-bug-control-doe.
Would you like more information on this topic?
Vào lúc 17:29:57 UTC+7 ngày Thứ Sáu, 16 tháng 6, 2023, KháchQuaĐường đã viết: