2014-02-13 18 views
3

我想爲每個選項卡添加X按鈕。 drawMode是OwnerDrawFixed。如果從左到右,它工作正常。 一旦我允許RightToLeftLayout = true和RightToLeft = true,它看起來不太好,因爲他仍然從左到右添加了字符串,而該標籤從右到左添加。tabcontrol OwnerDrawFixed從右到左在c#

如何讓字符串也是從右到左?

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

看看這個答案[關閉按鈕到TabPage與RightToLeft屬性](http://stackoverflow.com/a/34509304/4340666) – user4340666

回答

0

StringFormatFormatFlags標誌設置爲StringFormatFlags.DirectionRightToLeftDrawString()

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); 
+0

我不知道該怎麼把var bounds = new RectangleF()。 如果我把這個點(0,0,845,592)表示845592是tabcontrol的大小,那麼它首先會很好地發揮出來,然後它就會踐踏它。 – user3305653

+0

嘗試先通過e.Bounds(標籤頁眉的邊界),然後調整它以避免與[X] – PashaPash

+0

的文本重疊,我無法在e.Bounds標題後添加,我有Top或Right或Left等。我有4個字段需要添加到新的RectangleF(),並且我參與其中。如果我把:var bounds = new RectangleF(0,0,e.Bounds.Top,e.Bounds.Right + 30);它不會顯示我的字符串。 – user3305653