Using Windows Forms Appearance Category

Jump to: navigation, search

Windows Forms applications can be customized to look many different ways using the Appearance Category.

Contents


Appearance Category

If you look in the Category view for the form, the following properties are listed under Appearance:

Each of these properties alters the forms appearance in some manner.

An ambient property is a control property that, if not set, is retrieved from the parent control. For more information about ambient properties, see the AmbientProperties class (MSDN)


BackColor property

The Form.BackColor property overrides Control.BackColor. This property allows the background color of the control to be changed.

The default color when you create a new form is System.Drawing.SystemColors.Control. This is the control color defined in the 3D objects in the appearance setup under windows.

You can see this in action by right-clicking on the desktop and selecting properties. Choose the Appearance tab, then click on the Advanced button. This will bring up the Advanced Appearance dialog.


Image:526_06.jpg


Under items select 3D Controls and choose a different color, you will find that the background of your application has changed color.

To demonstrate this further, this sample application (written in C# Express) uses a timer to change the BackColor every 10 milliseconds.

To change the BackColor property at run time, simply set the property to the desired value.

this.BackColor = Color.Blue;


BackgroundImage property

The Form.BackgroundImage property is inherited from Control.BackgroundImage. This property allows a graphic image to be displayed in the background of the form.


Image:526_07.jpg


Consider these two notes about adding images:

1. Images with translucent or transparent colors are not supported by Windows Forms controls as background images.

2. This property is not supported on child controls whose RightToLeftLayout property is true.

To demonstrate this further, this sample application (written in C# Express) shows the image above, but tiled as in the next section.

To change the BackgroundImage property at runtime, use the Image.FromFile method.

this.BackgroundImage = Image.FromFile("shuttle.jpg");


BackgroundImageLayout property

The default value is set to Tile. This property is inherited from Control.BackgroundImageLayout and allows you to specify the position and behavior of an image you have placed onto a control and takes a value from the ImageLayout enumeration:

  • Center - The image is centered within the control's client rectangle.
  • None - The image is left-aligned at the top across the control's client rectangle.
  • Stretch - The image is stretched across the control's client rectangle.
  • Tile - The image is tiled across the control's client rectangle.
  • Zoom - The image is enlarged within the control's client rectangle.


Image:526_08.jpg


To demonstrate this further, this sample application (written in C# Express) shows the tiled image above.

To change the BackgroundImageLayout property at runtime, simply set the property to the desired value.

this.BackgroundImageLayout = ImageLayout.Center;


Cursor property

The default value is set to Cursors.Default. This property is inherited from Control.Cursor. This property allows the cursor that is displayed to be changed when the mouse pointer is over the control. All of the system cursors are held in the Cursors class. By modifying the code, we can draw all of the system cursors.



Here is the code to achieve this:

private Cursor[] CursorList()
{
  // Make an array of all the types of cursors in Windows Forms.
   return new Cursor[] {
    Cursors.AppStarting, Cursors.Arrow,       Cursors.Cross,
    Cursors.Default,     Cursors.Hand,        Cursors.Help,
    Cursors.HSplit,      Cursors.IBeam,       Cursors.No,
    Cursors.NoMove2D,    Cursors.NoMoveHoriz, Cursors.NoMoveVert,
    Cursors.PanEast,     Cursors.PanNE,       Cursors.PanNorth,
    Cursors.PanNW,       Cursors.PanSE,       Cursors.PanSouth,
    Cursors.PanSW,       Cursors.PanWest,     Cursors.SizeAll,
    Cursors.SizeNESW,    Cursors.SizeNS,      Cursors.SizeNWSE,
    Cursors.SizeWE,      Cursors.UpArrow,     Cursors.VSplit,
    Cursors.WaitCursor};
}
 
protected override void OnPaint(PaintEventArgs e)
{
  Graphics g = e.Graphics;
  Rectangle rectangle;
  SolidBrush drawBrush = new SolidBrush(Color.Black);
  
  int x = 0;
  int X = 0;
  int y = 0;
  int i = 0;
 
  foreach (Cursor cursor in CursorList())
  {
    g.DrawString(cursor.ToString(), this.Font, drawBrush, 10 + x, 10 + y);
    
    // Draw the cursor
    rectangle = new Rectangle(
      new Point(220 + x, 10 + y),
      new Size(cursor.Size.Width, cursor.Size.Height));
    cursor.Draw(g, rectangle);
 
    // Dispose of the cursor.
    cursor.Dispose();
    
    if( x == 0 )
      x = 220;
    else
      x = 0;
 
    i++;
 
    if ((i % 2) == 0)
    {
      y += 40;
    }
    else
      x += 50;
  }
  
  // Tidy up brush
  drawBrush.Dispose();
 
  base.OnPaint(e);
}

Here is a downloadable version of the sample application (written in C# Express).

The other sample supplied shows how to use each cursor over a panel control.


Image:526_10.jpg


public Form1()
{
  InitializeComponent();
 
  // Add all the cursor types to the combobox.
  foreach (Cursor cursor in CursorList())
  {
    cursorSelectionComboBox.Items.Add(cursor);
  }
}
 
private Cursor[] CursorList()
{
  // Make an array of all the types of cursors in Windows Forms.
  return new Cursor[] {
    Cursors.AppStarting, Cursors.Arrow,       Cursors.Cross,
    Cursors.Default,     Cursors.Hand,        Cursors.Help,
    Cursors.HSplit,      Cursors.IBeam,       Cursors.No,
    Cursors.NoMove2D,    Cursors.NoMoveHoriz, Cursors.NoMoveVert,
    Cursors.PanEast,     Cursors.PanNE,       Cursors.PanNorth,
    Cursors.PanNW,       Cursors.PanSE,       Cursors.PanSouth,
    Cursors.PanSW,       Cursors.PanWest,     Cursors.SizeAll,
    Cursors.SizeNESW,    Cursors.SizeNS,      Cursors.SizeNWSE,
    Cursors.SizeWE,      Cursors.UpArrow,     Cursors.VSplit,
    Cursors.WaitCursor};
}
 
 
private void cursorSelectionComboBox_SelectedIndexChanged
   (object sender, System.EventArgs e)
   {
   // Set the cursor in the test panel 
   // to be the selected cursor style.
   testPanel.Cursor = (Cursor)
      cursorSelectionComboBox.SelectedItem;
}

Here is a downloadable version of this sample application (written in C# Express).


Font property

The default is the value of the System.Windows.Forms.Control.DefaultFont property. This property is inherited from Control.Font.

This property sets the font that will be used on the form, unless otherwise overridden by a control's Font property. The font must be located on the computer that the application is running on.

In the first Cursor example, the text is drawn using the Font property of the Form class. At runtime we could make that text bold using a new instance of the Font class, as in the following code example.

protected override void OnPaint(PaintEventArgs e)
   {
   // Call base class OnPaint method
   base.OnPaint(e);
 
   // Method under System.Drawing.Graphics
   e.Graphics.DrawString( "Hello Mum!", 
                           new Font(this.Font.FontFamily,
                           24,
                           this.Font.Style | FontStyle.Bold),
                           new SolidBrush(Color.Tomato),
                           40,
                           40);
   }


Image:526_11.jpg


Here is a downloadable version of this sample application (written in C# Express).

You can change the font of any control using a FontDialog. See Set a control font with FontDialog for an example.

If you want to get all of the fonts installed on a PC, then read this example.

To change the Font property at runtime, set the property to a new instance of Font.

this.Font = new Font(new FontFamily("Arial"), 10);


ForeColor property

The default value is set to System.Windows.Forms.Control.DefaultForeColor. This property overrides Control.ForeColor and allows the foreground color of the control to be changed.

To change the ForeColor property at runtime, simply set the property to the desired value.

this.ForeColor = Color.Blue;


FormBorderStyle property

The default value is set to Sizable. This property determines how the border looks and behaves at runtime. This includes whether the border is resizeable and which control boxes (if any) appear in the caption bar.

The border of the application can reflect one of several options listed in the property FormBorderStyle:

  • Fixed3D - a fixed, three-dimensional border.
  • FixedDialog - a thick, fixed dialog-style border.
  • FixedSingle - a fixed, single-line border.
  • FixedToolWindow - a tool window border that is not resizeable. A tool window does not appear in the task bar or in the window that appears when the user presses ALT+TAB. Although forms that specify FixedToolWindow typically are not shown in the task bar, you must also ensure that the ShowInTaskbar property is set to false, since its default value is true.
  • None - no border.
  • Sizable - a resizeable border.
  • SizableToolWindow - a resizeable tool window border. A tool window does not appear in the taskbar or in the window that appears when the user presses ALT+TAB.



To change the FormBorderStyle property at runtime, simply set the property to the desired value.

this.FormBorderStyle = FormBorderStyle.FixedSingle;


RightToLeft property

The default value is set to RightToLeft.No. This property is inherited from Control.RightToLeft. This allows the controls on a form to be aligned to support locales using right-to-left fonts. This is used in international applications where the language is written from right to left.

Note:
If the value of the RightToLeft property is changed at runtime, only raw text without formatting is preserved.

To show this in action, the same application is shown here with the right application having the RightToLeft property set to true.


Image:526_12.jpg


Here is a downloadable version of this sample application (written in C# Express).

To change the RightToLeft property at runtime, simply set the property to the desired value.

this.RightToLeft = RightToLeft.Yes;


RightToLeftLayout property

The default value is set to false. This property allows the controls on a form to be mirrored. This is used in international applications where the language is written from right to left. To use this property, the RightToLeft property must be set to true.

To show this in action, the same application is shown here with the right application having the RightToLeftLayout property set to true.


Image:526_13.jpg


Here is a downloadable version of this sample application (written in C# Express).

To change the RightToLeftLayout property at runtime, simply set the property to the desired value.

this.RightToLeftLayout = true;


Text property

The default value is set to the name of the Form. This property overrides Control.Text. This sets or gets the text that appears in the caption bar of the form.

To change the the caption at runtime simply use the assignment operator. For example:

Form1.Text = "My simple Form";


UseWaitCursor property

The default value is set to False. This property is inherited from Control.UseWaitCursor and determines whether to use the wait cursor for the current control and all child controls.

When you set this property to true, the wait cursor is used instead of the default cursor over the whole of the form.

To change the UseWaitCursor property at runtime, simply set the property to the desired value.

this.UseWaitCursor = true;


prevpp.png  nextpp.png
C# Online.NET