I want to determine if the mouse cursor is above a Windows Form and if so, change its color, but I'm kind of new to this and C# also. I was thinking of something like this:

 public Form1()
 {
     InitializeComponent();

     if (Control.MousePosition.Y >= 0 && Control.MousePosition.Y <= 499
         && Control.MousePosition.X >= 0 && Control.MousePosition.X <= 297)
            this.BackColor = Control.DefaultForeColor;
 }

but it does not work. Can anyone give me a hand? Thanks.

bir_924 commented: if (!this.Bounds.Contains(this.PointToClient(MousePosition))) out on form +0

Consider the MouseMove or MouseEnter events. More info here.
The MouseEventArgs parameter will give you the X and Y coordinates.

Assuming you have a cursor with a different colour already made, you can simply change the Cursor property of the form to your new cursor. Now every time the cursor is over the form, it will change.

I don't know how should I make the example there work, 'cause it does nothing. The values for X and Y are the same all the time: {X=0,Y=0}

public Form1()
{
    InitializeComponent();

    this.Text += " " + this.Location.ToString();
}

Try something like this:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    this.Text = "X=" + e.X.ToString() + "   " + "Y=" + e.Y.ToString();
}

this.Location is the location of the form.

I managed to do the swicth of background colors and works fine, but then i added two buttons. I want the buttons to be visible or not, also conditioned by the mouse move. When i go with the mouse over the buttons, the colors of the background start changing over and over.

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.BackColor = Color.Black;           
        }

       private void Form2_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;
            this.button1.Visible = true;
            this.button2.Visible = true;
        }

        private void Form2_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = Color.Black;
            this.button1.Visible = false;
            this.button2.Visible = false;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;

        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.MidnightBlue;

        }
    }

With button1(2)_MouseEnter functions I tried to repair the problem, but they do nothing.

It's because you're making the buttons appear and disappear.

When you move your mouse over them, your mouse "leaves" the form window and enters the button window, causing your Form2_MouseLeave method to execute, causing your buttons to become invisible. This causes your mouse to "enter" the form window, causing Form2_MouseEnter to execute. This makes your button reappear, causing your mouse to "leave" the form window...An infinite cycle now starts of the button appearing and disappearing.

I put a panel over the form, and used these functions to do the switch:

private void Form2_MouseLeave(object sender, EventArgs e)
{
    flowLayoutPanel1.Visible = true; 
}

private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e)
{
    flowLayoutPanel1.Visible = false;
}

Works now. :)

if (!sender.Bounds.Contains(sender.PointToClient(MousePosition)))
left form area...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.