Questa documentazione è stata archiviata e non viene gestita.

Interfaccia ITypeDescriptorFilterService

Fornisce un'interfaccia per modificare l'insieme dei descrittori di membro per un componente in modalità progettazione.

Spazio dei nomi: System.ComponentModel.Design
Assembly: System (in system.dll)

public interface ITypeDescriptorFilterService
public interface ITypeDescriptorFilterService
public interface ITypeDescriptorFilterService

L'interfaccia ITypeDescriptorFilterService consente la modifica di proprietà, eventi e attributi a livello di classe di un componente in fase di progettazione. Questa modifica viene apportata mediante il gruppo di descrittori fornito da un componente tramite un oggetto TypeDescriptor. Il descrittore del tipo esegue una query al sito del componente per il servizio ITypeDescriptorFilterService e, se questo esiste, passa tutti i metadati raccolti al servizio. Il servizio, quindi, può modificare i metadati aggiungendo, rimuovendo e modificando le caratteristiche del componente.

È possibile, ad esempio, modificare le proprietà di un componente mediante una chiamata al metodo FilterProperties. Il servizio filtro ottiene un dizionario contenente i nomi delle proprietà e i relativi descrittori di tipo PropertyDescriptor. Le modifiche vengono implementate in base alla logica riportata di seguito.

Modifica

Implementazione

Rimozione

Elimina la voce del dizionario corrispondente.

Aggiunta

Aggiunge al dizionario la voce appropriata.

Modifica

Chiama i metodi del descrittore di proprietà esistenti, sostituisce la voce del descrittore di proprietà associato o l'intera coppia chiave/valore della proprietà nel dizionario.

Il valore restituito dal metodo FilterProperties determina se il gruppo di proprietà è fisso. Se il valore restituito dal metodo è true, l'oggetto TypeDescriptor per il componente può memorizzare i risultati nella cache. Tale cache viene gestita finché il componente viene raccolto nel Garbage Collector oppure viene chiamato il metodo Refresh del descrittore del tipo.

Note per gli implementatori: Per filtrare i descrittori di tipo esposti da un oggetto TypeDescriptor, implementare questa interfaccia su un componente ed eseguire l'override dei metodi FilterAttributes, FilterEvents o FilterProperties della classe per filtrarne rispettivamente attributi, eventi o proprietà.

Nell'esempio di codice riportato di seguito viene illustrata una finestra di progettazione in cui viene utilizzato l'oggetto ITypeDescriptorFilterService per filtrare l'insieme di attributi di un oggetto Button nuovo o già esistente, così da aggiungere un attributo della finestra di progettazione per una nuova finestra di progettazione prima di caricare o ricaricare la finestra di progettazione per il pulsante.

Per utilizzare l'esempio, aggiungere il codice a un progetto Windows Forms e caricare i componenti dalla libreria di classi nella casella degli strumenti.

Procedura dettagliata: compilazione automatica della casella degli strumenti con componenti personalizzati
Procedura dettagliata: compilazione automatica della casella degli strumenti con componenti personalizzati
Procedura dettagliata: compilare automaticamente la casella degli strumenti con componenti personalizzati
Procedura dettagliata: compilare automaticamente la casella degli strumenti con componenti personalizzati

Aggiungere dei pulsanti al form. Aggiungere un ButtonDesignerFilterComponent al form, che verrà visualizzato nella barra dei componenti. L'oggetto ButtonDesignerFilterComponent aggiunge un oggetto ButtonDesignerFilterService, che implementa un'interfaccia ITypeDescriptorFilterService, come provider di servizi in modalità progettazione. Gli oggetti Button nuovi o esistenti del form cambieranno colore in modo ciclico dopo che l'oggetto ButtonDesignerFilterService avrà sostituito la finestra di progettazione per ciascun pulsante nuovo o esistente con ColorCycleButtonDesigner. I pulsanti cambieranno colore spostando su di essi il puntatore del mouse e potranno continuare a cambiare colore o reimpostare i colori di sfondo e primo piano al verificarsi di eventi MouseLeave. A nuovi oggetti Button sarà assegnato il ColorCycleButtonDesigner mediante il metodo FilterAttributes del ButtonDesignerFilterService caricato, fino a quando ButtonDesignerFilterComponent non viene eliminato e sostituisce l'oggetto ITypeDescriptorFilterService originale. La classe ColorCycleButton di questo esempio è un Button associato a un ColorCycleButtonDesigner.

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Reflection;
using System.Timers;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ITypeDescriptorFilterSample
{	
    // Component to host the ButtonDesignerFilterComponentDesigner that loads the 
    // ButtonDesignerFilterService ITypeDescriptorFilterService.
    [Designer(typeof(ButtonDesignerFilterComponentDesigner))]
    public class ButtonDesignerFilterComponent : System.ComponentModel.Component
    {		
        public ButtonDesignerFilterComponent(System.ComponentModel.IContainer container)
        {
            container.Add(this);
        }

        public ButtonDesignerFilterComponent()
        {}
    }

    // Provides a designer that can add a ColorCycleButtonDesigner to each 
    // button in a design time project using the ButtonDesignerFilterService 
    // ITypeDescriptorFilterService.
    public class ButtonDesignerFilterComponentDesigner : System.ComponentModel.Design.ComponentDesigner
    {
        // Indicates whether the service has been loaded.
        private bool serviceloaded = false;		
        // Stores any old ITypeDescriptorFilterService to restore later.
        private ITypeDescriptorFilterService oldservice = null;
        
        public ButtonDesignerFilterComponentDesigner()
        {}
        
        // Loads the new ITypeDescriptorFilterService and reloads the 
        // designers for each button.
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);
            
            // Loads the custom service if it has not been loaded already
            LoadService();
            
            // Build list of buttons from Container.Components.
            ArrayList buttons = new ArrayList();
            foreach(IComponent c in this.Component.Site.Container.Components)
                if(c.GetType() == typeof(System.Windows.Forms.Button))
                    buttons.Add((System.Windows.Forms.Button)c);
            if(buttons.Count > 0)
            {		
                // Tests each Button for an existing 
                // ColorCycleButtonDesigner;
                // if it has no designer of type 
                // ColorCycleButtonDesigner, adds a designer.
                foreach(System.Windows.Forms.Button b in buttons)
                {
                    bool loaddesigner = true;
                    // Gets the attributes for each button.
                    AttributeCollection ac = TypeDescriptor.GetAttributes(b);
                    for(int i=0; i<ac.Count; i++)
                    {
                        // If designer attribute is not for a 
                        // ColorCycleButtonDesigner, adds a new 
                        // ColorCycleButtonDesigner.
                        if( ac[i] is DesignerAttribute )
                        {
                            DesignerAttribute da = (DesignerAttribute)ac[i];
                            if( da.DesignerTypeName.Substring(da.DesignerTypeName.LastIndexOf(".")+1) == "ColorCycleButtonDesigner" )
                                loaddesigner = false;
                        }
                    }
                    if(loaddesigner)
                    {
                        // Saves the button location so that it 
                        // can be repositioned.
                        Point p = b.Location;	
                    
                        // Gets an IMenuCommandService to cut and 
                        // paste control in order to register with 
                        // selection and movement interface after 
                        // designer is changed without reloading.
                        IMenuCommandService imcs = (IMenuCommandService)this.GetService(typeof(IMenuCommandService));
                        if( imcs == null )
                            throw new Exception("Could not obtain IMenuCommandService interface.");							
                        // Gets an ISelectionService to select the 
                        // button so that it can be cut and pasted.
                        ISelectionService iss = (ISelectionService)this.GetService(typeof(ISelectionService));
                        if( iss == null)
                            throw new Exception("Could not obtain ISelectionService interface.");
                        iss.SetSelectedComponents(new IComponent[] { b }, SelectionTypes.Auto);						
                        // Invoke Cut and Paste.
                        imcs.GlobalInvoke(StandardCommands.Cut);
                        imcs.GlobalInvoke(StandardCommands.Paste);							
                        // Regains reference to button from 
                        // selection service.
                        System.Windows.Forms.Button b2 = (System.Windows.Forms.Button)iss.PrimarySelection;
                        iss.SetSelectedComponents(null);							
                        // Refreshes TypeDescriptor properties of 
                        // button to load new attributes from
                        // ButtonDesignerFilterService.
                        TypeDescriptor.Refresh(b2);							
                        b2.Location = p;
                        b2.Focus();
                    }
                }
            }
        }

        // Loads a ButtonDesignerFilterService ITypeDescriptorFilterService 
        // to add ColorCycleButtonDesigner designers to each button.
        private void LoadService()
        {
            // If no custom ITypeDescriptorFilterService is loaded, 
            // loads it now.
            if(!serviceloaded)
            {
                // Stores the current ITypeDescriptorFilterService 
                // to restore later.
                ITypeDescriptorFilterService tdfs = (ITypeDescriptorFilterService)this.Component.Site.GetService(typeof(ITypeDescriptorFilterService));
                if( tdfs != null )								
                    oldservice = tdfs;								
                // Retrieves an IDesignerHost interface to use to 
                // remove and add services.
                IDesignerHost dh = (IDesignerHost)this.Component.Site.GetService(typeof(IDesignerHost));
                if( dh == null )
                    throw new Exception("Could not obtain IDesignerHost interface.");				
                // Removes standard ITypeDescriptorFilterService.
                dh.RemoveService(typeof(ITypeDescriptorFilterService));
                // Adds new custom ITypeDescriptorFilterService.
                dh.AddService(typeof(ITypeDescriptorFilterService), new ButtonDesignerFilterService());				
                serviceloaded = true;
            }
        }
        
        // Removes the custom service and reloads any stored, 
        // preexisting service.
        private void RemoveService()
        {
            IDesignerHost dh = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            if( dh == null )
                throw new Exception("Could not obtain IDesignerHost interface.");
            dh.RemoveService(typeof(ITypeDescriptorFilterService));
            if( oldservice != null )
                dh.AddService(typeof(ITypeDescriptorFilterService), oldservice);
            serviceloaded = false;
        }

        protected override void Dispose(bool disposing)
        {
            if(serviceloaded)
                RemoveService();
        }
    }

    // Provides a TypeDescriptorFilterService to add the 
    // ColorCycleButtonDesigner using a DesignerAttribute.
    public class ButtonDesignerFilterService : System.ComponentModel.Design.ITypeDescriptorFilterService
    {
        public ITypeDescriptorFilterService oldService = null;

        public ButtonDesignerFilterService()
        {}

        public ButtonDesignerFilterService(ITypeDescriptorFilterService oldService_)
        {
            // Stores any previous ITypeDescriptorFilterService to implement service chaining.
            this.oldService = oldService_;
        }

        public bool FilterAttributes(System.ComponentModel.IComponent component, System.Collections.IDictionary attributes)
        {
            if(oldService != null)
                oldService.FilterAttributes(component, attributes);
            
            // Creates a designer attribute to compare its TypeID with the TypeID of existing attributes of the component.
            DesignerAttribute da = new DesignerAttribute(typeof(ColorCycleButtonDesigner));
            // Adds the designer attribute if the attribute collection does not contain a DesignerAttribute of the same TypeID.
            if(component is System.Windows.Forms.Button && attributes.Contains(da.TypeId))
                attributes[da.TypeId]=da;
            return true;
        }

        public bool FilterEvents(System.ComponentModel.IComponent component, System.Collections.IDictionary events)
        {
            if(oldService != null)
                oldService.FilterEvents(component, events);
            return true;
        }

        public bool FilterProperties(System.ComponentModel.IComponent component, System.Collections.IDictionary properties)
        {
            if(oldService != null)
                oldService.FilterProperties(component, properties);
            return true;
        }
    }
    
    // Designer for a Button control which cycles the background color.
    public class ColorCycleButtonDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        private System.Windows.Forms.Timer timer1;
        private Color initial_bcolor, initial_fcolor;
        private int r, g, b;
        private bool ru, gu, bu, continue_;

        public ColorCycleButtonDesigner()
        {						
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 50;
            timer1.Tick += new EventHandler(this.Elapsed);
            ru = true;
            gu = false;
            bu = true;			
            continue_ = false;
            timer1.Start();
        }

        private void Elapsed(object sender, EventArgs e)
        {
            this.Control.BackColor = Color.FromArgb(r%255, g%255, b%255);
            this.Control.Refresh();

            // Updates color.		
            if(ru)
                r+=10;
            else if(r>10) 
                r-=10;
            if(gu)
                g+=10;
            else if(g>10)
                g-=10;
            if(bu)
                b+=10;
            else if(b>10)
                b-=10;

            // Randomly switches direction of color component values.
            Random rand = new Random();
            for(int i=0; i<4; i++)			
                switch(rand.Next(0, 2))
                {
                    case 0:
                        if(ru)
                            ru=false;
                        else
                            ru=true;
                        break;
                    case 1:
                        if(gu)
                            gu=false;
                        else
                            gu=true;
                        break;
                    case 2:
                        if(bu)
                            bu=false;
                        else
                            bu=true;
                        break;
                }

            this.Control.ForeColor = Color.FromArgb((this.Control.BackColor.R+128)%255, (this.Control.BackColor.G+128)%255, (this.Control.BackColor.B+128)%255);
        }

        protected override void OnMouseEnter()
        {
            if(!timer1.Enabled)
            {
                initial_bcolor = this.Control.BackColor;
                initial_fcolor = this.Control.ForeColor;
                r = initial_bcolor.R;
                g = initial_bcolor.G;
                b = initial_bcolor.B;			
                timer1.Start();
            }
        }

        protected override void OnMouseLeave()
        {
            if(!continue_)
            {
                continue_ = true;
                timer1.Stop();
            }
            else
                continue_ = false;
            
            this.Control.BackColor = initial_bcolor;
            this.Control.ForeColor = initial_fcolor;			
        }

        protected override void Dispose(bool disposing)
        {
            timer1.Stop();
            this.Control.BackColor = initial_bcolor;
            this.Control.ForeColor = initial_fcolor;
            base.Dispose(disposing);
        }
    }

    // System.Windows.Forms.Button associated with the ColorCycleButtonDesigner.
    [Designer(typeof(ColorCycleButtonDesigner))]
    public class ColorCycleButton : System.Windows.Forms.Button
    {
        public ColorCycleButton()
        {}
    }
}

package ITypeDescriptorFilterSample;

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.ComponentModel.Design.Serialization.*;
import System.Drawing.*;
import System.Reflection.*;
import System.Timers.*;
import System.Windows.Forms.*;
import System.Windows.Forms.Design.*;

// Component to host the ButtonDesignerFilterComponentDesigner that loads the 
// ButtonDesignerFilterService ITypeDescriptorFilterService.
/** @attribute Designer(ButtonDesignerFilterComponentDesigner.class)
 */
public class ButtonDesignerFilterComponent
    extends System.ComponentModel.Component
{     
    public ButtonDesignerFilterComponent(System.ComponentModel.
        IContainer container) 
    {
        container.Add(this);
    } //ButtonDesignerFilterComponent
         
    public ButtonDesignerFilterComponent() 
    {
    } //ButtonDesignerFilterComponent
} //ButtonDesignerFilterComponent
      
// Provides a designer that can add a ColorCycleButtonDesigner to each 
// button in a design time project using the ButtonDesignerFilterService 
// ITypeDescriptorFilterService.
public class ButtonDesignerFilterComponentDesigner
    extends System.ComponentModel.Design.ComponentDesigner
{
    // Indicates whether the service has been loaded.
    private boolean serviceLoaded = false;
    // Stores any old ITypeDescriptorFilterService to restore later.
    private ITypeDescriptorFilterService oldService = null;
     
    public ButtonDesignerFilterComponentDesigner()
    {
    } //ButtonDesignerFilterComponentDesigner

    // Loads the new ITypeDescriptorFilterService and reloads the 
    // designers for each button.
    public void Initialize(System.ComponentModel.IComponent component)
        throws Exception
    {
        super.Initialize(component);
        // Loads the custom service if it has not been loaded already
        LoadService();
         
        // Build list of buttons from Container.Components.
        ArrayList buttons =  new ArrayList();
        for(int iCtr = 0; iCtr < this.get_Component().get_Site().
            get_Container().get_Components().get_Count(); iCtr++) {
            
            IComponent c = this.get_Component().get_Site().get_Container().
                get_Components().get_Item(iCtr);
            if(c.GetType().Equals(System.Windows.Forms.
                Button.class.ToType())) {
                buttons.Add((System.Windows.Forms.Button)c);
            }
        }

        if (buttons.get_Count() > 0) {
            // Tests each Button for an existing 
            // ColorCycleButtonDesigner;
            // if it has no designer of type 
            // ColorCycleButtonDesigner, adds a designer.
            for (int iCtr=0; iCtr<buttons.get_Count(); iCtr++ ){                
                System.Windows.Forms.Button b =(System.Windows.
                    Forms.Button) buttons.get_Item(iCtr);
                boolean loadDesigner = true;
                // Gets the attributes for each button.
                AttributeCollection ac = TypeDescriptor.GetAttributes(b);
                for (int i = 0; i < ac.get_Count(); i++) {
                    // If designer attribute is not for a 
                    // ColorCycleButtonDesigner, adds a new 
                    // ColorCycleButtonDesigner.
                    if (ac.get_Item(i) instanceof DesignerAttribute) {
                        DesignerAttribute da = (DesignerAttribute)ac.
                        get_Item(i);

                        if (da.get_DesignerTypeName().Substring((da.
                            get_DesignerTypeName().LastIndexOf(".")+ 1)).
                            Equals("ColorCycleButtonDesigner")) {
                            loadDesigner = false;
                        }
                    }
                }
                if (loadDesigner) {
                    // Saves the button location so that it 
                    // can be repositioned.
                    Point p = b.get_Location();
                    // Gets an IMenuCommandService to cut and 
                    // paste control in order to register with 
                    // selection and movement interface after 
                    // designer is changed without reloading.
                    IMenuCommandService imcs = (IMenuCommandService)(this.
                        GetService(IMenuCommandService.class.ToType()));
                    if (imcs == null) {
                        throw new Exception("Could not obtain" 
                            + "ISelectionService interface.");
                    }
                    // Gets an ISelectionService to select the 
                    // button so that it can be cut and pasted.
                    ISelectionService iss = (ISelectionService)this.
                        GetService(ISelectionService.class.ToType());
                        if (iss == null) {
                            throw new Exception("Could not obtain"
                                + "ISelectionService interface.");
                    }
                    iss.SetSelectedComponents(new IComponent[] {b},
                        SelectionTypes.Normal);
                    
                    // Invoke Cut and Paste.
                    imcs.GlobalInvoke(StandardCommands.Cut);
                    imcs.GlobalInvoke(StandardCommands.Paste);
                    // Regains reference to button from 
                        
                    // selection service.
                    System.Windows.Forms.Button b2 = (System.Windows.
                        Forms.Button)iss.get_PrimarySelection();
                    iss.SetSelectedComponents(null);
                    // Refreshes TypeDescriptor properties of 
                    // button to load new attributes from
                    // ButtonDesignerFilterService.
                    TypeDescriptor.Refresh(b2);
                    b2.set_Location(p);
                    b2.Focus();
                }
            }
        }
    } //Initialize
      
    // Loads a ButtonDesignerFilterService ITypeDescriptorFilterService 
    // to add ColorCycleButtonDesigner designers to each button.
    private void LoadService() throws Exception 
    {
        // If no custom ITypeDescriptorFilterService is loaded, 
        // loads it now.
        if (!(serviceLoaded)) {
            // Stores the current ITypeDescriptorFilterService 
            // to restore later.
            ITypeDescriptorFilterService tdfs =
                (ITypeDescriptorFilterService)(this.get_Component().get_Site().
                GetService(ITypeDescriptorFilterService.class.ToType()));
            if (tdfs != null) {
                oldService = tdfs;
            }
            // Retrieves an IDesignerHost interface to use to 
            // remove and add services.
            IDesignerHost dh = (IDesignerHost) this.get_Component().
                get_Site().GetService(IDesignerHost.class.ToType());
            
            if (dh == null) {
                throw new Exception("Could not obtain IDesignerHost interface.");
            }
            // Removes standard ITypeDescriptorFilterService.
            dh.RemoveService(ITypeDescriptorFilterService.class.ToType());
            // Adds new custom ITypeDescriptorFilterService.
            dh.AddService(ITypeDescriptorFilterService.class.ToType(),
                new ButtonDesignerFilterService());
            serviceLoaded = true;
        }
    } //LoadService
      
    // Removes the custom service and reloads any stored, 
    // preexisting service.
    private void RemoveService() throws Exception 
    {
        IDesignerHost dh 
            = (IDesignerHost)(this.GetService(IDesignerHost.class.ToType()));
        if (dh == null) {
            throw new Exception("Could not obtain IDesignerHost interface.");
        }
        dh.RemoveService(ITypeDescriptorFilterService.class.ToType());
        if (oldService != null) {
            dh.AddService(ITypeDescriptorFilterService.
                class.ToType(),oldService);
        }
        serviceLoaded = false;
    } //RemoveService
      
    protected void Dispose(boolean disposing) throws Exception
    {
        if (serviceLoaded) {
            RemoveService();
        }
    } //Dispose
} //ButtonDesignerFilterComponentDesigner
 
// Provides a TypeDescriptorFilterService to add the 
// ColorCycleButtonDesigner using a DesignerAttribute.
public class ButtonDesignerFilterService
    implements System.ComponentModel.Design.ITypeDescriptorFilterService
{
    public ITypeDescriptorFilterService oldService = null;
    public ButtonDesignerFilterService() 
    {
    } //ButtonDesignerFilterService
    
    public ButtonDesignerFilterService(
        ITypeDescriptorFilterService oldService_) 
    {
        // Stores any previous ITypeDescriptorFilterService to implement 
        //service chaining.
        this.oldService = oldService_;
    } //ButtonDesignerFilterService
     
    public boolean FilterAttributes(System.ComponentModel.IComponent component,
        System.Collections.IDictionary attributes) 
    {
        if (oldService != null ) {
            oldService.FilterAttributes(component, attributes);
        }
         
        // Creates a designer attribute to compare its TypeID with the TypeID
        //of existing attributes of the component.
        DesignerAttribute da = new DesignerAttribute(ColorCycleButtonDesigner
            .class.ToType());
        // Adds the designer attribute if the attribute collection does not
        //contain a DesignerAttribute of the same TypeID.
        if (component instanceof System.Windows.Forms.Button 
            && attributes.Contains(da.get_TypeId())) {
            attributes.set_Item( da.get_TypeId(), da );
        }
        return true ;
    } //FilterAttributes
      
    public boolean FilterEvents(System.ComponentModel.IComponent component,
        System.Collections.IDictionary events) 
    {
        if (oldService != null) {
            oldService.FilterEvents(component, events);
        }
        return true ;
    } //FilterEvents

    public boolean FilterProperties(System.ComponentModel.IComponent component,
        System.Collections.IDictionary properties)    
    {
        if (oldService  != null) {
            oldService.FilterProperties(component, properties);
        }
        return true ;
    } //FilterProperties
} //ButtonDesignerFilterService

// Designer for a Button control which cycles the background color.
public class ColorCycleButtonDesigner
    extends System.Windows.Forms.Design.ControlDesigner
{
    private System.Windows.Forms.Timer timer1;
    private Color initial_bcolor, initial_fcolor;
    private int r, g, b;
    private boolean ru, gu, bu, continue_;
      
    public ColorCycleButtonDesigner()
    {
        timer1 = new System.Windows.Forms.Timer();
        timer1.set_Interval(50);
        timer1.add_Tick(new EventHandler(this.Elapsed));
        ru = true;
        gu = false;
        bu = true;
        continue_ = false;
        timer1.Start();
    } //ColorCycleButtonDesigner
      
    private void Elapsed(Object sender, EventArgs e) 
    {
        this.get_Control().set_BackColor(Color.FromArgb(r % 255,
            g % 255, b % 255));
        this.get_Control().Refresh();
         
        // Updates color.        
        if (ru) {
            r += 10;
        }
        else {
            if (r > 10) {
                r -= 10;
            }
        }
        if( gu) {
            g += 10;
        }
        else {
            if (g > 10) {
                g -= 10;
            }
        }
        if (bu) {
            b += 10;
        }
        else {
            if ( b > 10  ) {
                b -= 10;
            }
        } 
        // Randomly switches direction of color component values.
        Random rand =  new Random();
        for (int i=0; i < 4; i++) {
            switch (rand.Next(0, 2)) {
                case 0 : 
                    if (ru) {
                    ru = false;
                    }
                    else {
                        ru = true;
                    }
                    break;
                case 1 : 
                    if (gu) {
                        gu = false;
                    }
                    else {
                        gu = true;
                    }
                    break;
                case 2 : 
                    if (bu) {
                        bu = false;
                    }
                    else {
                        bu = true;
                    }
                    break;
            }
        }  
        this.get_Control().set_ForeColor(Color.FromArgb((this.get_Control().
            get_BackColor().get_R() + 128) % 255,(this.get_Control().
            get_BackColor().get_G() + 128) % 255,(this.get_Control().
            get_BackColor().get_B() + 128) % 255));
    } //Elapsed
      
    protected void OnMouseEnter() 
    {
        if (!(timer1.get_Enabled())) {
            initial_bcolor = this.get_Control().get_BackColor();
            initial_fcolor = this.get_Control().get_ForeColor();
            r = initial_bcolor.get_R();
            g = initial_bcolor.get_G();
            b = initial_bcolor.get_B();
            timer1.Start();
        }
    } //OnMouseEnter
     
    protected void OnMouseLeave()
    {
        if (!(continue_)) {
            continue_ = true;
            timer1.Stop();
        }
        else {
            continue_ = false;
        } 
        this.get_Control().set_BackColor(initial_bcolor);
        this.get_Control().set_ForeColor(initial_fcolor);
    } //OnMouseLeave
    protected void Dispose(boolean disposing) 
    {
        timer1.Stop();
        this.get_Control().set_BackColor(initial_bcolor);
        this.get_Control().set_ForeColor(initial_fcolor);
        super.Dispose(disposing);
    } //Dispose
} //ColorCycleButtonDesigner
  
// System.Windows.Forms.Button associated with the ColorCycleButtonDesigner.
/** @attribute Designer(ColorCycleButtonDesigner.class)
 */
public class ColorCycleButton extends System.Windows.Forms.Button
{
    public ColorCycleButton() 
    {
    } //ColorCycleButton
} //ColorCycleButton

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

.NET Framework

Supportato in: 2.0 1.1 1.0
Mostra: