Hey there, I'm trying to implement a way to slide&fade controls around (more than one at the same time possibly) elegantly. So in other words, say i had a picture in the top left corner, and a texbox in the bottom right corner, i'd like to be able to have them slide. not just snap. slide, to the opposite corners and replace eachothers position. Ive been working for awhile but have not come up with anything efficient, even just some of the basic math calculations would be a great start.
I recommend that you switch to WPF; that would make it far easier.
It is completely impossible to fade controls in WinForms; Windows controls cannot have opacity.
The closest you can get would be to render the control and its area on the form to a pair of bitmaps, then crossfade the bitmaps in a PictureBox using a ColorMatrix.
To slide controls in WinForms, you can use a Timer to gradually change the Top
and/or Left
properties of the controls and move them across the form. However, you'll get an annoying flicker, which is (AFAIK) impossible to remove.
Save a copy of each of the items' x and y values as a float.
Use a timer, and upon each tick add a value to the top left item's x and y position. Subtract the same value from the bottom right item's x and y.
Make sure you use the cached float values here, otherwise you might get rounding errors when the item is relocated on the form.
If you're painting on whatever surface you're using for images/controls, invalidate it for each tick.
Should be easy-peasy.
Edit:
For transparent controls, check out Bob Powell's Tips & Tricks:
http://www.bobpowell.net/transcontrols.htm
And here is his own reply on how to make semitransparent controls:
http://www.mofeel.net/67-microsoft-public-dotnet-framework-windowsforms-controls/4860.aspx
Check out the dot-net-transitions project on Google Code. It supports a variety of linear/non-linear transitions including composite transitions that can be used for more complex effects such as ripple.
Here is a working sample that demonstrates your desired behavior:
var pictureBox = new PictureBox
{
ImageLocation = "http://bit.ly/aTsxKI",
SizeMode = PictureBoxSizeMode.AutoSize
};
var textBox = new TextBox
{
Text = "Hello World",
Location = new Point(140, 140)
};
var form = new Form
{
Controls =
{
textBox,
pictureBox
}
};
form.Click += (sender, e) =>
{
// swap the Left and Top properties using a transition
var t = new Transition(new TransitionType_EaseInEaseOut(1000));
t.add(pictureBox, "Left", textBox.Left);
t.add(pictureBox, "Top", textBox.Top);
t.add(textBox, "Left", pictureBox.Left);
t.add(textBox, "Top", pictureBox.Top);
t.run();
};
form.ShowDialog();
You could do this in WinForms, with a great deal of effort, so I would have to second the recommendations to use WPF (which is essentially built for exactly this kind of thing).
Your main obstacle to doing this in WinForms is the fact that a control location is specified by integer, which means you can't set a control's Left
property to 45.3425
, for example. This basically makes smooth animation of controls (assuming you want movement that changes speeds and directions) completely impossible - you will get an unavoidable jerkiness of movement this way (I've tried, so I know).
As SLaks suggested, the only way to do this in WinForms would be to "fake" it by taking "snapshots" of each control. Basically, you would start with an invisible Bitmap the size of your form, drawn with the form's BackColor. You would then create the "snapshots" by calling DrawToBitmap() on each control you wish to animate, and create the movement effect by drawing the snapshots onto the canvas (System.Drawing
can draw images with floating-point coordinates, avoiding the jerkiness of integer locations).
This is too much damn work, though. Just use WPF. :)
Edit: I should mention that it's actually easy to do something like this in WinForms, as long as you don't mind it looking awful and jerky and amateurish. My above comments refer to the difficulties of doing this well.