Results 1 to 6 of 6

Thread: [RESOLVED] Tab pages backcolor & flicker

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    517

    Resolved [RESOLVED] Tab pages backcolor & flicker

    Hi Guys,

    I have an program which uses an MDI Form. My mdi form's background is painted gradient through a code I use.

    Now I need to use tab pages in my Mdi Form. But I want to see the background of my MDI (which is coloured). I tried but failed so I decided to colour the background of tab pages to that mdi. I am able to paint it gradient so that part is okay. But there are two problems :

    - A lot of flickering takes place in my tab pages color whenever I resize my mdi form or perform some activity (like insert child form or so).
    - the area around tab headers is not coloured and shows dark grey. How to fix that ?

    My Mdi form when painted gradient works okay and has minimum flicker. But now I have put a tab control docked to fill. Though I am able to pain area of page to gradient but there is a lot of flickering.

    If anyone can advice how to solve the problem, I will be thankful.




    Code:
     Private Sub TabPage1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPage1.Paint
            Dim g As Graphics = TabPage1.CreateGraphics
            Dim rect As New Rectangle(0, 0, TabPage1.Width, TabPage1.Height)
            Dim b As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.FromArgb(0, 58, 140), Color.FromArgb(0, 215, 255), Drawing2D.LinearGradientMode.Vertical) ' Or Drawing2D.LinearGradientMode.ForwardDiagonal)
            g.FillRectangle(b, rect)
            ' g.Dispose()
        End Sub
    Thanks,

    Cheers,
    GR

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Tab pages backcolor & flicker

    Hi,

    This code is taken from an old project and should allow you to colour the area next to the TabHeaders:
    (nb: Try the code in the Paint event. If you need to use the DrawItem Event you might need to set the TabControls DrawMode property to OwnerDrawFixed
    If thats the case you will need to manually paint the TabHeaders and their text.)
    VB Code:
    1. 'Colour in the area surrounding the Tab Headers (if you are using MultiLine Tabs, you will need to tweak this)
    2.  
    3.     Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
    4.  
    5.         Dim Rect1 As Rectangle = TabControl1.Bounds 'Get the TabControl's rectangle
    6.         Dim Rect2 As New Rectangle
    7.  
    8.         'Record the Left(X), Top(Y) and Height of the first Tab Header
    9.         Dim X As Integer = TabControl1.GetTabRect(0).X
    10.         Dim Y As Integer = TabControl1.GetTabRect(0).Y
    11.         Dim Wid As Integer = 0
    12.         Dim Height As Integer = TabControl1.GetTabRect(0).Height
    13.  
    14.         'Record the Width of all the Tab Headers
    15.         For Each tp As TabPage In TabControl1.TabPages
    16.             Wid += TabControl1.GetTabRect(TabControl1.TabPages.IndexOf(tp)).Width
    17.         Next
    18.  
    19.         'Make both rectangles the same height (adding 2 will prevent a drawing issue where a line is drawn below the Tab Headers)
    20.         Rect1.Height = Height + 2
    21.  
    22.  
    23.         'Draw a rectangle for the area occupied by the TabHeaders
    24.         Rect2.X = X
    25.         Rect2.Y = Y
    26.         Rect2.Width = Wid
    27.         Rect2.Height = Height
    28.  
    29.         'Add rectangle1 into a region
    30.         Dim reg As New Region(Rect1)
    31.  
    32.         'Region.Xor cuts 1 rectangle out of another (in this case we are cuttting the rectangle occupied by the Tab Headers, out of the rectangle that
    33.         'is the same height as the tab headers and is the width of the TabControl). This will give us a region that surrounds the Tab Headers.
    34.         reg.Xor(Rect2)
    35.  
    36.         'Now colour in the region using any colour you like
    37.         Dim brush As New SolidBrush(Color.FromArgb(0, 58, 140))
    38.         e.Graphics.FillRegion(brush, reg)
    39.  
    40.     End Sub
    Let me know if you have any probs with this as the VS2010 TabControl's leave this area transparent by default, so I can't test the code.

    And moving onto the Flicker problem, add the following code to your project:
    VB Code:
    1. Protected Overrides ReadOnly Property CreateParams() As CreateParams
    2.         Get
    3.             Dim cp As CreateParams = MyBase.CreateParams
    4.             cp.ExStyle = cp.ExStyle Or &H2000000
    5.             Return cp
    6.         End Get
    7.     End Property 'CreateParams
    The code overrides the forms window style. In this case it is telling the form to use the WS_CLIPCONTROLS constant (&H2000000); so now everytime the form redraws, it will not redraw the areas of the form occupied by child controls.

    If for any reason you need to force a redraw on any of the controls on your form, you can do so by using:
    VB Code:
    1. Control.Refresh()
    Last edited by JayJayson; Apr 5th, 2011 at 01:04 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    517

    Post Re: Tab pages backcolor & flicker

    Quote Originally Posted by jay20aiii View Post
    Hi,

    This code is taken from an old project and should allow you to colour the area next to the TabHeaders:
    (nb: Try the code in the Paint event. If you need to use the DrawItem Event you might need to set the TabControls DrawMode property to OwnerDrawFixed
    If thats the case you will need to manually paint the TabHeaders and their text.)
    VB Code:
    1. 'Colour in the area surrounding the Tab Headers (if you are using MultiLine Tabs, you will need to tweak this)
    2.  
    3.     Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
    4.  
    5.         Dim Rect1 As Rectangle = TabControl1.Bounds 'Get the TabControl's rectangle
    6.         Dim Rect2 As New Rectangle
    7.  
    8.         'Record the Left(X), Top(Y) and Height of the first Tab Header
    9.         Dim X As Integer = TabControl1.GetTabRect(0).X
    10.         Dim Y As Integer = TabControl1.GetTabRect(0).Y
    11.         Dim Wid As Integer = 0
    12.         Dim Height As Integer = TabControl1.GetTabRect(0).Height
    13.  
    14.         'Record the Width of all the Tab Headers
    15.         For Each tp As TabPage In TabControl1.TabPages
    16.             Wid += TabControl1.GetTabRect(TabControl1.TabPages.IndexOf(tp)).Width
    17.         Next
    18.  
    19.         'Make both rectangles the same height (adding 2 will prevent a drawing issue where a line is drawn below the Tab Headers)
    20.         Rect1.Height = Height + 2
    21.  
    22.  
    23.         'Draw a rectangle for the area occupied by the TabHeaders
    24.         Rect2.X = X
    25.         Rect2.Y = Y
    26.         Rect2.Width = Wid
    27.         Rect2.Height = Height
    28.  
    29.         'Add rectangle1 into a region
    30.         Dim reg As New Region(Rect1)
    31.  
    32.         'Region.Xor cuts 1 rectangle out of another (in this case we are cuttting the rectangle occupied by the Tab Headers, out of the rectangle that
    33.         'is the same height as the tab headers and is the width of the TabControl). This will give us a region that surrounds the Tab Headers.
    34.         reg.Xor(Rect2)
    35.  
    36.         'Now colour in the region using any colour you like
    37.         Dim brush As New SolidBrush(Color.FromArgb(0, 58, 140))
    38.         e.Graphics.FillRegion(brush, reg)
    39.  
    40.     End Sub
    Let me know if you have any probs with this as the VS2010 TabControl's leave this area transparent by default, so I can't test the code.

    And moving onto the Flicker problem, add the following code to your project:
    VB Code:
    1. Protected Overrides ReadOnly Property CreateParams() As CreateParams
    2.         Get
    3.             Dim cp As CreateParams = MyBase.CreateParams
    4.             cp.ExStyle = cp.ExStyle Or &H2000000
    5.             Return cp
    6.         End Get
    7.     End Property 'CreateParams
    The code overrides the forms window style. In this case it is telling the form to use the WS_CLIPCONTROLS constant (&H2000000); so now everytime the form redraws, it will not redraw the areas of the form occupied by child controls.

    If for any reason you need to force a redraw on any of the controls on your form, you can do so by using:
    VB Code:
    1. Control.Refresh()
    Thanks for your help buddy but the problems are still persisting.

    - tab headers are coloured but the area next to them isn't. Actually I wanted the area next to tab headers to be colored and not the headers themself.

    - the flicker problem is just like before.

    - tab control does not have a paint event. I had to paste it in drawitem as the function said.

    So we are still stuck there I guess.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Tab pages backcolor & flicker

    okies, you've pretty much got 3 choices with the TabPage Background.

    1. Take a look at http://dotnetrix.co.uk/tabcontrol.htm. Specifically the OwnerDraw section. Lot's of work involved but it will get the result your looking for.

    2. Create/download a Custom TabControl

    3. Dock a Panel control onto the TabPage and use that for the background. See code below:

    VB Code:
    1. 'CreateParams effect will work on the Panel.
    2.     Protected Overrides ReadOnly Property CreateParams() As CreateParams
    3.         Get
    4.             Dim cp As CreateParams = MyBase.CreateParams
    5.             cp.ExStyle = cp.ExStyle Or &H2000000
    6.             Return cp
    7.         End Get
    8.     End Property 'CreateParams
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         Panel1_Background()
    12.     End Sub
    13.  
    14.     'Create Panel and set colour / properties
    15.     Private Panel1 As New Panel
    16.     Private Sub Panel1_Background()
    17.         Me.Controls.Add(Panel1)
    18.         Panel1.Dock = DockStyle.Fill
    19.  
    20.         Panel1.Parent = TabControl1.TabPages(0)
    21.  
    22.         Dim pBackground As New Bitmap(Panel1.Width, Panel1.Height)
    23.         Dim g As Graphics = Graphics.FromImage(pBackground)
    24.  
    25.         Dim rect As New Rectangle(0, 0, pBackground.Width, pBackground.Height)
    26.         Dim b As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.FromArgb(0, 58, 140), Color.FromArgb(0, 215, 255), Drawing2D.LinearGradientMode.Vertical)
    27.         g.FillRectangle(b, rect)
    28.         g.Dispose()
    29.  
    30.         Panel1.BackgroundImage = pBackground
    31.         Panel1.BackgroundImageLayout = ImageLayout.Stretch
    32.     End Sub
    33.  
    34.     'Move panel onto next TabPage
    35.     Private Sub TabControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseDown
    36.  
    37.         For i As Integer = 0 To TabControl1.TabCount - 1
    38.             If TabControl1.GetTabRect(i).Contains(e.Location) Then
    39.                 Panel1.Parent = TabControl1.TabPages(i)
    40.                 Exit For
    41.             End If
    42.         Next i
    43.  
    44.     End Sub

    I'll try and look at the code for colouring the TabHeaders area when I get chance.

  5. #5
    Addicted Member
    Join Date
    Mar 2007
    Posts
    163

    Re: Tab pages backcolor & flicker

    To get rid of the grey patch that you indicate in your picture try enabling visual styles.

    Call this from your constructor
    vb Code:
    1. Application.EnableVisualStyles()

    In order to paint the actual tabs you'll have to try what jay sugested.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    517

    Re: Tab pages backcolor & flicker

    Thanks a lot Jay, I appreciate your help. The link you suggested should do the job.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width