c# - automatic opacity change in windows for app - c#

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;
}

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
}
}

Visual studio c# platformer,background issue

I am currently working on a platformer in C#,VS 2015
The problem i have encountered is:when i press the left/right arrow,the background moves behind the player in the opposite direction,and this happens using a timer(which cannot be set to a value lower than 1 milisecond).
Because the timer is limited to 1 milisecond,the background moves laggy,and i would like to solve this :D
Here is the code
public Form1()
{
InitializeComponent();
}
Bitmap back;
Bitmap bobright;
Bitmap bobleft;
Bitmap bobimage;
Bitmap exit;
Point boblocation = Point.Empty;
Point exitlocation = Point.Empty;
float offSet = 0, vit;
int acc_secunde, acc_secunde_max = 30;
bool left = false, right = false;
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Game()
{
int imageNumber = panel1.Width / back.Width + 3;
using (Bitmap frame = new Bitmap(panel1.Width, panel1.Height))
{
using (Graphics graph = Graphics.FromImage(frame))
{
for (int i = 0; i < imageNumber; i++)
{
graph.DrawImage(back, offSet/2 % back.Width + i * back.Width - back.Width, 0);
}
graph.DrawImage(bobimage, boblocation);
graph.DrawImage(exit,exitlocation);
}
using (Graphics graph = panel1.CreateGraphics())
{
graph.DrawImage(frame, Point.Empty);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
back = Properties.Resources.back;
bobleft = Properties.Resources.bobleft;
bobright = Properties.Resources.bobright;
bobimage = bobright;
exit = Properties.Resources.Exit;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
boblocation.X = panel1.Width / 2 - bobimage.Width / 2;
boblocation.Y = 210;
exitlocation.X = panel1.Width - exit.Width - 10;
exitlocation.Y = 5;
if (left)
{
offSet = offSet + acc_secunde;
bobimage = bobleft;
}
if(right)
{
bobimage = bobright;
offSet = offSet - acc_secunde;
}
Game();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Start();
left = true;
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Start();
right = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Stop();
deacceleratie.Start();
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Stop();
deacceleratie.Start();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
private void deacceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde > 0)
acc_secunde = acc_secunde - acc_secunde_max / 3;
if (acc_secunde <= 0)
{
deacceleratie.Stop();
if (right == true)
right = false;
if (left == true)
left = false;
acc_secunde = 0;
}
}
private void acceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde< acc_secunde_max)
acc_secunde+=2;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Point mousePt = new Point(e.X, e.Y);
if (mousePt.X > panel1.Width - exit.Width - 10 && mousePt.X < panel1.Width - 10 && mousePt.Y > panel1.Top + 10 && mousePt.Y < panel1.Top + 10 + exit.Height)
Application.Exit();
}
}
}

How to change the window position in wpf on a mouse click

I need to change the position of a window on mouse click.
here is the code.
private void Button_Click(object sender, RoutedEventArgs e)
{
for(int i=0; i<50; i++)
{
this.Top -= i;
this.Left -= i;
}
}
But whenever i run this program only the last position is shown. My intention is to move it continuosly till the end of loop.
Finally i found the answer myself. It s working perfectly as i expected. I used SynchronizationContext which can post Actions to update controls on UI thread.
public partial class Splash : Window
{
SynchronizationContext sc;
System.Timers.Timer t;
double i=0;
double tempTop;
double angle = 0;
public Splash()
{
InitializeComponent();
sc=SynchronizationContext.Current;
}
private void Move(object sender, MouseEventArgs e)
{
DragMove();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void btnMinim_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
l1.Content = "Helicopter Moving";
if(t!=null)
{
t.Stop();
t.Dispose();
}
//for (double i = 0; i < 1; i += 0.05)
//{
// this.Top -= i;
// this.Left -= i;
// Thread.Sleep(100);
//}
//l1.Content = "Helicopter Stopped";
tempTop = this.Top;
t = new System.Timers.Timer();
t.Interval = 10;
t.Enabled = true;
t.Elapsed += Change;
t.Start();
}
void Change(object sender, EventArgs e)
{
if (i <= 3)
{
sc.Post(o =>
{
this.Top = tempTop * (Math.Cos(Math.PI * angle / 180));
this.Left -= i;
angle = (angle >= 360) ? 0 : ++angle;
i = i + 0.01;
}, null);
}
else
{
t.Stop();
i = i * -1;
}
}
}
}
Try this should work Thread.Sleep will not work for you as its a UI thread. You need timer to make this work
Timer t;
private void Button_Click(object sender, RoutedEventArgs e)
{
i=0;
if(t!=null)
{
t.Stop();
t.Dispose();
}
t = new Timer();
t.Interval = 800;
t.Enabled = true;
t.Tick += T_Tick;
t.Start();
}
int i=0;
private static void T_Tick(object sender, EventArgs e)
{
if(i<=50)
{
this.Top -= i;
this.Left -= i;
i++;
}
else
t.Stop();
}
Just start an animation when your click event triggers. You can define how long should the animation last.
Basic benefit of using animations instead of doing calculations manually is animations being run in separated thread, so you don't loose application's responsiveness.
What is more, you can edit your animations in separated tools, such as Blend without the need to verifying animations in runtime.
Few sources:
http://www.wpf-tutorial.com/styles/trigger-animations-enteractions-exitactions/
http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx
http://www.wpftutorial.net/Animation.html

How to Sync Progressbar with ListBox Items Removal c#

I've a project in which i got a listBox1 a timer1 a button1 and a progressBar1.
When i click button1 the timer1 starts.
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
When timer1 ticks it removes one item from listBox1 and progressBar1 must show the progress of Removal.
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
}
if (listBox1.Items.Count > 0)
{
timer1.Enabled = true;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
}
But i think the above code got some bug with progressBar1 as it dont show progress while removal of items and it is full when the listBox1 items = 0.
There is a better way than this, So set the step property like this in click event :
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = this.listBox1.Items.Count;
progressBar1.Step = 1;
Now you can do the following:
void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.PerformStep();
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
this.timer1.Enabled = false;
}
}
Complete Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PopulateList();
}
private void PopulateList()
{
for (int i = 0; i < 10; i++)
{
listBox1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.PerformStep();
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Tick += timer1_Tick;
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Enabled = true;
progressBar1.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Step = 1;
}
}
You are setting progressbar value to 0 after incrementing it...
Try this:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
timer1.Enabled = true;
}
}

Animate WinForms

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;
}
}
}

Categories