I want to make 4 buttons display around pointer when it delays 3s. If I active by hovering one of 4 buttons, all buttons will be hidden.
Like this:
I'll assume you're using WinForms.
A problem is that your picture actually displayed not rectangular buttons. This is another difficulty, but I want to restrict it to your problem. Therefore you'll need to be able to do a few things.
Let's do this step by step:
1. Getting the mouse position
Therefore you'll have to handle the MouseMove
event. Therefore you select your form, go to properties (press F4), select the events tab (the lightning bolt icon) and double click into the field right from MouseMove. This will create the Form1_MouseMove
method in your code behind. In this method you can get the Mouse position with e.Location;
.
Our method till here looks like that:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var location = e.Location;
}
2. Getting a method that is called when 3 seconds without mouse movement passed
Therefore I'd like to use a timer. To do this you switch to the designer again (press F7), open the toolbox (CTRL + ALT + X) and drag the Timer item into your form. Select the newly created timer (It's visible under your form), switch to the properties tab again, select the properties (the icon left from the lightning bolt), double click on the field right from Enabled to set it to true, switch to the events tab again and double click onto the field right from Tick. The method there will be called every 100 milliseconds. Then you'll add a field called something like _timePassedSinceLastMove. You'll increase this everytime the tick method is called and set it to 0 again when Form1_MouseMove
is called. In the timer1_Tick
method you'll check wether the _timePassedSinceLastMove field already is over 30 and if so, you'll call the method that shall get called every 3 seconds. Actually we still have to check wether your mouse position really differs from the old one, because winForms is a little bit hyper sensitive in that case. Therefore we add a field with the old location and compare it to the new one. Your class should look something like that now:
public partial class Form1 : Form
{
private Point _oldMousePosition;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var location = e.Location;
if (_oldMousePosition == location)
return;
_oldMousePosition = location;
_timePassedSinceLastMove = 0;
}
private int _timePassedSinceLastMove;
private void timer1_Tick(object sender, EventArgs e)
{
_timePassedSinceLastMove++;
if (_timePassedSinceLastMove > 30)
{
MouseNotMove3Seconds();
_timePassedSinceLastMove = 0;
}
}
private void MouseNotMove3Seconds()
{
}
}
3. Dynamically creating buttons
I'd call this task the easiest of all. I think it's best explained on an example:
var button = new Button();
Controls.Add(button);
We simply create a new button and add this to the controls of our forms (this is executed in the code behind as well). If we want to add 4 buttons whenever the mouse didn't move we'll simply do it like that:
private void MouseNotMove3Seconds()
{
var button1 = new Button();
Controls.Add(button1);
var button2 = new Button();
Controls.Add(button2);
var button3 = new Button();
Controls.Add(button3);
var button4 = new Button();
Controls.Add(button4);
}
4. Setting the position of the buttons
Setting the position of a button isn't too difficult either:
var button1 = new Button();
button1.Location = new Point(0, 0);
We could write this more beautiful as well with an object initializer:
var button1 = new Button {Location = new Point(0, 0)};
But now we don't want all buttons at the location 0,0 but around our cursor. So we'll do something like that (positions are always of the left upper corner):
private void MouseNotMove3Seconds()
{
var button1 = new Button {Location = new Point(_oldMousePosition.X + Cursor.Size.Width, _oldMousePosition.Y)};
Controls.Add(button1);
var button2 = new Button {Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y + Cursor.Size.Height)}; //I cheated a little bit here, but as the buttons are all the same...
Controls.Add(button2);
var button3 = new Button {Location = new Point(_oldMousePosition.X - (int)(button1.Width * 1.5), _oldMousePosition.Y)};
Controls.Add(button3);
var button4 = new Button {Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y - (int)(button1.Height * 1.5))};
Controls.Add(button4);
}
You now said that you want to delete the buttons again when hovered. So we'll need to create a method ButtonHovered
:
private void ButtonHovered(object sender, EventArgs e)
{
Controls.Clear();
}
Now we're simply deleting all the components (buttons) we added to the form. Of course, if you have more components in your form, you'll have to just delete those four buttons from the components and leave the rest in.
This method isn't called now though, because we didn't handle the Hover events of the buttons. Therefore we'll have to modify our MouseNotMoved3Seconds
method a little bit:
private void MouseNotMove3Seconds()
{
var button1 = new Button {Location = new Point(_oldMousePosition.X + Cursor.Size.Width, _oldMousePosition.Y)};
Controls.Add(button1);
var button2 = new Button {Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y + Cursor.Size.Height)}; //I cheated a little bit here, but as the buttons are all the same...
Controls.Add(button2);
var button3 = new Button {Location = new Point(_oldMousePosition.X - (int)(button1.Width * 1.5), _oldMousePosition.Y)};
Controls.Add(button3);
var button4 = new Button {Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y - (int)(button1.Height * 1.5))};
Controls.Add(button4);
button1.MouseHover += ButtonHovered;
button2.MouseHover += ButtonHovered;
button3.MouseHover += ButtonHovered;
button4.MouseHover += ButtonHovered;
}
Now we have the events attached to the ButtonHovered method, which will be called whenever the user hovers over one of the buttons.
If you want to know which button caused the buttons to disappear, the button that caused the event to fire is given to the ButtonHovered
method through the sender
property.
In your picture your buttons also have text written on them, so let's do that as well. Therefore we'll change the Text
property of the buttons in the object initializer in the MouseNotMove3Seconds
method:
private void MouseNotMove3Seconds()
{
var button1 = new Button
{
Location = new Point(_oldMousePosition.X + Cursor.Size.Width, _oldMousePosition.Y),
Text = "B"
};
Controls.Add(button1);
var button2 = new Button
{
Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y + Cursor.Size.Height),//I cheated a little bit here, but as the buttons are all the same...
Text = "C"
};
Controls.Add(button2);
var button3 = new Button
{
Location = new Point(_oldMousePosition.X - (int)(button1.Width * 1.5), _oldMousePosition.Y),
Text = "D"
};
Controls.Add(button3);
var button4 = new Button
{
Location = new Point(_oldMousePosition.X - button1.Width / 2, _oldMousePosition.Y - (int)(button1.Height * 1.5)),
Text = "A"
};
Controls.Add(button4);
button1.MouseHover += ButtonHovered;
button2.MouseHover += ButtonHovered;
button3.MouseHover += ButtonHovered;
button4.MouseHover += ButtonHovered;
}
While this solution doesn't look like your picture (That's because the differently shaped buttons), it should help you with your problem. If your problem were the differently shaped buttons, ask a new question.
I hope, I could help you.