RichEdit controls: get/set text
Posted: 15 Oct 2017, 17:08
I have a function that works to set the text of an internal/external RichEdit control when you specify raw text, or RTF text (text that starts '{\rtf'), but setting the RTF text so far only works with ANSI mode and not Unicode.
Also I have not found a way to get the RTF text directly from an external RichEdit control, note: this is not a priority for me, is this possible? A workaround would be to save the text using WordPad, as an rtf file, or perhaps copy and paste the text into an internal RichEdit control and retrieve the RTF text via EM_STREAMOUT (not yet tested), or if it's possible, to copy the text to the clipboard and retrieve the RTF text somehow.
[Note: I worked on some related functions here: JEE_ClipboardSetHtml and JEE_ClipboardSetRtf]
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 34#p170834
Also I have not found a way to get the RTF text directly from an external RichEdit control, note: this is not a priority for me, is this possible? A workaround would be to save the text using WordPad, as an rtf file, or perhaps copy and paste the text into an internal RichEdit control and retrieve the RTF text via EM_STREAMOUT (not yet tested), or if it's possible, to copy the text to the clipboard and retrieve the RTF text somehow.
Code: Select all
;tested on WordPad (Windows XP and Windows 7 versions)
q:: ;RichEdit control - set text (RTF)
ControlGet, hCtl, Hwnd,, RICHEDIT50W1, A
FormatTime, vDate,, HH:mm dd/MM/yyyy
vRtf := "{\rtf{\b " vDate "}}"
JEE_RichEditSetText(hCtl, vRtf)
return
;==================================================
JEE_RichEditGetText(hCtl)
{
ControlGetText, vText,, % "ahk_id " hCtl
return vText
}
;==================================================
JEE_RichEditSetText(hCtl, vText, vFlags:=0x0, vCP:=0x0)
{
vScriptPID := DllCall("kernel32\GetCurrentProcessId", UInt)
WinGet, vPID, PID, % "ahk_id " hCtl
if (vPID = vScriptPID)
vIsLocal := 1, vPIs64 := (A_PtrSize=8)
;ST_UNICODE := 0x8 ;ST_NEWCHARS := 0x4
;ST_SELECTION := 0x2 ;ST_KEEPUNDO := 0x1
;ST_DEFAULT := 0x0
;CP_ACP := 0 ;Unicode (1200)
VarSetCapacity(SETTEXTEX, 8)
NumPut(vFlags, &SETTEXTEX, 0, "UInt") ;flags
NumPut(vCP, &SETTEXTEX, 4, "UInt") ;codepage
if (vCP = 0)
{
vSize := StrPut(vText, "CP0")
VarSetCapacity(vOutput, vSize)
StrPut(vText, &vOutput, "CP0")
}
else
{
vSize := StrLen(vText)*2+2
vOutput := vText
}
if vIsLocal
SendMessage, 0x461, % &SETTEXTEX, % &vOutput,, % "ahk_id " hCtl ;EM_SETTEXTEX := 0x461
else
{
if !hProc := JEE_DCOpenProcess(0x438, 0, vPID)
return
if !pBuf := JEE_DCVirtualAllocEx(hProc, 0, 8+vSize, 0x3000, 0x4)
return
JEE_DCWriteProcessMemory(hProc, pBuf, &SETTEXTEX, 8, 0)
JEE_DCWriteProcessMemory(hProc, pBuf+8, &vOutput, vSize, 0)
SendMessage, 0x461, % pBuf, % pBuf+8,, % "ahk_id " hCtl ;EM_SETTEXTEX := 0x461
JEE_DCVirtualFreeEx(hProc, pBuf, 0, 0x8000)
JEE_DCCloseHandle(hProc)
}
}
;==================================================
JEE_DCOpenProcess(vAccess, hInherit, vPID)
{
return DllCall("kernel32\OpenProcess", UInt,vAccess, Int,hInherit, UInt,vPID, Ptr)
}
JEE_DCVirtualAllocEx(hProc, vAddress, vSize, vAllocType, vProtect)
{
return DllCall("kernel32\VirtualAllocEx", Ptr,hProc, Ptr,vAddress, UPtr,vSize, UInt,vAllocType, UInt,vProtect, Ptr)
}
JEE_DCWriteProcessMemory(hProc, vBAddress, pBuf, vSize, vWritten)
{
return DllCall("kernel32\WriteProcessMemory", Ptr,hProc, Ptr,vBAddress, Ptr,pBuf, UPtr,vSize, Ptr,vWritten)
}
JEE_DCReadProcessMemory(hProc, vBAddress, pBuf, vSize, vRead)
{
return DllCall("kernel32\ReadProcessMemory", Ptr,hProc, Ptr,vBAddress, Ptr,pBuf, UPtr,vSize, Ptr,vRead)
}
JEE_DCVirtualFreeEx(hProc, vAddress, vSize, vFreeType)
{
return DllCall("kernel32\VirtualFreeEx", Ptr,hProc, Ptr,vAddress, UPtr,vSize, UInt,vFreeType)
}
JEE_DCCloseHandle(hObject) ;e.g. hProc
{
return DllCall("kernel32\CloseHandle", Ptr,hObject)
}
;==================================================
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 34#p170834