2015-12-29 7 views
8

मैं TabControl के TabPages पर एक करीबी बटन जोड़ना चाहता हूं। मैं इस कोड की कोशिश है और यह एक दाएं से बाएं TabControl के साथ ठीक काम करता है:दाएं से बाएं टैबकंट्रोल के टैबपेज के लिए बंद बटन सी #

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

      } 
     } 
    } 

लेकिन जब मैं संपत्ति RightToLeftLayout = true और RightToLeft = true यह काम नहीं करता निर्धारित करते हैं, TabPage खिताब बंद करें बटन दिखाई देते हैं और यह भी नहीं है।

तो RightToLeft संपत्ति स्वीकार करने वाले तरीके को कैसे ठीक किया जाए?

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

और जब RTL मोड में पेंटिंग, निर्देशांक इस तरह की गणना:

tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect); 
इसके अलावा

आप

+0

एक TabControl पर ड्राइंग करके, यह आप 'खुद के द्वारा RightToLeft' संपत्ति की प्रक्रिया पूरी की, संपादन' TabControl2_DrawItem' – J3soon

+0

में अपने कोड Wher संपादित करने की कोशिश का मतलब है? मैंने r.offset को बदलने की कोशिश की लेकिन यह 'टैबकंट्रोल 2_DrawItem' ब्लॉक में पहले टैबपेज – user4340666

+0

के लिए काम करता है,' if (RightToLeft) 'स्थिति – J3soon

उत्तर

4

आप RTL एक कंटेनर में निर्देशांक एक आयत के निर्देशांक अनुवाद करने के लिए एक समारोह बना सकते हैं StringFormat का उपयोग करके अपने तारों को खींचना चाहिए और इसे StringFormatFlags.DirectionRightToLeft का उपयोग करने के लिए सेट करें जब आप आरटीएल मोड में हों और स्ट्रिंग प्रारूप का उपयोग करके अनुवादित आयत में स्ट्रिंग खींचें:

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

आप में TabControl विरासत में सभी कोड encapsulate कर सकते हैं।

स्क्रीनशॉट

enter image description here enter image description here

पूरे कोड हो सकता है:

मैं तुम्हें कहीं Properties.Default.Close की तरह एक करीबी छवि है और this.CloseImage के लिए असाइन करें लगता है। यहां दी गई छवि है: enter image description here

मैंने छवि को चित्रित करने के लिए अतिरिक्त खाली स्थान प्रदान करने के लिए this.tabControl2.Padding = new Point(10, 3); भी सेट किया है।

इसके अलावा आप बस पहले टैब को बंद न करने के अपने मानदंड जोड़ सकते हैं।

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

धन्यवाद बहुत बहुत अच्छा काम करता है – user4340666

+0

आपका स्वागत है :) –

+0

@Dotnet यहां [छवि] है (https://i.stack.imgur.com/8kuxe.png)। आप 'this.tabControl2.Padding = new Point (10, 3); 'के साथ खेल सकते हैं। –

संबंधित मुद्दे