Home All Groups Group Topic Archive Search About

RichTextBox EM_GETCHARFORMAT

Author
28 Nov 2007 9:19 AM
Mark Collard
I'm currently writing an application that has a RichTextBox and a toolbar
which includes the following toolbar buttons:
- Font
- Font Size
- Bold
- Italic
- Underline

I want the toolbox buttons to act the same as the ones in Word. i.e. When
you select text that has different Fonts the Font toolbar button (combo box)
is blanked, and when you select text that has different Font Sizes the Font
Size toolbar button (combo box) is blanked.

The RichTextBox control has a property called SelectionFont. The
SelectionFont property returns null if the selected text contains different
fonts, which is fine if I just wanted to update the Font toolbar button
(combo box), but I also want to update the Font Size, Bold, Italic and
Underline buttons. So I looked for an API solution and found that using the
EM_GETCHARFORMAT message with the SendMessage API would return me the
selected Font, Font Size, Bold, Italic and Underline settings, but it doesn't
tell me when multiple Fonts or Font Sizes have been selected. In these cases
it seems to return the Font and Font Size values of where the cursor is in
the RichTextBox.

How can I workout if the selected text in the RichTextBox control contains
multiple fonts, but the same font size?
And how can I workout if the selected text in the RichTextBox control
contains multiple font sizes, but the same font?

Thank you for your help.

Author
28 Nov 2007 4:55 PM
Nicholas Paldino [.NET/C# MVP]
Mark,

    You can still call SendMessage, passing the EM_GETCHARFORMAT message.
You just need to check the dwMask field to see which attributes are
consistent throughout the selection.

--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"Mark Collard" <MarkColl***@discussions.microsoft.com> wrote in message
news:06693DC1-5148-4C93-A7BE-D47EB2C6CBEC@microsoft.com...
> I'm currently writing an application that has a RichTextBox and a toolbar
> which includes the following toolbar buttons:
> - Font
> - Font Size
> - Bold
> - Italic
> - Underline
>
> I want the toolbox buttons to act the same as the ones in Word. i.e. When
> you select text that has different Fonts the Font toolbar button (combo
> box)
> is blanked, and when you select text that has different Font Sizes the
> Font
> Size toolbar button (combo box) is blanked.
>
> The RichTextBox control has a property called SelectionFont. The
> SelectionFont property returns null if the selected text contains
> different
> fonts, which is fine if I just wanted to update the Font toolbar button
> (combo box), but I also want to update the Font Size, Bold, Italic and
> Underline buttons. So I looked for an API solution and found that using
> the
> EM_GETCHARFORMAT message with the SendMessage API would return me the
> selected Font, Font Size, Bold, Italic and Underline settings, but it
> doesn't
> tell me when multiple Fonts or Font Sizes have been selected. In these
> cases
> it seems to return the Font and Font Size values of where the cursor is in
> the RichTextBox.
>
> How can I workout if the selected text in the RichTextBox control contains
> multiple fonts, but the same font size?
> And how can I workout if the selected text in the RichTextBox control
> contains multiple font sizes, but the same font?
>
> Thank you for your help.
Author
29 Nov 2007 1:49 PM
Mark Collard
Nicholas,

Can you please show me an example on how I would check the dwMask field to
see which attributes are consistent throughout the selection.

Thank you.

Regards
Mark

Show quote
"Nicholas Paldino [.NET/C# MVP]" wrote:

> Mark,
>
>     You can still call SendMessage, passing the EM_GETCHARFORMAT message.
> You just need to check the dwMask field to see which attributes are
> consistent throughout the selection.
>
> --
>           - Nicholas Paldino [.NET/C# MVP]
>           - mvp@spam.guard.caspershouse.com
>
> "Mark Collard" <MarkColl***@discussions.microsoft.com> wrote in message
> news:06693DC1-5148-4C93-A7BE-D47EB2C6CBEC@microsoft.com...
> > I'm currently writing an application that has a RichTextBox and a toolbar
> > which includes the following toolbar buttons:
> > - Font
> > - Font Size
> > - Bold
> > - Italic
> > - Underline
> >
> > I want the toolbox buttons to act the same as the ones in Word. i.e. When
> > you select text that has different Fonts the Font toolbar button (combo
> > box)
> > is blanked, and when you select text that has different Font Sizes the
> > Font
> > Size toolbar button (combo box) is blanked.
> >
> > The RichTextBox control has a property called SelectionFont. The
> > SelectionFont property returns null if the selected text contains
> > different
> > fonts, which is fine if I just wanted to update the Font toolbar button
> > (combo box), but I also want to update the Font Size, Bold, Italic and
> > Underline buttons. So I looked for an API solution and found that using
> > the
> > EM_GETCHARFORMAT message with the SendMessage API would return me the
> > selected Font, Font Size, Bold, Italic and Underline settings, but it
> > doesn't
> > tell me when multiple Fonts or Font Sizes have been selected. In these
> > cases
> > it seems to return the Font and Font Size values of where the cursor is in
> > the RichTextBox.
> >
> > How can I workout if the selected text in the RichTextBox control contains
> > multiple fonts, but the same font size?
> > And how can I workout if the selected text in the RichTextBox control
> > contains multiple font sizes, but the same font?
> >
> > Thank you for your help.
>
>
>

AddThis Social Bookmark Button