-1

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();
            }
        }
    }
}
Guy Levy
  • 1,439
  • 2
  • 14
  • 26
Nikolai
  • 33
  • 4
  • Have you tried `BackgroundImage = null;`? – Vilx- Jan 02 '18 at 20:14
  • 1
    After you have played the sound and waited, set the background image back to Null? – Stefan H Jan 02 '18 at 20:14
  • i'd tried already, but if i did that, then picture not appearing at all. – Nikolai Jan 02 '18 at 20:16
  • you might want to try to minimize your window by setting `FormWindowState.Minimized` see https://stackoverflow.com/questions/6317033/how-do-i-minimize-a-winforms-application-to-the-notification-area – ironstone13 Jan 02 '18 at 20:21
  • 1
    @Nikolai Are you setting the background image back to null after your thread.sleep? – Stefan H Jan 02 '18 at 20:24
  • @Stefan H Thanks for reply, i have tried to set background back to null, but then picture not appearing at all – Nikolai Jan 02 '18 at 20:47
  • @ironstone13 Thanks for reply, but this also not working, because window get minimized immediately , even if i do System.Threading.Thread.Sleep(2000); this.WindowState = FormWindowState.Minimized; – Nikolai Jan 02 '18 at 20:52
  • 1
    You realize that none of us know what "Scary Screamer" is and therefore putting it in your question title adds no value? – mason Jan 02 '18 at 22:01
  • Noted, next time i will be more accurate with titles. – Nikolai Jan 02 '18 at 23:39

1 Answers1

1

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
        }
    }
pajamac
  • 113
  • 12