简体   繁体   English

MaterialSkin从右到左的自定义控件

[英]MaterialSkin Right to Left custom Control

I'm using MaterialSkin UI controls , how can I change the control properties to Right to left, because it's by default Left to Right. 我正在使用MaterialSkin UI控件,如何将控件属性更改为从右到左,因为默认情况下从左到右。

I think this is the code should be edited : 我认为这是应该编辑的代码:

  private void UpdateTabRects()
    {
        _tabRects = new List<Rectangle>();

        //If there isn't a base tab control, the rects shouldn't be calculated
        //If there aren't tab pages in the base tab control, the list should just be empty which has been set already; exit the void
        if (_baseTabControl == null || _baseTabControl.TabCount == 0) return;

        //Calculate the bounds of each tab header specified in the base tab control
        using (var b = new Bitmap(1, 1))
        {
            using (var g = Graphics.FromImage(b))
            {
                _tabRects.Add(new Rectangle(SkinManager.FormPadding, 0, TabHeaderPadding * 2 + (int)g.MeasureString(_baseTabControl.TabPages[0].Text, SkinManager.Font_Size11).Width, Height));
                for (int i = 1; i < _baseTabControl.TabPages.Count; i++)
                {
                    _tabRects.Add(new Rectangle(_tabRects[i - 1].Right, 0, TabHeaderPadding * 2 + (int)g.MeasureString(_baseTabControl.TabPages[i].Text , SkinManager.Font_Size11).Width , Height));
                }
            }
        }
    }

and this is the full code for the control : 这是控件的完整代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using MaterialSkin.Animations;

namespace MaterialSkin.Controls
 {
  public class MaterialTabSelector : Control, IMaterialControl
{
    [Browsable(false)]
    public int Depth { get; set; }
    [Browsable(false)]
    public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
    [Browsable(false)]
    public MouseState MouseState { get; set; }

    private MaterialTabControl _baseTabControl;
    public MaterialTabControl BaseTabControl
    {
        get { return _baseTabControl; }
        set
        {
            _baseTabControl = value;
            if (_baseTabControl == null) return;
            _previousSelectedTabIndex = _baseTabControl.SelectedIndex;
            _baseTabControl.Deselected += (sender, args) =>
            {
                _previousSelectedTabIndex = _baseTabControl.SelectedIndex;
            };
            _baseTabControl.SelectedIndexChanged += (sender, args) =>
            {
                _animationManager.SetProgress(0);
                _animationManager.StartNewAnimation(AnimationDirection.In);
            };
            _baseTabControl.ControlAdded += delegate
            {
                Invalidate();
            };
            _baseTabControl.ControlRemoved += delegate
            {
                Invalidate();
            };
        }
    }

    private int _previousSelectedTabIndex;
    private Point _animationSource;
    private readonly AnimationManager _animationManager;

    private List<Rectangle> _tabRects;
    private const int TabHeaderPadding = 24;
    private const int TabIndicatorHeight = 2;

    public MaterialTabSelector()
    {
        SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
        Height = 48;

        _animationManager = new AnimationManager
        {
            AnimationType = AnimationType.EaseOut,
            Increment = 0.04
        };
        _animationManager.OnAnimationProgress += sender => Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;
        g.TextRenderingHint = TextRenderingHint.AntiAlias;

        g.Clear(SkinManager.ColorScheme.PrimaryColor);

        if (_baseTabControl == null) return;

        if (!_animationManager.IsAnimating() || _tabRects == null || _tabRects.Count != _baseTabControl.TabCount)
            UpdateTabRects();

        var animationProgress = _animationManager.GetProgress();

        //Click feedback
        if (_animationManager.IsAnimating())
        {
            var rippleBrush = new SolidBrush(Color.FromArgb((int)(51 - (animationProgress * 50)), Color.White));
            var rippleSize = (int)(animationProgress * _tabRects[_baseTabControl.SelectedIndex].Width * 1.75);

            g.SetClip(_tabRects[_baseTabControl.SelectedIndex]);
            g.FillEllipse(rippleBrush, new Rectangle(_animationSource.X - rippleSize / 2, _animationSource.Y - rippleSize / 2, rippleSize, rippleSize));
            g.ResetClip();
            rippleBrush.Dispose();
        }

        //Draw tab headers
        foreach (TabPage tabPage in _baseTabControl.TabPages)
        {
            var currentTabIndex = _baseTabControl.TabPages.IndexOf(tabPage);
            Brush textBrush = new SolidBrush(Color.FromArgb(CalculateTextAlpha(currentTabIndex, animationProgress), SkinManager.ColorScheme.TextColor));

            g.DrawString(tabPage.Text.ToUpper(), SkinManager.Font_Size11, textBrush, _tabRects[currentTabIndex], new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            textBrush.Dispose();
        }

        //Animate tab indicator
        var previousSelectedTabIndexIfHasOne = _previousSelectedTabIndex == -1 ? _baseTabControl.SelectedIndex : _previousSelectedTabIndex;
        var previousActiveTabRect = _tabRects[previousSelectedTabIndexIfHasOne];
        var activeTabPageRect = _tabRects[_baseTabControl.SelectedIndex];

        var y = activeTabPageRect.Bottom - 2;
        var x = previousActiveTabRect.X + (int)((activeTabPageRect.X - previousActiveTabRect.X) * animationProgress);
        var width = previousActiveTabRect.Width + (int)((activeTabPageRect.Width - previousActiveTabRect.Width) * animationProgress);

        g.FillRectangle(SkinManager.ColorScheme.AccentBrush, x, y, width, TabIndicatorHeight);
    }

    private int CalculateTextAlpha(int tabIndex, double animationProgress)
    {
        int primaryA = SkinManager.ActionBarText.A;
        int secondaryA = SkinManager.ActionBarTextSecondary.A;

        if (tabIndex == _baseTabControl.SelectedIndex && !_animationManager.IsAnimating())
        {
            return primaryA;
        }
        if (tabIndex != _previousSelectedTabIndex && tabIndex != _baseTabControl.SelectedIndex)
        {
            return secondaryA;
        }
        if (tabIndex == _previousSelectedTabIndex)
        {
            return primaryA - (int)((primaryA - secondaryA) * animationProgress);
        }
        return secondaryA + (int)((primaryA - secondaryA) * animationProgress);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);

        if (_tabRects == null) UpdateTabRects();
        for (var i = 0; i < _tabRects.Count; i++)
        {
            if (_tabRects[i].Contains(e.Location))
            {
                _baseTabControl.SelectedIndex = i;
            }
        }

        _animationSource = e.Location;
    }

    private void UpdateTabRects()
    {
        _tabRects = new List<Rectangle>();

        //If there isn't a base tab control, the rects shouldn't be calculated
        //If there aren't tab pages in the base tab control, the list should just be empty which has been set already; exit the void
        if (_baseTabControl == null || _baseTabControl.TabCount == 0) return;

        //Calculate the bounds of each tab header specified in the base tab control
        using (var b = new Bitmap(1, 1))
        {
            using (var g = Graphics.FromImage(b))
            {
                _tabRects.Add(new Rectangle(SkinManager.FormPadding, 0, TabHeaderPadding * 2 + (int)g.MeasureString(_baseTabControl.TabPages[0].Text, SkinManager.Font_Size11).Width, Height));
                for (int i = 1; i < _baseTabControl.TabPages.Count; i++)
                {
                    _tabRects.Add(new Rectangle(_tabRects[i - 1].Right, 0, TabHeaderPadding * 2 + (int)g.MeasureString(_baseTabControl.TabPages[i].Text , SkinManager.Font_Size11).Width , Height));
                }
            }
        }
    }
}

} }

If you're using windows forms you would go into the properties of the tab control and make: 如果您使用的是Windows窗体,则可以进入选项卡控件的属性并进行:

RightToLeft = Yes and RightToLeftLayout = True. RightToLeft = Yes和RightToLeftLayout = True。

This is also a duplicate question: How to make Managed Tab Control (MTC) appear right to left 这也是一个重复的问题: 如何使托管选项卡控件(MTC)从右到左显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM