Re: Changing color and Font of CString
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 05 Sep 2006 09:53:33 -0400
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,Joseph M. Newcomer [MVP]
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?
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Changing color and Font of CString
- From: AnuBala
- Changing color and Font of CString
- Prev by Date: Re: Add a CDialog variable to a class
- Next by Date: Re: Win32 app has problems running on 64-bit edition of XP (functions fail)
- Previous by thread: Re: Changing color and Font of CString
- Next by thread: Re: Changing color and Font of CString
- Index(es):
Relevant Pages
|