Setting the font of selected text in a RichEdit control

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Jan
Posts: 10
Joined: Wed Mar 16, 2022 8:19 am
Location: Germany

Setting the font of selected text in a RichEdit control

Post 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?
ic2
Posts: 1861
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

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

Post 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
Luc
Posts: 28
Joined: Sat Apr 30, 2016 7:31 pm
Location: Belgium

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

Post 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
Jan
Posts: 10
Joined: Wed Mar 16, 2022 8:19 am
Location: Germany

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

Post 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
ecos
Posts: 103
Joined: Tue Nov 10, 2015 6:49 am
Location: Germany

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

Post 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
Post Reply