code_junkie

Double buffering problem of a derived owner-drawn TabControl?

Nov 11th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. class DoubleBufferedTabControl : TabControl
  2. {
  3. public DoubleBufferedTabControl() : base()
  4. {
  5. this.DoubleBuffered = true;
  6. this.SetStyle
  7. (
  8. ControlStyles.UserPaint |
  9. ControlStyles.AllPaintingInWmPaint |
  10. ControlStyles.ResizeRedraw |
  11. ControlStyles.OptimizedDoubleBuffer |
  12. ControlStyles.SupportsTransparentBackColor,
  13. false
  14. );
  15. }
  16. }
  17.  
  18. private void Navigation_PageContent_DrawItem(object sender, DrawItemEventArgs e)
  19. {
  20. //Structure.
  21. Graphics g = e.Graphics;
  22. TabControl t = (TabControl)sender;
  23. TabPage CurrentPage = t.TabPages[e.Index];
  24.  
  25. //Get the current tab
  26. Rectangle CurrentTabRect = t.GetTabRect(e.Index);
  27.  
  28. //Get the last tab.
  29. Rectangle LastTab = t.GetTabRect(t.TabPages.Count - 1);
  30.  
  31. //Main background rectangle.
  32. Rectangle BackgroundRect = new Rectangle(LastTab.Width, t.Bounds.Y - 4, t.Width - (LastTab.Width), t.Height);
  33.  
  34. //Tab background rectangle.
  35. Rectangle TabBackgroundRect = new Rectangle(0, LastTab.Y + LastTab.Height, LastTab.Width, t.Bounds.Height - (LastTab.Y + LastTab.Height));
  36.  
  37. //Set anitialiasing for the text.
  38. e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  39.  
  40. //String format for the text.
  41. StringFormat StringFormat = new StringFormat();
  42. StringFormat.Alignment = StringAlignment.Center;
  43. StringFormat.LineAlignment = StringAlignment.Center;
  44.  
  45. //Fill the background.
  46. g.FillRectangle(Brushes.LightGray, BackgroundRect);
  47. g.FillRectangle(Brushes.Bisque, TabBackgroundRect);
  48.  
  49. //Draw the selected tab.
  50. if(e.State == DrawItemState.Selected)
  51. {
  52. g.FillRectangle(Brushes.White, e.Bounds);
  53. Rectangle SelectedTabOutline = new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 4);
  54. g.DrawRectangle(new Pen(Brushes.LightGray, 4f), SelectedTabOutline);
  55. g.DrawString(CurrentPage.Text, new Font("Arial", 12f, FontStyle.Bold, GraphicsUnit.Point), new SolidBrush(Color.FromArgb(70, 70, 70)), CurrentTabRect, StringFormat);
  56. }
  57. else
  58. {
  59. g.FillRectangle(new SolidBrush(Color.FromArgb(230, 230, 230)), e.Bounds);
  60. g.DrawString(CurrentPage.Text, new Font("Arial", 12f, FontStyle.Regular, GraphicsUnit.Point), Brushes.Gray, CurrentTabRect, StringFormat);
  61. }
  62.  
  63. }
  64.  
  65. protected override CreateParams CreateParams
  66. {
  67. get
  68. {
  69. CreateParams cp = base.CreateParams;
  70. cp.ExStyle |= 0x02000000;
  71. return cp;
  72. }
  73. }
  74.  
  75. protected override void OnPaintBackground( PaintEventArgs pevent )
  76. {
  77. //do not call base - I don't want the background re-painted!
  78. }
Add Comment
Please, Sign In to add comment