在C#中实现TabControl控件的OwnerDrawFixed从右到左的显示方式

3
我想为每个选项卡添加一个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;
            }
        }
    }

}

4
请查看此答案:RightToLeft属性下TabPage的关闭按钮 - user4340666
2个回答

0
StringFormat与设置了FormatFlags标志为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);

我不知道该把什么放在var bounds = new RectangleF()中。 如果我放置点(0,0,845,592),那么845,592是TabControl的大小,所以一开始看起来很好,之后就被覆盖了。 - user3305653
首先尝试通过 e.Bounds(选项卡标题的边界) ,然后调整它以避免与 [X] 文本重叠。 - PashaPash
我无法在e.Bounds标题后添加内容,我只有Top、Right、Left等。我有4个字段需要添加到新的RectangleF()中,但我卡住了。如果我输入:var bounds = new RectangleF(0, 0, e.Bounds.Top, e.Bounds.Right+30); 它不会显示字符串。 - user3305653
尝试使用RectangleF(e.Bounds.Left, e.Bounds.Top, e.Bound.Width, e.Bounds.Height)。 - PashaPash
它不能正常工作。它从左到右开始,并且任何额外的制表符都会从左到右添加,就像之前一样。 - user3305653
显示剩余2条评论

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接