Re: Changing color and Font of CString



You might as well try to change the font and color of an integer. It doesn't make any
sense. A CString is just that: a sequence of characters. Concepts like "font" and
"color" have no meaning to a sequence of characters stored in memory.

There are some other problems, also. Why are you assuming that you are using only 8-bit
characters? Why are you still using a concept as out-of-date as strcpy, which isn't safe?
Why are you using strcpy at all, since there is little if any need for it in MFC?

If you want text to have properties, you do this at the time it is displayed. To get text
selectively displayed with color and underlining, you would use a CRichEditCtrl to display
it. You would then have to use CRichEditCtrl::SetSelectCharFormat to set the format of
those characters, which means you have to know where they are in the rich edit control.
You would fill in a CHARFORMAT or CHARFORMAT2 structure, setting the style properties you
want and the appropriate style bits, then setting the selection to be the word MAN, and
calling SetSelectionCharFormat.

Now, that said, it *is* possible to encode font and color information in a string, but I
don't think this is what you were thinking of. For example, if you want MAN to be bold
and underlined, such as in

I said, "MAN, you are cool"

then you could store that as a CString that *encoded* the color information, e.g.,

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 I said, "\ul\b MAN\ulnone\b0 , you are cool"\par
}

and use the CRichEditCtrl::StreamIn instead of SetWindowText to load this string into the
Rich Edit control. Note that some of this is "overhead" such as setting the font and
informing the system what code page you used for the text, so you could have just used

I said, "\ul\b MAN\ulnone\b0 , you are cool"

To get the desired rtf, the easiest way is to save a file using WordPad and then read it
with an ordinary text editor, such as NotePad. And you need to read the rtf documentation
as well.

Note that this requires special handling, and it does not set the characteristics of the
CString, it merely encodes the information in a way that a rendering control, when
properly fed the string, will cause it to be rendered in the way you want. Needless to
say, the length of the string is no longer the 3 of MAN, but about 20 (if I've counted
right) just for that one word (that space after \b0 counts). And this will *only* have an
effect when it is streamed into a Rich Edit control. The rest of the time, it is just
characters in a CString, so you couldn't search for "MAN," in the string, because it
doesn't exist as that text.

The correct way of copying a string, should you be so silly as to need to copy a string
into an old-fashioned buffer, is

StringCchCopy(czVal, sizeof(czVal)/sizeof(TCHAR), _T("MAN"));
(you have to #include <strsafe.h>)
or, if you are using VS.NET 2005,
_tcscpy_s(czVale, sizeof(czVal)/sizeof(TCHAR), _T("MAN"));

where I'm presuming czVal is declared as
TCHAR czVal[somesizehere];

but this seems an odd thing to want to do in an MFC program, since there is little reason
to have such a variable.

joe

On 5 Sep 2006 05:27:40 -0700, "AnuBala" <anu_bala2003@xxxxxxxxxxx> wrote:

Hi,
iam copying a text to a CString variable,Here can i change the color
and font of the text in my coding.
Like strcpy(czVal,"MAN");
here MAN should be bold and underline...
How can i chnge this,,Is thre anyway?
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Russian language support
    ... Actually in our previous test we didnt do language transition properly ... bytes, or if in Unicode, just a string of 2-byte values, each of which is ... it selects a font to use to display ... TTF fonts in Windows CE map Unicode characters into suitable glyphs. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: CListCtrl unicode doesnt display korean characters correctly
    ... Your assumptions are ok about the not described types, Strings are CString, not arrays of TCHARs ... It is a font from Microsoft. ... The korean string is in case 2. ... The LoadString method gets it from the resources. ...
    (microsoft.public.vc.mfc)
  • Re: CFileDialog drives me insane. Handle Problem ?
    ... The basic type you care about is CString. ... This is the equivalent to the Java 'String' ... There are rare cases in which you need a LPTSTR pointer, ... const wchar_t * - const pointer to Unicode characters. ...
    (microsoft.public.vc.mfc)
  • Re: CFileDialog drives me insane. Handle Problem ?
    ... The basic type you care about is CString. ... This is the equivalent to the Java 'String' ... There are rare cases in which you need a LPTSTR pointer, ... const wchar_t * - const pointer to Unicode characters. ...
    (microsoft.public.vc.mfc)
  • Re: CString and UTF-8
    ... installing a machine that isn't a standard locale and see how CString ... as multibyte characters, there are two chinese characters in there. ... ASCII string with UTF-8 embedded into it, delimited with quotes, the ...
    (microsoft.public.vc.mfc)