Bug in caption FormRibbon

Feb 14, 2013 at 2:25 AM
Hi there,

I'm trying to correct the caption on FormRibbon but i dont right. When the routine use [WinApi.DrawTextOnGlass (....)] she changes temporarily but then it changes back to its previous state. Keeping the font in color white.

Someone fixed this bug?

Please, help!
Feb 14, 2013 at 7:32 AM
Hi there,

I think I don't get you right. What do you mean by "trying to correct the caption"? Is there a problem which you are trying to correct? Or do you want to change the Text of the Caption Bar? Or just the Font or the Color?
Also I think that the function you mentionend "WinApi.DrawTextOnGlass" is never used.
Please clarify.
Feb 14, 2013 at 2:35 PM
Hi there,

The font color is changed when window state it's maximized. Take a look at the examples.

Image
Ribbon Form: Window State = Normal


Image
Ribbon Form: Window State = Maximized



Image
Word Form: Window State = Normal

Image
Word Form: Window State = Maximized

I think this is a bug. The source code is commented.
private void DrawCaptionBarText(Rectangle captionBar, RibbonRenderEventArgs e)
        {
            Form f = e.Ribbon.FindForm();

            if (f == null)
                return;

            StringFormat sf = new StringFormat(); sf.LineAlignment = sf.Alignment = StringAlignment.Center;
            sf.Trimming = StringTrimming.EllipsisCharacter; sf.FormatFlags |= StringFormatFlags.NoWrap;
            Font ft = new Font(SystemFonts.CaptionFont, FontStyle.Regular);

            if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddString(f.Text, ft.FontFamily, (int)ft.Style, ft.SizeInPoints + 3, captionBar, sf);

                    if (f.WindowState != FormWindowState.Maximized)
                    {
                        using (Pen p = new Pen(Color.FromArgb(90, Color.White), 4))
                        {
                            e.Graphics.DrawPath(p, path);
                        }
                    }

                    e.Graphics.FillPath(f.WindowState == FormWindowState.Maximized ? Brushes.White : Brushes.Black, path);
                }
            }
            else if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
            {
                TextRenderer.DrawText(e.Graphics, f.Text, ft, captionBar, ColorTable.FormBorder);
            }
            //Console.WriteLine("capt " + DateTime.Now.Millisecond + e.ClipRectangle.ToString());
            //WinApi.FillForGlass(e.Graphics, captionBar);
            //WinApi.DrawTextOnGlass(e.Ribbon.Handle, f.Text, SystemFonts.CaptionFont, captionBar, 10);
        }
I'm sorry, I think I was not really clear. ;)

Regards
Feb 14, 2013 at 2:53 PM
Hi,

Now it makes sense! The problem is that I am still working on XP.
But I think you have already located the relevant section above:
            if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddString(f.Text, ft.FontFamily, (int)ft.Style, ft.SizeInPoints + 3, captionBar, sf);

                    if (f.WindowState != FormWindowState.Maximized)
                    {
                        using (Pen p = new Pen(Color.FromArgb(90, Color.White), 4))
                        {
                            e.Graphics.DrawPath(p, path);
                        }
                    }

                    e.Graphics.FillPath(f.WindowState == FormWindowState.Maximized ? Brushes.White : Brushes.Black, path);
                }
            }
I guess you have to change the colors to your requirements.

Please tell me if that worked!
Feb 14, 2013 at 5:06 PM
Hi,

I´m trying two codes:

1) Using [WinApi.DrawTextOnGlass(...)]
  • It was identical to the model of MS-Word, but by rolling the mouse over a RibbonPannel the caption disappears. It's very strange!
if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddString(f.Text, ft.FontFamily, (int)ft.Style, ft.SizeInPoints + 3, captionBar, sf);
                    if (Environment.OSVersion.Version.Major >= 6) {
                        //e.Graphics.DrawPath(new Pen(Color.FromArgb(75, Color.White), 4), path);
                        //e.Graphics.FillPath(Brushes.Black, path);

                        WinApi.DrawTextOnGlass(e.Ribbon.Handle, f.Text, SystemFonts.CaptionFont, captionBar, 10);
                    } else {
                        if (f.WindowState != FormWindowState.Maximized)
                        {
                            using (Pen p = new Pen(Color.FromArgb(90, Color.White), 4))
                            {
                                e.Graphics.DrawPath(p, path);
                            }
                        }
                        e.Graphics.FillPath(f.WindowState == FormWindowState.Maximized ? Brushes.White : Brushes.Black, path);
                    }
                }
            }
            else if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
            {
                TextRenderer.DrawText(e.Graphics, f.Text, ft, captionBar, ColorTable.FormBorder);
            }
2) Changing manually
  • It was similar to the model, but I think it would be better if use [WinApi.DrawTextOnGlass (...)]
if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddString(f.Text, ft.FontFamily, (int)ft.Style, ft.SizeInPoints + 3, captionBar, sf);
                    if (Environment.OSVersion.Version.Major >= 6) {
                        e.Graphics.DrawPath(new Pen(Color.FromArgb(75, Color.White), 4), path);
                        e.Graphics.FillPath(Brushes.Black, path);

                        //WinApi.DrawTextOnGlass(e.Ribbon.Handle, f.Text, SystemFonts.CaptionFont, captionBar, 10);
                    } else {
                        if (f.WindowState != FormWindowState.Maximized)
                        {
                            using (Pen p = new Pen(Color.FromArgb(90, Color.White), 4))
                            {
                                e.Graphics.DrawPath(p, path);
                            }
                        }
                        e.Graphics.FillPath(f.WindowState == FormWindowState.Maximized ? Brushes.White : Brushes.Black, path);
                    }
                }
            }
            else if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
            {
                TextRenderer.DrawText(e.Graphics, f.Text, ft, captionBar, ColorTable.FormBorder);
            }
Temporarily left my code (2), until I find the problem of the "mouse over" within RibbonPannel. Also put a check version of Windows to not disrupt your work, but if you possibly fix before, I appreciate it.

Regards
Feb 15, 2013 at 7:22 AM
Can you post screenshots?
Feb 15, 2013 at 2:59 PM
Of course!

Image
Vista Minimized

Image
Vista Maximized

Image
XP Minimized

Image
XP Maximized

I'm still working on the first code above. But, i still have not tested in Vista without windows glass.
If anyone can test on Vista without windows glass, I am grateful.

Regards
Feb 18, 2013 at 7:50 AM
OK. I think I got you.
Some additionaly information: your checks for OS version can be skipped. If you are inside "e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass" then Aero is enabled - so it is at least Vista.

Change the line (from original source):
e.Graphics.FillPath(f.WindowState == FormWindowState.Maximized ? Brushes.White : Brushes.Black, path);
to:
e.Graphics.FillPath(f.WindowState == Brushes.Black, path);
That should do it.
Feb 18, 2013 at 5:18 PM
Really makes sense.

I think I got confused, on your explanation. [ f.WindowState == Brushes.Black ]?
Or, the code will look like this?
            if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaGlass)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddString(f.Text, ft.FontFamily, (int)ft.Style, ft.SizeInPoints + 3, captionBar, sf);
                    e.Graphics.DrawPath(new Pen(Color.FromArgb(75, Color.White), 4), path);
                    e.Graphics.FillPath(Brushes.Black, path);
                }
            }
            else if (e.Ribbon.ActualBorderMode == RibbonWindowMode.NonClientAreaCustomDrawn)
            {
                TextRenderer.DrawText(e.Graphics, f.Text, ft, captionBar, ColorTable.FormBorder);
            }
The code above works wonderfully!

Regards
Feb 19, 2013 at 7:26 AM
I don't what happend to me when I was typing that non-sense.
You got me right nevertheless ;-)
Feb 19, 2013 at 3:04 PM
This has happened to me too. =]
Apr 28, 2013 at 7:26 PM
If you still read this, I got it fixed!
Now the caption is not hiding any more without any reason if you move the mouse outside the ribbon?!?!
https://officeribbon.codeplex.com/SourceControl/changeset/21514