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:
- Created a Form - named buttonForm
Constructor
InitializeComponent(); this.Owner = owner; _buttonText = buttonText; _buttonSize = buttonSize; _usedSpeedOpacity = SelectSpeedOpacity(showSpeed);
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(); }
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;
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();
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;
An event of my button_click
Thanks in advance,
Oz.