Convert C# to C++/CLI Windows Forms

Tetora, Serhii 21 Reputation points
2022-10-27T09:15:10.557+00:00

I want to display a vertical ControlTab with horizontal text. I have found the following C# code. Please help me to translate this for using with C++/CLI Windows Forms.

using System;  
using System.Drawing;  
using System.Windows.Forms;  
  
class VerticalTabControl : TabControl {  
    public VerticalTabControl() {  
        this.Alignment = TabAlignment.Right;  
        this.DrawMode = TabDrawMode.OwnerDrawFixed;  
        this.SizeMode = TabSizeMode.Fixed;  
        this.ItemSize = new Size(this.Font.Height * 3 / 2, 75);  
    }  
    public override Font Font {  
        get { return base.Font;  }  
        set {  
            base.Font = value;  
    this.ItemSize = new Size(value.Height * 3 / 2, base.ItemSize.Height);  
    }  
    }  
        protected override void OnDrawItem(DrawItemEventArgs e) {  
        using (var _textBrush = new SolidBrush(this.ForeColor)) {  
            TabPage _tabPage = this.TabPages[e.Index];  
            Rectangle _tabBounds = this.GetTabRect(e.Index);  
  
            if (e.State != DrawItemState.Selected) e.DrawBackground();  
            else {  
                using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.White, Color.LightGray, 90f)) {  
                    e.Graphics.FillRectangle(brush, e.Bounds);  
                }  
            }  
  
            StringFormat _stringFlags = new StringFormat();  
            _stringFlags.Alignment = StringAlignment.Center;  
            _stringFlags.LineAlignment = StringAlignment.Center;  
            e.Graphics.DrawString(_tabPage.Text, this.Font, _textBrush, _tabBounds, new StringFormat(_stringFlags));  
        }  
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,500 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,600 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.8K Reputation points
    2022-10-27T14:34:02.52+00:00

    Check this class:

    using namespace System;  
    using namespace System::Drawing;  
    using namespace System::Windows::Forms;  
      
    namespace MyNamespace  
    {  
        public ref class VerticalTabControl : public TabControl  
        {  
            using base = TabControl;  
      
        public:  
      
            VerticalTabControl( )  
            {  
                this->Alignment = TabAlignment::Right;  
                this->DrawMode = TabDrawMode::OwnerDrawFixed;  
                this->SizeMode = TabSizeMode::Fixed;  
                this->ItemSize = System::Drawing::Size( this->Font->Height * 3 / 2, 75 );  
            }  
      
            property System::Drawing::Font^ Font  
            {  
                virtual System::Drawing::Font^ get( ) override  
                {  
                    return base::Font;  
                }  
      
                virtual void set( System::Drawing::Font^ value ) override  
                {  
                    base::Font = value;  
                    this->ItemSize = System::Drawing::Size( value->Height * 3 / 2, base::ItemSize.Height );  
                }  
            }  
      
        protected:  
      
            virtual void OnDrawItem( DrawItemEventArgs^ e ) override  
            {  
                SolidBrush _textBrush( ForeColor );  
      
                TabPage^ _tabPage = TabPages[e->Index];  
                Rectangle _tabBounds = GetTabRect( e->Index );  
      
                if( e->State != DrawItemState::Selected )  
                {  
                    e->DrawBackground( );  
                }  
                else  
                {  
                    Drawing2D::LinearGradientBrush brush( e->Bounds, Color::White, Color::LightGray, 90.0 );  
      
                    e->Graphics->FillRectangle( % brush, e->Bounds );  
                }  
      
                StringFormat _stringFlags;  
                _stringFlags.Alignment = StringAlignment::Center;  
                _stringFlags.LineAlignment = StringAlignment::Center;  
                e->Graphics->DrawString( _tabPage->Text, Font, % _textBrush, _tabBounds, gcnew StringFormat( % _stringFlags ) );  
            }  
        };  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tetora, Serhii 21 Reputation points
    2022-10-27T09:16:32.227+00:00

    I started with:

        using namespace System;  
        using namespace System::Drawing;  
        using namespace System::Windows::Forms;  
          
        ref class VerticalTabControl: public System::Windows::Forms::TabControl {  
          public: VerticalTabControl() {  
           this->Alignment = TabAlignment::Right;  
           this->DrawMode = TabDrawMode::OwnerDrawFixed;  
           this->SizeMode = TabSizeMode::Fixed;  
           this->ItemSize = System::Drawing::Size(this->Font->Height*3/2, 75);  
          }  
        };  
    

    But next I couldn't understand.

    0 comments No comments