Animate WinForms - c#

I have a windows form.
I would like, when a certain button is click to animate an expansion of the form, revealing new section of the form (without having to forms, and animating one of them).
Is that possible?

You can get a nice enough effect by relying on a Timer. Here you have a sample code showing how to "animate" the increasing size of the main form after clicking on a button. By increasing/decreasing all the variables (X/Y inc. values or interval) you can get full control on how the animation "looks". Just include a button (button1) and a timer (timer1) on your form and the code below.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int timerInterval, curWidth, curHeight, incWidth, incHeight, maxWidth, maxHeight;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
curWidth = this.Location.X + this.Width;
curHeight = this.Location.Y + this.Height;
incWidth = 100;
incHeight = 20;
maxWidth = 2000;
maxHeight = 1500;
timerInterval = 100;
timer1.Enabled = false;
timer1.Interval = timerInterval;
}
private void timer1_Tick(object sender, EventArgs e)
{
curWidth = curWidth + incWidth;
curHeight = curHeight + incHeight;
if (curWidth >= maxWidth)
{
curWidth = maxWidth;
}
if (curHeight >= maxHeight)
{
curHeight = maxHeight;
}
this.Width = curWidth;
this.Height = curHeight;
if (this.Width == maxWidth && this.Height == maxHeight)
{
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
}
}
}

Related

Programming Scary Screamer on C#

I am new in c# and trying to develop Scary Screamer Application. It is an joke windows forms application which is running on the PC and invisible in taskbar.
There is timer running in this application. If system datetime.now.minute = 15
It should play scary sound and show scary picture on the screen. After 1-2 seconds picture should disappear from the screen.
But i am stuck and don't know how to make picture disappear. Any Ideas how to do that?
Below is my code:
namespace screamer2
{
public partial class Form1 : Form
{
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Width = Screen.PrimaryScreen.Bounds.Width/2;
this.Height = Screen.PrimaryScreen.Bounds.Height/2;
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
BackgroundImage = Properties.Resources._1;
System.Threading.Thread.Sleep(500);
pla.Play();
}
}
}
}
I would propose to set image once in Form1_Load and then control any showing and hiding of window using Form.Opacity variable. I have tested the code below and should work as you wanted.
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Opacity = 0; //This line added
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height / 2;
this.BackgroundImage = Properties.Resources._1; //We set the image once here
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
this.Opacity = 1; //We show the window
System.Threading.Thread.Sleep(500);
pla.Play();
this.Opacity = 0; //We hide the window
}
}

How to scroll flow layout panel with button click event

I am adding dyanmic controls(textboxes) in flowlayout panel.I want the controls to scroll when I click on a button(up or down button).The scroll buttons should not be visible.How can I achieve this.Any suggestions?? It is windows application using c#.net
try following code
int yLoc = 0;
private void Button1_Click(object sender, EventArgs e)
{
if (flowPanel.Location.Y <= yLoc && flowPanel.Location.Y >= flowPanel.VerticalScroll.Minimum)
{
yLoc -= 50;
flowPanel.Location = new Point(0, yLoc);
}
}
private void Button2_Click(object sender, EventArgs e)
{
if (flowPanel.Location.Y <= yLoc && flowPanel.Location.Y < flowPanel.VerticalScroll.Maximum)
{
yLoc += 50;
flowPanel.Location = new Point(0, yLoc);
}
}

c# - automatic opacity change in windows for app

I try to make my windows form app to change opacity from 1 to 0 (invisible form), and then back from 0 opacity to 1 (normal, visible form) when i press the button.
Every single opacity changing step is connected with timer. It make opacity change
(-/+0,10) in every timer1_tick.
I start with Opacity = 1 (100%)
Now i have something like that:
public partial class Form1 : Form
{
double OpacityStep;
public Form1()
{
OpacityStep = 0.10;
InitializeComponent();
updateButton1();
updateButton2();
}
private void updateButton1()
{
if (Opacity < 1.00) button1.Enabled = true;
else
{
button1.Enabled = false;
}
}
private void updateButton2()
{
if (Opacity > 0.0) button2.Enabled = true;
else
{
button1.Focus();
button2.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
double NewOpacity = Opacity + OpacityStep;
if (NewOpacity > 1.0) Opacity = 1.0;
else Opacity = NewOpacity;
updateButton2();
updateButton1();
}
private void button2_Click(object sender, EventArgs e)
{
double NewOpacity = Opacity - OpacityStep;
if (NewOpacity < 0.0) Opacity = 0.0;
else Opacity = NewOpacity;
updateButton1();
updateButton2();
}
private void button3_Click(object sender, EventArgs e) // app start
{
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
button2_Click(sender, e);
}
}
This code make my app invisible, but how make it back to visible (opacity = 1) ? ? ?
"I try to make my windows form app to change opacity from 1 to 0 (invisible form), and then back from 0 opacity to 1 (normal, visible form) when i press the button."
Sounds pretty clear to me:
public partial class Form1 : Form
{
double OpacityStep = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
this.Opacity = 1;
OpacityStep = -0.10;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
double NewOpacity = Opacity + OpacityStep;
if (NewOpacity <= 0)
{
NewOpacity = 0;
OpacityStep = 0.10;
}
else if(NewOpacity >= 1 && OpacityStep > 0)
{
NewOpacity = 1.0;
button1.Enabled = true;
timer1.Stop();
}
this.Opacity = NewOpacity;
}
}
EDIT: "If i need to loop this ( opacity from 1 to 0 and back from 0 to 1, in loop), what should i change in this code ?"
With a for loop:
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
// fade out:
this.Opacity = 1.0;
for (double opacity = 1.0; opacity >= 0.0; opacity = opacity - .1)
{
System.Threading.Thread.Sleep(100);
this.Opacity = opacity;
Application.DoEvents();
}
// fade in:
this.Opacity = 0.0;
for (double opacity = 0.0; opacity <= 1.0; opacity = opacity + .1)
{
System.Threading.Thread.Sleep(100);
this.Opacity = opacity;
Application.DoEvents();
}
this.Opacity = 1.0;
button1.Enabled = true;
}

using Label control to create looping marquee text in c# winform

I have been create Marquee text using Label control her is sample code
public partial class FrmMarqueeText : Form
{
private int xPos = 0, YPos = 0;
public FrmMarqueeText()
{
InitializeComponent();
}
private void FrmMarqueeText_Load(object sender, EventArgs e)
{
lblText.Text = "Hello this is marquee text";
xPos = lblText.Location.X;
YPos = lblText.Location.Y;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (xPos == 0)
{
this.lblText.Location = new System.Drawing.Point(this.Width, YPos);
xPos = this.Width;
}
else
{
this.lblText.Location = new System.Drawing.Point(xPos, YPos);
xPos -= 2;
}
}
but when the first time was finished, it didn't continues work .Please help me!
In timer1_Tick change
if (xPos == 0)
to
if (xPos <= 0)
Otherwise it won't work if this.Width is odd.
I think you need to check if xPos <= 0. Because if xPos = 1 after xPos -= 2 your xPos will be equal to -1

Move images in C#

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form.
I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image.
How I can show image smoothly transition to the final location?
Here is my code so far:
public partial class Form1 : Form
{
private int counter = 0;
void timer_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pictureBox1.Show();
timer1.Stop();
counter = 0;
}
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
while(i<=100){
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
timer1.Start();
}
}
}
Does this work? Sorry, I can't test it where I am right now (on netbook without VS).
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}

Categories