This question already has answers here:
Close button for TabPages of Right To Left TabControl c#
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
我想为每个标签添加X按钮。
drawMode为OwnerDrawFixed。如果从左到右,它工作正常。
一旦我允许RightToLeftLayout = true和RightToLeft = true,它看起来就不好了,因为他仍然从左到右添加字符串,而该选项卡从右到左添加。

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





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;
            }
        }
    }

}

最佳答案

StringFormat标志设置为FormatFlagsStringFormatFlags.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 OwnerDraw在C#中从右到左固定,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21753701/

10-11 10:39