xsharp.eu • Setting the font of selected text in a RichEdit control
Page 1 of 1

Setting the font of selected text in a RichEdit control

Posted: Thu May 23, 2024 8:10 am
by Jan
Hello,

I would like to toggle the bold style of a selected text in a RichEdit control.

(XSharp Cahors version 2.19.0.2)

Code: Select all

METHOD ToggleBold() AS VOID

   LOCAL fnt AS Font
   fnt := SELF:Font()
   fnt:Bold := .not. fnt:Bold
   SELF:Font(fnt)
RETURN
Behaviour: Not the bold style of only the selection is changed, but of the full text in the RichEdit control.

Code: Select all

fnt := SELF:FONT()
... takes the font of the selection, and that is what I want, but

Code: Select all

SELF:Font(fnt)
... doesn't apply the font to the selection, but to the full text of the RichEdit control.


My question is: How is it possible to change the font of the selection only?

Re: Setting the font of selected text in a RichEdit control

Posted: Thu May 23, 2024 9:20 am
by ic2
Hello Jan,

In my VO code I use this (taken from an old newsgroup post). I pass the RTF control name as parameter and then the selected text will get bold.

Code: Select all

METHOD MakeBold(oFIELD) CLASS IC2RichEdit
SELF:ChangeCharAttribute(CFM_BOLD,CFE_BOLD)
RETURN NIL

METHOD ChangeCharAttribute(dwMask,dwEffects) CLASS IC2RichEdit
//#s Makes selected text bold/italic/underline 29-05-2006
LOCAL strucCharFormat IS _WINCHARFORMAT
	strucCharFormat.cbSize := _SIZEOF( _WINCHARFORMAT )
// Set the mask to tell oRichEdit to pay attention to the appropriate bit of  the dwEffects PARAMETER
	strucCharFormat.dwMask := dwMask
// Get the current status of the text - wparam true indicates the selected TEXT, FALSE the first char OF oRichedit
	SendMessage(SELF:Handle(),EM_GETCHARFORMAT,DWORD(_CAST,TRUE),LONG(_CAST,@strucCharFormat))
// Toggle the effect.
	strucCharFormat.dwEffects :=_XOR(strucCharFormat.dwEffects,DWORD(_CAST,dwEffects))
// Now update the charformat structure
	SendMessage(SELF:Handle(),EM_SETCHARFORMAT,SCF_SELECTION,LONG(_CAST,@strucCharFormat))
RETURN NIL
Dick

Re: Setting the font of selected text in a RichEdit control

Posted: Thu May 23, 2024 10:52 am
by Luc
Jan,


I have a method doSetBold on an inherited RichEdit like:

CLASS LMORichEdit INHERIT RichEdit

METHOD DOSetBold() CLASS LMORichEdit
LOCAL oFont AS font

INSPECTDEBUG "LMORichEdit set bold"

OFont := SELF:Controlfont // LM 28/1/2022
OFont:bold := TRUE
SELF:ControlFont := oFont // LM 28/1/2022

RETURN NIL


the RTF control has an right click menu that calls the method DoSetBold like:

SELF:RegisterItem(IDM_MenuRTFcontext_X_Set_bold_ID, ;
HyperLabel{#DoSetBold, ;
LoadResString("Set bold",IDS_MEN_SETBOLD,TAAL), ;
LoadResString("Set bold",IDS_MEN_SETBOLD,TAAL), ;
,})


Finally the window to which the richEdit belongs catches the menu command and sends it to the active RTF (multiple RTF controls can be on the same form).



METHOD DOSetBold() CLASS WIBon_InfoTech

RETURN SELF:oActiveRTFcontrol:DOSetBold()


When a selection is made in the richedit control and the right click menu is invoked, the modification (font or bold) is only applied to the selection.

P.S :
the catch is probably in the fact that in vo OFont := SELF:font, and this needs to be changed to SELF:Controlfont now




grtz,

Luc

Re: Setting the font of selected text in a RichEdit control

Posted: Thu May 23, 2024 11:43 am
by Jan
Thank you for your help. Using SELF:ControlFont instead of SELF:Font works well.

Code: Select all

METHOD ToggleBold() AS VOID

   LOCAL fnt AS Font
   fnt := SELF:ControlFont
   fnt:Bold := .not. fnt:Bold
   SELF:ControlFont := fnt

RETURN

Re: Setting the font of selected text in a RichEdit control

Posted: Fri May 24, 2024 6:40 am
by ecos
Jan,

I think you shoul use Dick's suggestion, that is the correct way. My implementation is:

Code: Select all

CLASS MyRichEdit INHERIT RichEdit

METHOD SetBold()

SELF:SetEffect(CFE_BOLD)

RETURN SELF

METHOD setitalic()

SELF:SetEffect(CFE_ITALIC)

RETURN SELF

METHOD SetUnderline()

SELF:SetEffect(CFE_UNDERLINE)

RETURN SELF

METHOD SetEffect(nEffect)
LOCAL cf IS _WINCHARFORMAT

cf.cbSize := _SIZEOF(_WINCHARFORMAT)

// Get the font formatting status into the CHARFORMAT strucuture,
// use the selected text. wParam is TRUE to indicate the
// selected text, FALSE for the first character in the
// RTF Edit control.
SendMessage(SELF:Handle(), EM_GETCHARFORMAT, 1, LONG(_CAST, @cf))
IF _AND(cf.dwEffects,DWORD(_CAST,nEffect)) <> 0
	cf.dwEffects := _XOR(cf.dwEffects,DWORD(_CAST,nEffect))
ELSE
	cf.dwEffects := _OR(cf.dwEffects,DWORD(_CAST,nEffect))
ENDIF
SendMessage(SELF:Handle(), EM_SETCHARFORMAT, _OR(SCF_SELECTION, SCF_WORD), LONG(_CAST, @cf))

RETURN SELF

Karl