Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Color de fondo al texto

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Color de fondo al texto Responder citando

Hola foro
en delphi he logrado poner el color de fondo al texto seleccionado (no el color de letra) de esta forma
RichEdit1.SelAttributes.BackColor := clRed;
pero en c++ aun no encuentro como y ya he buscado bastante en la ayuda incluso alguien sabe como?
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Pudes usar esta manera

Código:
   this->RichEdit1->SelAttributes->Color = clRed;


O bien puedes usar un objeto TTextAttributes *Obj para generar el texto
y posteriormente asignarlo a RichEdit1->SelAttributes


Código:
TTextAttributes *Obj;
Obj->Color = clRed;

this->RichEdit1->SelAttributes = Obj;

_________________
Cool


Ultima edición por Cesar C. Salazar Gonzalez el Sab 07 Feb, 2009 5:29 pm, editado 1 vez
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mensaje    Asunto: Responder citando

Eso te cambia el color del fondo de todo el richEdit pero lo que el creo que pide es unicamente el texto seleccionado si no me equivoco , y eso no se si se puede hacer ,supongo que habra alguna manera aunque yo la desconozco. encontre un codigo de delphi que creo que lo hace pero no fui capaz que funcionara en bcb6

This procedure will search and change the attributes (font name, font size, font color, font style, and back color) of certain words inside a rich edit control. Try the example.

USES RichEdit;
.....................................................
type TTextAttributes = record
Font : TFont;
BackColor : TColor;
end;
.....................................................
.....................................................
.....................................................

procedure SetTextColor(oRichEdit : TRichEdit; sText : String; rAttributes : TTextAttributes);
var
iPos : Integer;
iLen : Integer;

Format: CHARFORMAT2;
begin
FillChar(Format, SizeOf(Format), 0);
Format.cbSize := SizeOf(Format);
Format.dwMask := CFM_BACKCOLOR;
Format.crBackColor := rAttributes.BackColor;

iPos := 0;
iLen := Length(oRichEdit.Lines.Text) ;
iPos := oRichEdit.FindText(sText, iPos, iLen, []);

while iPos <> -1 do begin
oRichEdit.SelStart := iPos;
oRichEdit.SelLength := Length(sText) ;
oRichEdit.SelAttributes.Color := rAttributes.Font.Color;
oRichEdit.SelAttributes.Size := rAttributes.Font.Size;
oRichEdit.SelAttributes.Style := rAttributes.Font.Style;
oRichEdit.SelAttributes.Name := rAttributes.Font.Name;

oRichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));

iPos := oRichEdit.FindText(sText,iPos + Length(sText),iLen, []) ;
end;
end;
.....................................................

Example:
.....................................................
var
rAttrib : TTextAttributes;
begin
RichEdit1.Lines.Add('Delphi is great once you know how to use it.');

rAttrib.Font := TFont.Create;
rAttrib.Font.Color := clWhite;
rAttrib.Font.Size := 16;
rAttrib.Font.Style := [fsBold];
rAttrib.BackColor := clRed;

SetTextColor(RichEdit1,'Delphi',rAttrib);

//Change another word attributes.
rAttrib.Font.Color := clYellow;
rAttrib.Font.Size := 10;
rAttrib.Font.Style := [fsBold,fsItalic];
rAttrib.BackColor := clBlue;

SetTextColor(RichEdit1,'once you',rAttrib);

rAttrib.Font.Free; //Now free the font.
end;
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

1-Cesar agradezco la respuesta pero escribi entre parentesis que no era para el color del texto , sino para el fondo del texto.Ya ese metodo lo conocia gracias....
2-mns te agradezco la ayuda tambien , esto es general para que me ayuden

yo estoy trabajando con el builder 2009 y esa funcion charformat2 y charformat estan bien claras como se utilizan en la ayuda del propio richedit(que por cierto es una muy buena ayuda) lo malo del asunto es que no he sabido interpretar esa ayuda ni el modo de utilizarlos , mns te gradezco pero no he podido convertir ese codigo que me das a c++ SI ALGUIEN PUEDE TRADUCIRLO PORFAVOR LO AGRADECERIA MUCHO...
mandaria la ayuda que trae el c++ 2009 para el richedit en ese caso pero es extensa.... bueno lo hare a continuacion ok?
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

como ya comente anteriormente hay algo relacionado con el barckcolor pero no se usarlo....

The CHARFORMAT2 structure contains information about character formatting in a rich edit control. CHARFORMAT2 is a Microsoft Rich Edit 2.0 extension of the CHARFORMAT structure. Rich Edit 2.0 allows you to use either structure with the EM_GETCHARFORMAT and EM_SETCHARFORMAT messages.

Syntax

typedef struct _charformat2 {
UINT cbSize;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;
BYTE bPitchAndFamily;
TCHAR szFaceName[LF_FACESIZE];
WORD wWeight;
SHORT sSpacing;
COLORREF crBackColor;
LCID lcid;
DWORD dwReserved;
SHORT sStyle;
WORD wKerning;
BYTE bUnderlineType;
BYTE bAnimation;
BYTE bRevAuthor;
BYTE bReserved1;
} CHARFORMAT2;
Members

cbSize
Specifies the size, in bytes, of the structure. Before passing this structure to a rich edit control, set cbSize to the size of the CHARFORMAT or CHARFORMAT2 structure. If cbSize equals the size of a CHARFORMAT structure, the control uses only the CHARFORMAT members.
dwMask
Specifies the parts of the CHARFORMAT2 structure that contain valid information. The dwMask member can be a combination of the values from two sets of bit flags. One set indicates the structure members that are valid. Another set indicates the valid attributes in the dwEffects member.
Set the following values to indicate the valid attributes of the dwEffects member. Value Meaning
CFM_ALLCAPS The CFE_ALLCAPS value is valid.
CFM_BOLD The CFE_BOLD value is valid.
CFM_COLOR The CFE_AUTOCOLOR value is valid, or the crTextColor member is valid.
CFM_DISABLED The CFE_DISABLED value is valid.
CFM_EMBOSS The CFE_EMBOSS value is valid.
CFM_HIDDEN The CFE_HIDDEN value is valid.
CFM_IMPRINT The CFE_IMPRINT value is valid.
CFM_ITALIC The CFE_ITALIC value is valid.
CFM_LINK The CFE_LINK value is valid.
CFM_OUTLINE The CFE_OUTLINE value is valid.
CFM_PROTECTED The CFE_PROTECTED value is valid.
CFM_REVISED The CFE_REVISION value is valid.
CFM_SHADOW The CFE_SHADOW value is valid.
CFM_SMALLCAPS The CFE_SMALLCAPS value is valid.
CFM_STRIKEOUT The CFE_STRIKEOUT value is valid.
CFM_SUBSCRIPT The CFE_SUBSCRIPT and CFE_SUPERSCRIPT values are valid.
CFM_SUPERSCRIPT The CFE_SUBSCRIPT and CFE_SUPERSCRIPT values are valid.
CFM_UNDERLINE. The CFE_UNDERLINE value is valid.


Set the following values to indicate the valid structure members.

CFM_ANIMATION
The bAnimation member is valid.
CFM_BACKCOLOR
The crBackColor member is valid.
CFM_CHARSET
The bCharSet member is valid.
CFM_COLOR
The crTextColor member is valid unless the CFE_AUTOCOLOR flag is set in the dwEffects member.
CFM_FACE
The szFaceName member is valid.
CFM_KERNING
The wKerning member is valid.
CFM_LCID
The lcid member is valid.
CFM_OFFSET
The yOffset member is valid.
CFM_REVAUTHOR
The bRevAuthor member is valid.
CFM_SIZE
The yHeight member is valid.
CFM_SPACING
The sSpacing member is valid.
CFM_STYLE
The sStyle member is valid.
CFM_UNDERLINETYPE
The bUnderlineType member is valid.
CFM_WEIGHT
The wWeight member is valid.
dwEffects
A set of bit flags that specify character effects. Some of the flags are included only for compatibility with Microsoft Text Object Model (TOM) interfaces; the rich edit control stores the value but does not use it to display text.
This member can be a combination of the following values.

CFE_ALLCAPS
Characters are all capital letters. The value does not affect the way the control displays the text. This value applies only to versions earlier than Rich Edit 3.0.
CFE_AUTOBACKCOLOR
The background color is the return value of GetSysColor(COLOR_WINDOW). If this flag is set, crBackColor member is ignored.
CFE_AUTOCOLOR
The text color is the return value of GetSysColor(COLOR_WINDOWTEXT). If this flag is set, the crTextColor member is ignored.
CFE_BOLD
Characters are bold.
CFE_DISABLED
Characters are displayed with a shadow that is offset by 3/4 point or one pixel, whichever is larger.
CFE_EMBOSS
Characters are embossed. The value does not affect how the control displays the text.
CFE_HIDDEN
For Rich Edit 3.0 and later, characters are not displayed.
CFE_IMPRINT
Characters are displayed as imprinted characters. The value does not affect how the control displays the text.
CFE_ITALIC
Characters are italic.
CFE_LINK
A rich edit control can send EN_LINK notification messages when it receives mouse messages while the mouse pointer is over text with the CFE_LINK effect.
CFE_OUTLINE
Characters are displayed as outlined characters. The value does not affect how the control displays the text.
CFE_PROTECTED
Characters are protected; an attempt to modify them will cause an EN_PROTECTED notification message.
CFE_REVISED
Characters are marked as revised.
CFE_SHADOW
Characters are displayed as shadowed characters. The value does not affect how the control displays the text.
CFE_SMALLCAPS
Characters are in small capital letters. The value does not affect how the control displays the text.
CFE_STRIKEOUT
Characters are struck out.
CFE_SUBSCRIPT
Characters are subscript. The CFE_SUPERSCRIPT and CFE_SUBSCRIPT values are mutually exclusive. For both values, the control automatically calculates an offset and a smaller font size. Alternatively, you can use the yHeight and yOffset members to explicitly specify font size and offset for subscript and superscript characters.
CFE_SUPERSCRIPT
Characters are superscript.
CFE_UNDERLINE
Characters are underlined.
yHeight
Specifies the character height, in twips (1/1440 of an inch, or 1/20 of a printer's point). To use this member, set the CFM_SIZE flag in the dwMask member.
yOffset
Character offset from the baseline, in twips,. If the value of this member is positive, the character is a superscript; if the value is negative, the character is a subscript. To use this member, set the CFM_OFFSET flag in the dwMask member.
crTextColor
Text color. To use this member, set the CFM_COLOR flag in the dwMask member. This member is ignored if the CFE_AUTOCOLOR character effect is specified. To generate a COLORREF, use the RGB macro.
bCharSet
Character set value. It can be one of the values specified for the lfCharSet member of the LOGFONT structure. To use this member, set the CFM_CHARSET flag in the dwMask member.
bPitchAndFamily
Specifies the font family and pitch. This member is the same as the lfPitchAndFamily member of the LOGFONT structure.
szFaceName
A null-terminated character array specifying the font name. To use this member, set the CFM_FACE flag in the dwMask member.
wWeight
Font weight. This member is the same as the lfWeight member of the LOGFONT structure. To use this member, set the CFM_WEIGHT flag in the dwMask member.
sSpacing
Horizontal space between letters, in twips. This value has no effect on the text displayed by a rich edit control; it is included for compatibility with Microsoft WindowsText Object Model (TOM) interfaces. To use this member, set the CFM_SPACING flag in the dwMask member.
crBackColor
Background color. To use this member, set the CFM_BACKCOLOR flag in the dwMask member. This member is ignored if the CFE_AUTOBACKCOLOR character effect is specified. To generate a , use the macro.
lcid
A 32-bit locale identifier that contains a language identifier in the lower word and a sorting identifier and reserved value in the upper word. This member has no effect on the text displayed by a rich edit control, but spelling and grammar checkers can use it to deal with language-dependent problems. You can use the macro to create an LCID value. To use this member, set the CFM_LCID flag in the dwMask member.
dwReserved
Reserved; the value must be zero.
sStyle
Character style handle. This value has no effect on the text displayed by a rich edit control; it is included for compatibility with WindowsTOM interfaces. To use this member, set the CFM_STYLE flag in the dwMask member. For more information see the Text Object Model documentation.
wKerning
Value of the font size, above which to kern the character (yHeight). This value has no effect on the text displayed by a rich edit control; it is included for compatibility with TOM interfaces. To use this member, set the CFM_KERNING flag in the dwMask member.
bUnderlineType
Specifies the underline type. To use this member, set the CFM_UNDERLINETYPE flag in the dwMask member. This member can be one of the following values.
CFU_CF1UNDERLINE
The structure maps CHARFORMAT's bit underline to CHARFORMAT2, (that is, it performs a CHARFORMAT type of underline on this text).
CFU_UNDERLINE
Solid underlined text.
CFU_UNDERLINEDOTTED
Dotted underlined text. For versions earlier than Rich Edit 3.0, text is displayed with a solid underline.
CFU_UNDERLINEDOUBLE
Double-underlined text. The rich edit control displays the text with a solid underline.
CFU_UNDERLINENONE
No underline. This is the default.
CFU_UNDERLINEWORD
Underline words only. The rich edit control displays the text with a solid underline.
bAnimation
Text animation type. This value has no effect on the text displayed by a rich edit control; it is included for compatibility with TOM interfaces. To use this member, set the CFM_ANIMATION flag in the dwMask member.
bRevAuthor
An index that identifies the author making a revision. The rich edit control uses different text colors for each different author index. To use this member, set the CFM_REVAUTHOR flag in the dwMask member.
bReserved1
Reserved; the value must be zero.
Structure Information

Header Declared in Richedit.h
Minimum operating systems Windows 95 with Rich Edit 2.0, Windows 98, Windows NT 4.0
Unicode Implemented as ANSI and Unicode versions.
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Lo siguiente quitando unas cuantas líneas puede servirte:

https://cppbuilder.es/viewtopic.php?t=1257
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

_Leo como siempre ...muchas gracias
quiciera hacerte unas preguntas adicionales sobre el tema
1-se podria cambiar esta funcion para que lo que marcara como fondo sea el texto seleccionado y no toda la linea? Me podrias dr un norte porfavor?
2-al estar recorriendo el texto con selstart++ (asi lo hago yo en una funcion que ya tengo hecha ) se podria obtener ese color de fondo (a ver si me explico , tu funcion para mi viene siendo como un set y lo que deseo seria como un get) se puede ?
gracias miles de veras
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Gracias a la muy buena ayuda del foro en especial _Leo Wink y a la ayuda del c++ 2009 ya tengo mis respuestas y la comparto con el foro por si a alguien le sirve de algo.
Advertencia: solo lo he probado en c++ 2009 aunque segun lo que dice _Leo y la ayuda ...
Esto solo funciona en la versión 2 del richedit o superior, por lo que en las primeras versiones de Windows95 sin actualizar podría no funcionar.
asi que un builder 6 todo debe ir viento en popa

declarar la cebecera
Código:
#include <richedit>

en un boton para dar color seria:
boton 1
Código:

::CHARFORMAT2 cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_BACKCOLOR;
cf.crBackColor = ColorToRGB(clRed);

SendMessage(RichEdit1->Handle, EM_SETCHARFORMAT, SCF_SELECTION, (int)&cf);


en un boton dos para obtener color (se pueden hacer muchas modificaciones)
Código:

::CHARFORMAT2 cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_BACKCOLOR;
SendMessage(RE->Handle, EM_GETCHARFORMAT, SCF_SELECTION, (int)&cf);
if (cf.crBackColor == clRed) {
   ShowMessage("es rojo");
}

cualquier sugerencia estoy esperandola ok
saludos y gracias
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

ifrit escribió:
1-se podria cambiar esta funcion para que lo que marcara como fondo sea el texto seleccionado y no toda la linea? Me podrias dr un norte porfavor?

El "norte" ya te lo daba más arriba donde decía: "quitando unas cuantas líneas". Quedaría algo así:
Código:
void PintarFondo(TRichEdit* RichEdit, TColor Color)
{
    // Pintamos el fondo de la selección del color especificado
    ::CHARFORMAT2 cf;
    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_BACKCOLOR;
    cf.crBackColor = ColorToRGB(Color);
    RichEdit->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (int)&cf);
}


ifrit escribió:
tu funcion para mi viene siendo como un set y lo que deseo seria como un get) se puede ?

Si observas con atención lo que tu mismo has puesto más arriba creo que tienes la respuesta: "Rich Edit 2.0 allows you to use either structure with the EM_GETCHARFORMAT and EM_SETCHARFORMAT messages."


[Mensaje editado] :: No me fije en tu último mensaje ya que tenía el navegador abierto en esta página. Pero me alegro de que observaras más atentamente el código y hayas dado con la solución..
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

_Leo de todos modos muchisimas gracias igual tu ayuda siempre es lo maximo
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Aun con dudas
he seguido indagando y he pdido darle la propiedad de oculto al texto con
ejemplo
Código:
hd.dwMask = CFM_HIDDEN;

pero el poder obtenerla no me resulta como le hari apara poder saber que el texto esta oculto?
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.