Repaint the title of the TabControl tab in WinForm, winformtabcontrol

Source: Internet
Author: User

Repaint the title of the TabControl tab in WinForm, winformtabcontrol

Recently developed WinForm frequently uses the TabControl control. The tabs of this control do not haveBackgroundImageIn this case, how do I add a background image for each tab? (This is the header of each TabPage, that is, the title, not the work area .)

When I first used TabControl, each of my tabs was written to death. Then, due to project requirements, TabControl was dynamically added and various tabs were generated. I had to re-paint the title twice, so here I will share the method of re-painting in the two cases through an example.

First, drag a Tabcontrol Control in the form, and then change itsAlignmentThe property isLeftTo the left. You can changeItemSizeAdjust the size of each tab. It is worth noting that because TabControl is rotated 90 ° to the left, the height of TabControl is actually changed when the width is set, when it is set to high, it actually changes its width. The setting width (Change height) is invalid because it is affected by the tab text font and the font size can be changed accordingly. After some adjustments:

Next we will go to the topic to redraw its tab background. FirstDrawModeAttribute changedOwnerDrawFixed. AddDrawItemEvent. The event repainting method is as follows:

1 private void tabMain_DrawItem (object sender, DrawItemEventArgs e) 2 {3 Bitmap b0 = new Bitmap (@".. \.. \ Images \ 1.jpg"); 4 Bitmap b1 = new Bitmap (@".. \.. \ Images \ 2.jpg"); 5 Bitmap b2 = new Bitmap (@".. \.. \ Images \ 3.jpg"); 6 Bitmap b3 = new Bitmap (@".. \.. \ Images \ 4.jpg"); 7 switch (e. index) 8 {9 case 0: 10 e. graphics. drawImage (b0, e. bounds); 11 break; 12 case :13 e. graphics. drawImage (b1, e. bounds); 14 break; 15 case 2: 16 e. graphics. drawImage (b2, e. bounds); 17 break; 18 case :19 e. graphics. drawImage (b3, e. bounds); 20 break; 21} 22}View Code

I put all the Images in this article in an Images folder under the current project.

Finally:

The preceding example shows how to redraw a title when a tab is generated dynamically. Before you describe this, add the original TabControlSelectedIndexChangedEvent, obtain itsSelectedIndexTo know which tab is clicked, here, I dynamically add another TabControl In the second tab and generate each of its tabpages. The Code is as follows:

1 private void tabMain_SelectedIndexChanged (object sender, EventArgs e) 2 {3 switch (tabMain. selectedIndex) 4 {5 case 0: 6 break; 7 case 1: 8 // assume that the dynamically generated TabControl has four tabs 9 for (int I = 0; I <4; I ++) 10 {11 TabPage page = new TabPage (); 12 page. name = "page" + I. toString (); 13 page. text = (I + 1 ). toString (); 14 this. tabSub. controls. add (page); 15} 16 this. tabSub. itemSize = new System. drawing. size (80, 80); 17 this. tabSub. font = new System. drawing. font ("", 20); 18 this. tabSub. dock = DockStyle. fill; 19 this. tabMain. tabPages [1]. controls. add (tabSub); 20 break; 21 case 2: 22 break; 23 case 3: 24 break; 25} 26}View Code

Pre-define the TabControl variable to be generated externally:

TabControl tabSub = new TabControl();

The effect is as follows:

Now, we should re-draw the title for it. First, add these two lines in the code that sets the tabSub attribute above:

this.tabSub.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabSub_DrawItem);this.tabSub.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;

The first line is an event for Adding titles to tabSub, and the second line is to allow the system to use our own style.

Then write a drawing method by yourself. The Code is as follows:

1 private void tabSub_DrawItem (object sender, DrawItemEventArgs e) 2 {3 List <string> imgPath = new List <string> () 4 {5 @".. \.. \ Images \ d.jpg ", 6 @".. \.. \ Images \ s.jpg ", 7 @".. \.. \ Images \ n.jpg ", 8 @".. \.. \ Images \ x.jpg "9}; 10 for (int I = 0; I <this. tabSub. tabCount; I ++) 11 {12 Bitmap B = new Bitmap (imgPath [I]); 13 if (e. index = I) 14 {15 e. graphics. drawImage (B, e. bounds); 16} 17} 18}View Code

The effect is as follows:

OK. Now we are done. Of course, the above Code is flawed. When you rotate the last tab on the left to the second tab, it will load more than once, you only need to add a judgment when you draw the chart. You can determine whether the controls are included in the current tab, you can also define a Boolean variable to determine whether it has been loaded.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.