2015-12-29 6 views
8

Desidero aggiungere un pulsante di chiusura a TabPages di un TabControl. Provo questo codice e funziona benissimo con una sinistra a destra TabControl:Pulsante di chiusura per TabPages di TabControl da destra a sinistra C#

private Point _imageLocation = new Point(13, 5); 
private Point _imgHitArea = new Point(13, 2); 

this.tabControl2.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed; 

tabControl2.DrawItem += TabControl2_DrawItem; 

private void TabControl2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
{ 
    try 
    { 
     Image img = new Bitmap(GestionP.Properties.Resources.Close); 
     Rectangle r = e.Bounds; 
     r = this.tabControl2.GetTabRect(e.Index); 
     r.Offset(2, 2); 
     Brush TitleBrush = new SolidBrush(Color.Black); 
     Font f = this.Font; 
     string title = this.tabControl2.TabPages[e.Index].Text; 
     e.Graphics.DrawString(title, f, TitleBrush, new PointF(r.X, r.Y)); 

     if (tabControl2.SelectedIndex >= 1) 
     { 
      e.Graphics.DrawImage(img, new Point(r.X + (this.tabControl2.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y)); 
     } 

    } 
     catch (Exception) { } 
} 

    private void tabControl2_MouseClick(object sender, MouseEventArgs e) 
    { 
     TabControl tc = (TabControl)sender; 
     Point p = e.Location; 
     int _tabWidth = 0; 
     _tabWidth = this.tabControl2.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X); 
     Rectangle r = this.tabControl2.GetTabRect(tc.SelectedIndex); 
     r.Offset(_tabWidth, _imgHitArea.Y); 
     r.Width = 16; 
     r.Height = 16; 
     if (tabControl2.SelectedIndex >= 1) 
     { 
      if (r.Contains(p)) 
      { 
       TabPage TabP = (TabPage)tc.TabPages[tc.SelectedIndex]; 
       tc.TabPages.Remove(TabP); 

      } 
     } 
    } 

Ma quando ho impostato la proprietà RightToLeftLayout = true e RightToLeft = true non funziona, TabPage titoli non appaiono e anche il pulsante di chiusura.

Quindi, come risolvere il codice in modo che accetti la proprietà RightToLeft?

+0

da disegno su una TabControl, che significa che è necessario elaborare 'proprietà RightToLeft' da soli, provare a modificare il codice in' TabControl2_DrawItem' – J3soon

+0

Wher modificare? ho provato a cambiare r.offset ma questo funziona solo per la prima tabpage – user4340666

+0

nel blocco 'TabControl2_DrawItem', aggiungere una condizione' if (RightToLeft) ' – J3soon

risposta

4

È possibile creare una funzione di tradurre le coordinate di un rettangolo di coordinate RTL in un contenitore:

public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle) 
{ 
    return new Rectangle(
     container.Width - drawRectangle.Width - drawRectangle.X, 
     drawRectangle.Y, 
     drawRectangle.Width, 
     drawRectangle.Height); 
} 

E quando la pittura in modalità RTL, calcolare le coordinate in questo modo:

tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect); 

Inoltre è dovrebbe disegnare le stringhe usando un StringFormat e impostarlo per utilizzare StringFormatFlags.DirectionRightToLeft quando si è in modalità RTL e tracciare una stringa nel rettangolo tradotto utilizzando il formato stringa:

e.Graphics.DrawString(this.tabControl2.TabPages[e.Index].Text, 
         this.Font, Brushes.Black, tabRect, sf); 

È possibile incapsulare tutti i codici in un CustomTabControl ereditando TabControl.

Schermata

enter image description here enter image description here

L'intero codice potrebbe essere:

Suppongo di avere un'immagine di stretta da qualche parte come Properties.Default.Close e assegnarlo a this.CloseImage. Ecco l'immagine che ho usato: enter image description here

Ho anche impostato lo this.tabControl2.Padding = new Point(10, 3); per fornire ulteriore spazio libero per disegnare l'immagine.

Inoltre, è possibile semplicemente aggiungere i criteri per non chiudere la prima scheda.

Image CloseImage; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.tabControl2.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed; 
    tabControl2.DrawItem += TabControl2_DrawItem; 
    tabControl2.MouseClick += tabControl2_MouseClick; 
    CloseImage = Properties.Resources.Close; 
    this.tabControl2.Padding = new Point(10, 3); 
} 


private void TabControl2_DrawItem(object sender, 
            System.Windows.Forms.DrawItemEventArgs e) 
{ 
    try 
    { 
     var tabRect = this.tabControl2.GetTabRect(e.Index); 
     tabRect.Inflate(-2, -2); 
     var imageRect = new Rectangle(tabRect.Right - CloseImage.Width, 
           tabRect.Top + (tabRect.Height - CloseImage.Height)/2, 
           CloseImage.Width, 
           CloseImage.Height); 

     var sf = new StringFormat(StringFormat.GenericDefault); 
     if (this.tabControl2.RightToLeft == System.Windows.Forms.RightToLeft.Yes && 
      this.tabControl2.RightToLeftLayout == true) 
     { 
      tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect); 
      imageRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, imageRect); 
      sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft; 
     } 

     e.Graphics.DrawString(this.tabControl2.TabPages[e.Index].Text, 
           this.Font, Brushes.Black, tabRect, sf); 
     e.Graphics.DrawImage(CloseImage, imageRect.Location); 

    } 
    catch (Exception) { } 
} 

private void tabControl2_MouseClick(object sender, MouseEventArgs e) 
{ 

    for (var i = 0; i < this.tabControl2.TabPages.Count; i++) 
    { 
     var tabRect = this.tabControl2.GetTabRect(i); 
     tabRect.Inflate(-2, -2); 
     var imageRect = new Rectangle(tabRect.Right - CloseImage.Width, 
           tabRect.Top + (tabRect.Height - CloseImage.Height)/2, 
           CloseImage.Width, 
           CloseImage.Height); 
     if (imageRect.Contains(e.Location)) 
     { 
      this.tabControl2.TabPages.RemoveAt(i); 
      break; 
     } 
    } 
} 

public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle) 
{ 
    return new Rectangle(
     container.Width - drawRectangle.Width - drawRectangle.X, 
     drawRectangle.Y, 
     drawRectangle.Width, 
     drawRectangle.Height); 
} 
+0

Grazie mille funziona molto bene – user4340666

+0

Siete i benvenuti :) –

+0

@Dotnet Ecco la [immagine] (https://i.stack.imgur.com/8kuxe.png). Puoi giocare con 'this.tabControl2.Padding = new Point (10, 3);'. –

Problemi correlati