c# - tabcontrol OwnerDrawFixed 在 C# 中从右到左

标签 c# tabcontrol

<分区>

我想为每个选项卡添加 X 按钮。 drawMode 是 OwnerDrawFixed。如果从左到右,它工作正常。 一旦我允许 RightToLeftLayout = true 和 RightToLeft = true,它看起来不太好,因为他仍然从左到右添加字符串,而该选项卡从右到左添加。

如何使字符串也从右到左?

enter image description here

enter image description here

private void addCloseButton(object sender, DrawItemEventArgs e)
{
    //This code will render a "x" mark at the end of the Tab caption. 
    e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15 , e.Bounds.Top +4 );
    e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left+4, e.Bounds.Top+4);
    e.DrawFocusRectangle();

}

private void actionClose(object sender, MouseEventArgs e)
{
    //Looping through the controls.
    for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
    {
        Rectangle r = tabControl1.GetTabRect(i);
        //Getting the position of the "x" mark.
        Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 12, 10);
        if (closeButton.Contains(e.Location))
        {
            if (MessageBox.Show("?האם אתה רוצה לסגור טאב זה", "אישור", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.tabControl1.TabPages.RemoveAt(i);
                break;
            }
        }
    }

}

最佳答案

StringFormatFormatFlags 标志设置为 StringFormatFlags.DirectionRightToLeft 传递给 DrawString():

StringFormat drawFormat = new StringFormat(StringFormatFlags.DirectionRightToLeft);

var bounds = new RectangleF(.. set actual bound rectangle for text... )    

e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, bounds, drawFormat);

关于c# - tabcontrol OwnerDrawFixed 在 C# 中从右到左,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753701/

相关文章:

wpf - 将 CommandTarget 设置为 TabControl 中的选定控件

c# - S.O.L.I.D 原理和编译?

c# - 更改部分工具栏的背景

wpf - 在Catel MVVM WPF中使用TabControl时的性能

c# - WPF 选项卡控件 : How do I get the currently selected tab?

WinForms Tab控件问题

c# - 如何更改 WinForms 上下文菜单上复选标记或 'more' 箭头的颜色?

c# - 使用 URL 在 Web Api 2 中进行 Controller 版本控制

c# - 在应用设置中同步应用版本和内部版本号

c# - 运行时新建 TabItem