0

I did a lot of search and research and didn't find a answer for my probleam, so.. I made a custom Menu using WinForm C++, where we override the Renderer and OnPaint methods. Our application can move to one monitor to another and when we do that, the texts of the options on my Menu get corrupted. I don't have any idea or know if this has a solution. I'll be very thankfull if somebody can help me.

Heres the menu right:
menu correct

And the Bug:
Text corrupted

MyRenderer.h

    ref class MyRenderer: ToolStripProfessionalRenderer
    {

    protected:

    virtual void OnRenderToolStripBorder(System::Windows::Forms::ToolStripRenderEventArgs ^e) override
      {

       System::Drawing::SolidBrush^ brush = gcnew System::Drawing::SolidBrush(Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(60)),static_cast<System::Int32>(static_cast<System::Byte>(107))));

       e->Graphics->FillRectangle(brush, e->ConnectedArea);
       Drawing::Rectangle rectBorder = Drawing::Rectangle(0, 1, e->AffectedBounds.Width-1, e->AffectedBounds.Height-2);

       Pen^ pen = gcnew Pen(brush, 2);

       e->Graphics->DrawRectangle(pen, rectBorder);
      }
    };

MenuHover.h

    public ref class MenuHover: ToolStripMenuItem
    {
    protected:
    virtual void OnPaint(PaintEventArgs ^e) override
    {
    if (this->Visible)
    {
        System::Drawing::SolidBrush^ brush = gcnew             System::Drawing::SolidBrush(Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(60)), 
            static_cast<System::Int32>(static_cast<System::Byte>(107))));

        e->Graphics->FillRectangle(brush, e->ClipRectangle.X, e->ClipRectangle.Y, e->ClipRectangle.Width, e->ClipRectangle.Height+34);

        Drawing::Rectangle rect = Drawing::Rectangle(e->ClipRectangle.X, e->ClipRectangle.Y+5, e->ClipRectangle.Width, e->ClipRectangle.Height+34);
        StringFormat^ sf = gcnew StringFormat();
        sf->Alignment = StringAlignment::Center;

        e->Graphics->DrawString(this->Text, this->Font, Brushes::White, rect, sf);
    }

    if (this->Selected || this->Pressed)
    {
        System::Drawing::SolidBrush^ brush = gcnew System::Drawing::SolidBrush(Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(42)), 
            static_cast<System::Int32>(static_cast<System::Byte>(82))));

        e->Graphics->FillRectangle(brush, e->ClipRectangle.X, e->ClipRectangle.Y, e->ClipRectangle.Width, e->ClipRectangle.Height);

        Drawing::Rectangle rect = Drawing::Rectangle(e->ClipRectangle.X, e->ClipRectangle.Y+5, e->ClipRectangle.Width, e->ClipRectangle.Height);

        StringFormat^ sf = gcnew StringFormat();
        sf->Alignment = StringAlignment::Center;

        e->Graphics->DrawString(this->Text, this->Font, Brushes::White, rect, sf);
       }
      }
    };

MenuHoverItem.h

    ref class MenuHoverItem: ToolStripMenuItem
    {
    protected:
    virtual void OnPaint(PaintEventArgs ^e) override
    {
    if (this->Visible)
    {
        System::Drawing::SolidBrush^ brush = gcnew System::Drawing::SolidBrush(Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(42)), 
            static_cast<System::Int32>(static_cast<System::Byte>(82))));

        e->Graphics->FillRectangle(brush, e->ClipRectangle.X, e->ClipRectangle.Y, e->ClipRectangle.Width, e->ClipRectangle.Height);
        Drawing::Rectangle rect = Drawing::Rectangle(e->ClipRectangle.X+14, e->ClipRectangle.Y+1, e->ClipRectangle.Width, e->ClipRectangle.Height);
        StringFormat^ sf = gcnew StringFormat();
        sf->LineAlignment = StringAlignment::Center;

        e->Graphics->DrawString(this->Text, this->Font, Brushes::White, rect, sf);
    }

    if (this->Selected || this->Pressed)
    {
        System::Drawing::SolidBrush^ brush = gcnew System::Drawing::SolidBrush(Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(60)), 
            static_cast<System::Int32>(static_cast<System::Byte>(107))));

        e->Graphics->FillRectangle(brush, e->ClipRectangle.X, e->ClipRectangle.Y, e->ClipRectangle.Width, e->ClipRectangle.Height-1);
        Drawing::Rectangle rect = Drawing::Rectangle(e->ClipRectangle.X+14, e->ClipRectangle.Y+1, e->ClipRectangle.Width, e->ClipRectangle.Height);

        StringFormat^ sf = gcnew StringFormat();
        sf->LineAlignment = StringAlignment::Center;
        e->Graphics->DrawString(this->Text, this->Font, Brushes::White, rect, sf);
       }
      }
    };

If theres a question like that, please send me a link and my I'm sorry if theres anything wrong, thanks for the help

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • It looks like an effect from failure to erase background during an animated window move/size. – Cheers and hth. - Alf Sep 14 '16 at 14:24
  • @Cheersandhth.-Alf I think it get lost on the location when rendering, after I stop moving and do a mouse hover, it get back to normal again, but I don't know if theres a way to fix this – Paloma Costa Sep 14 '16 at 14:29
  • ClipRectangle is probably the issue — don't use it since you are trying to redraw the whole menu. Also, TextRenderer.DrawText should replace the DrawString method since it fixed a lot of drawing issues. DrawString should only be used on a printer graphic. – LarsTech Sep 14 '16 at 15:12

0 Answers0