70-526 MCTS


Appearance

Windows Forms applications can be customized to appear many different ways.

Note

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

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

The default colour when you create a new form is System.Drawing.SystemColors.Control. This is the control colour 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, and then click on the Advanced button. This will bring up the Advanced Appearance dialog.

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

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

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

The MSDN Page has two notes to mention about adding images.

1. Images with translucent or transparent colours 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 run time, use the Image.FromFile method.

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

BackgroundImageLayout

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

  • Center - The image is centred 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.

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

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

    this.BackgroundImageLayout = ImageLayout.Center;

    Cursor

    The default value is set to Cursors.Default. This property is inherited from Control.Cursor and 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 (MSDN). By modifying the code supplied in the aforementioned MSDN page, we can draw all of the system cursors.

    Here is the code to do 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 in the aforementioned MSDN page shows how to use each cursor over a panel control.

    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

    The default value is set to Cursors.Default. 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 run-time 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);
    }

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

    You can change the font of any control using a FontDialog:

    using System.Windows.Forms;
    ...
    FontDialog fontDialog = new FontDialog(); 
    if ( fontDialog.ShowDialog() != DialogResult.Cancel )
    {
      textBox.Font = fontDialog.Font;
    }

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

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

    ForeColor

    The default value is set to System.Drawing.SystemColors.ControlText. This property overrides Control.ForeColor and allows the foreground colour of the control to be changed.

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

    this.ForeColor = Color.Blue;

    FormBorderStyle

    The default value is set to Sizable. This property determines how the border looks and behaves at run time. This includes whether the border is resizable, 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 resizable. A tool window does not appear in the taskbar or in the window that appears when the user presses ALT+TAB. Although forms that specify FixedToolWindow typically are not shown in the taskbar, you must also ensure that the ShowInTaskbar property is set to false, since its default value is true.
  • None - No border.
  • Sizable - A resizable border.
  • SizableToolWindow - A resizable 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 run time, simply set the property to the desired value.

    this.FormBorderStyle = FormBorderStyle.FixedSingle;

    RightToLeft

    The default value is set to No. This property is inherited from Control.RightToLeft and 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 run time, 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.

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

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

    this.RightToLeft = RightToLeft.Yes;

    RightToLeftLayout

    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.

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

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

    this.RightToLeftLayout = true;

    Text

    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 Text property at run time, simply set the property to the desired value.

    this.Text = "My simple Form";

    UseWaitCursor

    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 run time, simply set the property to the desired value.

    this.UseWaitCursor = true;

    MSDN references

  • Form.BackColor Property
  • Control.BackgroundImage Property
  • Control.BackgroundImageLayout Property
  • Control.Cursor Property
  • Control.Font Property
  • Control.ForeColor Property
  • Form.FormBorderStyle Property
  • Control.RightToLeft Property
  • Form.RightToLeftLayout Property
  • Form.Text Property
  • Control.UseWaitCursor Property
  • AmbientProperties Class


    << Previous Contents Next >>

    © Publicjoe, 2007