Jump to content

C# fade in and out form

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello


I wondering how to do a fade in and out with a form, and also making sure the form is centered of the computer screen?
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Here's a way to make your form fade in tel you move the mouse over it then it's opacity turns to full.
namespace fader1
{
    public partial class Form1 : Form
    {
        Timer timer1 = new Timer();
        Timer timer2 = new Timer();
        int mState = 0;
        public Form1()
        {
            InitializeComponent();
            this.Opacity = 0.10;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 200;
            timer1.Enabled = true;
            timer2.Tick += new EventHandler(timer2_Tick);
            timer2.Interval = 15;
        }

        void timer2_Tick(object sender, EventArgs e)
        {
            // Adjust opacity until end-value is reached
            double op = this.Opacity + mState * 0.03;
            timer2.Enabled = false;
            if (op >= 0.99) op = 0.99;
            else if (op < 0.10) op = 0.10;
            else timer2.Enabled = true;
            this.Opacity = op;
            if (!timer2.Enabled) mState = 0;
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            Point pos = Control.MousePosition;
            bool inForm = pos.X >= Left && pos.Y >= Top && pos.X < Right && pos.Y < Bottom;
            if (inForm && mState <= 0)
            {
                // Fade in
                timer2.Enabled = true;
                mState = 1;
            }
            if (!inForm && mState >= 0)
            {
                // Fade out
                timer2.Enabled = true;
                mState = -1;
            }
        }
    }

}

This will center your form.
//Get the working area of the screen and assign it to a rectangle object   
                Rectangle rect = Screen.PrimaryScreen.WorkingArea;
                //Divide the screen in half, and find the center of the form to center it
                this.Top = (rect.Height / 2) - (this.Height / 2);
                this.Left = (rect.Width / 2) - (this.Width / 2);

Hope I help.:) not sure what you ment by the form fading in and out.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Here's a way to make your form fade in tel you move the mouse over it then it's opacity turns to full.
namespace fader1

{

    public partial class Form1 : Form

    {

        Timer timer1 = new Timer();

        Timer timer2 = new Timer();

        int mState = 0;

        public Form1()

        {

            InitializeComponent();

            this.Opacity = 0.10;

            timer1.Tick += new EventHandler(timer1_Tick);

            timer1.Interval = 200;

            timer1.Enabled = true;

            timer2.Tick += new EventHandler(timer2_Tick);

            timer2.Interval = 15;

        }


        void timer2_Tick(object sender, EventArgs e)

        {

            // Adjust opacity until end-value is reached

            double op = this.Opacity + mState * 0.03;

            timer2.Enabled = false;

            if (op >= 0.99) op = 0.99;

            else if (op < 0.10) op = 0.10;

            else timer2.Enabled = true;

            this.Opacity = op;

            if (!timer2.Enabled) mState = 0;

        }


        void timer1_Tick(object sender, EventArgs e)

        {

            Point pos = Control.MousePosition;

            bool inForm = pos.X >= Left && pos.Y >= Top && pos.X < Right && pos.Y < Bottom;

            if (inForm && mState <= 0)

            {

                // Fade in

                timer2.Enabled = true;

                mState = 1;

            }

            if (!inForm && mState >= 0)

            {

                // Fade out

                timer2.Enabled = true;

                mState = -1;

            }

        }

    }


}

This will center your form.
//Get the working area of the screen and assign it to a rectangle object   

                Rectangle rect = Screen.PrimaryScreen.WorkingArea;

                //Divide the screen in half, and find the center of the form to center it

                this.Top = (rect.Height / 2) - (this.Height / 2);

                this.Left = (rect.Width / 2) - (this.Width / 2);

Hope I help.:) not sure what you ment by the form fading in and out.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#4
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Thanks for the reply thegamemaker, just wanted to ask, how do you have it appear, which it does, but it waits about 10 seconds, then it fades out?

also... the name, thegamemaker, do you really make games? do you use xna etc.? what language do you use? just wondering if the name is what you do, not trying to be a jerk or anything :)
Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#5
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Um I'm not sure you'll just have to mess with the timers and stuff I just googled it for you.:)

Yes I really was making games when I made the account I was using Game Maker 7 and was starting to use XNA but then I decided I'd learn to program before game programming and am now learning C# which I think will be handy if I decide I want to make games.:)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.