Magic Logo
top
constructors
images
border width
popup
properties
sample code
Class InertButton
Surely the last thing we need is another button class when the framework already provides support via the existing Button class.

Various components in the Magic Library need a button with the popup style of operation that can be pressed to generate Click events but without the button becoming selected. Why would you want a button that can be operated but does not become selected?

Imagine you have a RichTextBox control placed onto a Form along with a Button. The user types in some text and then presses the button in order to save the work completed so far. Once the button has been released the focus has now moved from the RichTextBox to the Button. But I want the button press to leave the focus and the cursor in the text box. The user would quickly get annoyed with reselecting the text control everytime they want to save their work.

This class overcomes these limitations.

Constructors
		
   public InertButton()
   public InertButton(ImageList imageList, int imageIndexEnabled)
        
   public InertButton(ImageList imageList, int imageIndexEnabled, 
                      int imageIndexDisabled)
        
   public InertButton(ImageList imageList, int imageIndexEnabled, 
                      int imageIndexDisabled, ImageAttributes imageAttr)

Images
When the button is enabled it will show the defined image as it exists in the provided InertButton.ImageList property. When the button is disabled it will draw the same image but with the colors washed out. If you prefer to define disabledimage manually then you can either use the appropriate constructor or set the InertButton.ImageIndexDisabled property.

If you want to be really adventurous then you can provide an instance of the ImageAttributes class for use when drawing the images at runtime. This can be provided via either the appropriate constructor or the InertButton.ImageAttributes property.In this way you can modify the color rendering.

For example, the following code will draw different images when enabled and disabled. It will also remap any black pixels to blue and white pixels to become red.


   using Crownwood.Magic.Controls;

   // Create color-remapping objects
   ColorMap remap1 = new ColorMap();
   ColorMap remap2 = new ColorMap();

   // Change any black pixels to blue	
   remap1.OldColor = Color.Black;
   remap1.NewColor = Color.Blue;

   // Change any white pixels to red
   remap2.OldColor = Color.White;
   remap2.NewColor = Color.Red;

   // Create the image attributes object
   ImageAttributes myAttribs = new ImageAttributes();

   // Define the image remappings
   myAttribs.SetRemapTable(new ColorMap[]{remap1, remap2});		

   // Use the overloaded constructor that takes two 
   // different images for enabled/disabled and also
   // applies the image attributes to drawing them both
   InertButton myButton = new InsertButton(imageList, 0, 1, myAttribs);

   // Define the size and position
   myButton.Size = new Size(32,32);
   myButton.Location = new Point(10,10);

   // We want notification when button is pressed
   myButton.Click += new EventHandler(OnButtonClick);

   Controls.Add(myButton);

Setting the border width
By default the border that is drawn around the button is 2 pixels wide when the mouse moves over the control. This is fine for normal buttons on a Form but not for many buttons that appear in the Magic Library. Examine the TabControl and you will notice that the close and left/right arrow buttons are drawn with a single pixel width border. The property InertButton.BorderWidth can be used to change the defaut border width.
Popup buttons
By default the InertButton.PopupStyle property is defined as true and so the button will appear flat and borderless until the mouse is over the control. If you need the traditional look where the button always has a border then set this property to false.
Properties
public int ImageIndexEnabled
Index for drawing an image when the button is enabled.
Default: -1

public int ImageIndexDisabled
Index for drawing an image when the button is disabled.
Default: -1

public ImageList ImageList
Images for use when drawing the button.
Default: null

public ImageAttributes ImageAttributes
Object used to modify image drawing at runtime.
Default: null

public int BorderWidth
Pixel width of the border around the button.
Default: 2

public bool PopupStyle
Border appears only when mouse moves over button.
Default: true

Sample code
	
   using Crownwood.Magic.Controls;

   // Use overloaded constructor to define image
   InertButton myButton = new InsertButton(imageList, 0);

   // Define the size and position
   myButton.Size = new Size(32,32);
   myButton.Location = new Point(10,10);

   // We want notification when button is pressed
   myButton.Click += new EventHandler(OnButtonClick);

   Controls.Add(myButton);