-1

I didn't find a way in C# for creating a button which will support opacity - instead of just appearing when show method is called, to have the ability to slowly fade it into view.

I created my own button and I would like to know what you think of the implementation.

Basically, I created a Windows Form, which supports opacity property and handled all the corner cases regarding "adding" a form to another form, specifically: - location changed event - the owner form lost focus

The form consist of a label, which represents the button's text and that's all.

The form constructor gets the text for the label, the desired size of the button and the speed (based on an Enum) for the button appear. In the form load method the label is being located in the middle of the button

when the label or the form itself clicked a simple graphic is performed and an event is raised to whoever catches it.

My Code:

  1. Created a Form - named buttonForm
  2. Constructor

            InitializeComponent();
            this.Owner = owner;
            _buttonText = buttonText;
            _buttonSize = buttonSize;
            _usedSpeedOpacity = SelectSpeedOpacity(showSpeed);
    
  3. A method to illustrate click command - being called when the user clicks on the form and on the label itself:

        this.Location = new Point(this.Location.X + 1, this.Location.Y);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        Thread.Sleep(50);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
        this.Location = new Point(this.Location.X - 1, this.Location.Y);
        if (Button_Clicked != null)
        {
            Button_Clicked();
        }
    
  4. form load - locating the label in the middle of the control etc

        labelButtonText.Text = _buttonText;
        this.Size = _buttonSize;
        double remainning = this.Width - labelButtonText.Size.Width;
        Point labelNewLocation = new Point(
            (int)(remainning / 2),
            (int)(this.Height / 2 - this.Font.Height / 2));
        labelButtonText.Location = labelNewLocation;
    
  5. FadeShow (and FadeHide the same)

        int tempCounter = 0;
        Opacity = 0;
        Show();
        while (tempCounter <= 1000)
        {
            if (Opacity == 1.0)
            {
                break;
            }
            if (tempCounter % 10 == 0)
            {
                Opacity += _usedSpeedOpacity;
            }
            Refresh();
            tempCounter++;
        }
        this.Visible = true;
        this.BringToFront();
    
  6. Update location method so when the parent form moves i call this method

        this.Location = new Point(
            this.Location.X - (ParentFormLocation.X - newLocation.X),
            this.Location.Y - (ParentFormLocation.Y - newLocation.Y));
        ParentFormLocation = newLocation;
    
  7. An event of my button_click

Thanks in advance,

Oz.

Oz Radiano
  • 779
  • 2
  • 11
  • 30
  • 1
    Check this http://stackoverflow.com/questions/9358500/winforms-making-a-control-transparent/9359642#9359642 – Amen Ayach Feb 20 '12 at 20:16
  • Nice one but its not exactly what i need if I understand right - this is a control which supports opacity and i need an opacity fade in and out control – Oz Radiano Feb 20 '12 at 21:46
  • Interesting method however I believe this would mean the current thread would be constantly busy, perhaps if you used a background worker you could report the progress back and then update opacity that way. If you want I can post code – Neo Feb 25 '12 at 22:32
  • You mean that a background thread will paint the button? – Oz Radiano Feb 26 '12 at 12:07

1 Answers1

0

I did something similar with a windows form that changed its height. The way I did it was to implement a timer within the code so every half a second the opacity changed.

While the below is not a 100% correct answer without your code its difficult to show you.

void timer_Tick(object sender, EventArgs e)
        {            
            if (btnName.Opacity < 100)
            {
                btnName.Opacity++;
                timer2.Stop();
                timer2.Interval = 5000;
                timer2.Start();
            } else {
                timer2.Stop();
           }
        }
Neo
  • 2,305
  • 4
  • 36
  • 70
  • Nice. Although i prefer less timers in my application, its better than opening a thread for that matter. What do you say about my implementation ? – Oz Radiano Feb 21 '12 at 15:10
  • 1
    Can you post the implementation code as I tend to understand code better :) – Neo Feb 23 '12 at 12:06