Software products with distinction RSS 2.0
 Monday, May 18, 2009
Since Windows default Tab Control is quite primitive, every once in a while you'll need the ability to be able to close the individual tab pages in the control.

SnapConfig has this ability and I thought I would share some aspects of how this is done.

One of the problematic issues in drawing buttons yourself is to take care of themes in windows but still be able to support non themed desktops.

following is the code that does that

   
public void DrawTabCloseButton(System.Windows.Forms.DrawItemEventArgs e, Rectangle rect, VisualStyleElement style, ButtonState state)
    {
      if (VisualStyleRenderer.IsSupported)
      {        
        if (VisualStyleRenderer.IsElementDefined(style))
        {
          VisualStyleRenderer renderer = new VisualStyleRenderer(style);
          renderer.DrawBackground(e.Graphics, rect);
          return;
        }
      }
      ControlPaint.DrawCaptionButton(e.Graphics, rect, CaptionButton.Close, state);
    }



The rect parameter is where you want to draw the button,

The VisualStyleElement can be VisualStyleElement.Window.CloseButton.Hot or VisualStyleElement.Window.CloseButton.Pressed or any of the others supported if you are handling that logic.

ButtonState can be ButtonState.Pushed or any other.

This section only handles the actual drawing, there are other things which are done on the fly by SnapConfig that is maintaing the state of button on each tab, for example if you press and hold the mouse and move away from a close button it will become normal but if you come back while holding it will again press down, which is standard windows behaviour.

to get onDrawItem you need to set

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


Monday, May 18, 2009 4:37:22 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] -
Code
Navigation
Categories
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Sajid
Sign In
Statistics
Total Posts: 4
This Year: 0
This Month: 0
This Week: 0
Comments: 0
Themes
All Content © 2010, Sajid
DasBlog theme 'Business' created by Christoph De Baene (delarou)