9.1 Extending Common Controls

 <  Day Day Up  >  

You want to create a new control that is similar to a control that is already defined.


Technique

To create a custom control derived from an existing Windows Form control, click on Project, Add User Control from the main menu or Alt+P, U on the keyboard. After Visual Studio creates the control file, open it in Code View by right-clicking on the file in Solution Explorer and selecting View Code from the Context menu.

By default, a user control derives from System.Windows.Forms.UserControl . You can change it to any of the defined Windows Forms controls or to a control already created within a Windows Control Library. To alter the behavior or appearance of the control, override any properties or methods defined in the base class. In Listing 9.1, you can see a control that is derived from the Panel Windows Form control. When placed on a Windows Form, the control shows a preview of the screensaver, which you set using the custom ScreenSaverPath property.

To render the screensaver, you override the base class method OnPaint . This method is called whenever the control needs to render itself and is called as a result of the Paint event. OnPaint checks whether it needs to launch the screensaver, and if so, it uses the Process.Start method, passing the appropriate command-line arguments to the screensaver so that it renders within the control's window.

Listing 9.1 ScreenSaverPreview Control Based on the Panel Control
 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace _1_ComCtrlExtension {     public class ScreenSaverPreviewPanel : System.Windows.Forms.Panel     {         private System.ComponentModel.IContainer components;         private string screenSaver = "";         private Process saverProcess = null;         private bool launched = false;         public ScreenSaverPreviewPanel()         {             InitializeComponent();         }         protected override void OnPaint(PaintEventArgs e)         {             if( launched == false )             {                 try                 {                     if( saverProcess != null )                     {                         saverProcess.Kill();                         saverProcess = null;                     }                     if( this.Visible == true && screenSaver != "")                     {                         ProcessStartInfo startInfo = new ProcessStartInfo(                             screenSaver + " \"/p " + this.Handle.ToString() +                             "\"", "" );                         startInfo.UseShellExecute = false;                         saverProcess = Process.Start( startInfo );                         launched = true;                     }                     else                     {                         this.Visible = false;                     }                 }                 catch( Exception ex )                 {                     throw new Exception( "Could not launch screensaver", ex );                 }             }             base.OnPaint (e);         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if( components != null )                     components.Dispose();             }             base.Dispose( disposing );         }         #region Component Designer generated code>         private void InitializeComponent()         {             this.components = new System.ComponentModel.Container();         }         #endregion         public string ScreenSaverPath         {             get             {                 return screenSaver;             }             set             {                 this.Visible = true;                 screenSaver = value;                 launched = false;                 Invalidate();             }         }     } } 

Comments

A powerful feature of object-oriented programming is the ability to slightly alter the behavior of a class so that it better suits the problem you are trying to solve. Although there are several available controls for you to use, sometimes a certain control just doesn't do what you want it to do. If you come from a WIN32 background, the thought of creating a control that is based on an existing common control might conjure up fears of subclassing or performing various conniving tricks, such as writing to process memory. Although people like us find joy in such methods, it is still complicated and error-prone . With C# and the .NET Framework, creating a new control that is similar to an existing control simply entails defining a class that is derived from one of the many .NET Windows Forms controls.

When you derive a class from an existing Windows Form control, you inherit all the base functionality of the control you are deriving from. The control functions within the designer as a user sets properties and manipulates its positions . It properly interacts with its parent form when docking or anchoring, and it contains full functionality when a user is using it during runtime. All that you need is to override or create any properties or methods needed for the customization.

Listing 9.1 created a new control by deriving from the System.Windows.Forms.Panel control. If no properties or methods were overridden in the derived class, the control would behave exactly like any other Panel control. Although the thought of creating a new control that allows you to preview a screensaver, as shown in Figure 9.1, sounds like a complicated task, the process to do so is relatively trivial. You create a custom property so the control knows which screensaver to preview and override the OnPaint method so the screensaver successfully attaches to your control's window.

Figure 9.1. Windows Form application using the ScreenSaverPreviewPanel control.

graphics/09fig01.gif

In the code that accompanies this recipe, the custom control was created within the same executable assembly as the Windows Form that uses the control to reduce the amount of projects contained within this chapter's solution. In most cases, however, you want to create a Windows Control Library that creates a dynamic link library (DLL) assembly. Creating a control library is discussed in Recipe 2.13, "Creating and Using Class Libraries." When you create a control within a executable-based assembly, you can only use it on Windows Forms objects within the same assembly. By placing the control within a Windows Control Library, you can use it in any Windows Form application.

To place a control onto a Windows Form, you must first add it to the toolbox. Recipe 9.6, "Programmatically Adding Controls to the Toolbox," demonstrates how you do so programmatically during the installation of your control, but for testing, it is easier to manually perform the necessary steps. Right-click on the toolbox window and select Add/Remove Items. In the Customize Toolbox dialog, click on the Browse button and select the assembly that contains your control. An icon appears within the toolbox corresponding to your control. You can then drag and drop the icon onto a Windows Form just as you would with any other .NET control.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net