I\'m remaking windows Minesweeper (from XP) and something they had included was that if you click a number with as many flags as it\'s number with the left and right mouse b
Create a class boolean variable for the left and right button defaulted to false. When the mouse down event fires set the variable to true and check if both are true. When the mouse up fires set the variable to false.
public bool m_right = false;
public bool m_left = false;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
m_objGraphics.Clear(SystemColors.Control);
if (e.Button == MouseButtons.Left)
m_left = true;
if (e.Button == MouseButtons.Right)
m_right = true;
if (m_left == false || m_right == false) return;
//do something here
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
m_left = false;
if (e.Button == MouseButtons.Right)
m_right = false;
}
Kind of an old question, but I came across this (coincidentally also while doing a Minesweeper clone) and felt it was missing something. If you want to have the two-button click but still catch regular single-button clicks as well, you can do the following:
private bool left_down;
private bool right_down;
private bool both_click;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
left_down = true;
if (right_down)
both_click = true;
}
if (e.Button == MouseButtons.Right)
{
right_down = true;
if (left_down)
both_click = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!right_down)
{
if (both_click)
//Do both-click stuff here
else
//Do left-click stuff here
both_click = false;
}
left_down = false;
}
if (e.Button == MouseButtons.Right)
{
if (!left_down)
{
if (both_click)
//Do both-click stuff here
else
//Do right-click stuff here
both_click = false;
}
right_down = false;
}
}
It moves the detection to the mouse-up rather than the mouse-down. It doesn't do anything until you release both buttons. This works almost exactly like the Windows 7 version of Minesweeper, except that the right button alone in the original operates on mouse-down. (I honestly prefer my version). There's a bit of redundancy in that the both-click case is called in two places depending on whether you release the left or right button first, but this should probably be a single-line function-call anyhow. Bonus: You can check the both_click
flag from elsewhere in order to draw the hint-square around your cursor showing which squares will be revealed when you release the buttons.
Try this,
Private Sub Form_Click(... , ByVal e As ystem.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right And e.Button = MouseButtons.Left Then
MsgBox ('Right & Left code')
End If
I got the following code to work in my Click Event.
if ((Control.MouseButtons == MouseButtons.Right) || (Control.MouseButtons == MouseButtons.Left))
When only one mouse button is being pressed the "Control.MouseButton" assumes the value of "MouseButtons.None"
But when both the left and right mouse buttons are being pressed the "Control.MouseButton" assumes the value of either "MouseButtons.Right" or "MouseButtons.Left" according to which was pressed first / last (depending on how long time it was between the left and right button being pressed)
Complete Code:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftPressed = true;
else if (e.Button == MouseButtons.Right) rightPressed = true;
if (leftPressed && rightPressed)
{
MessageBox.Show("Hello");
// note:
// the following are needed if you show a modal window on mousedown,
// the modal window somehow "eats" the mouseup event,
// hence not triggering the MouseUp event below
leftPressed = false;
rightPressed = false;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftPressed = false;
else if (e.Button == MouseButtons.Right) rightPressed = false;
}
Another option is to use the static MouseButtons on the System.Windows.Forms.Control class
This will tell you which mouse buttons are currently pressed so that you can do something like the following:
((Control.MouseButtons & MouseButtons.Right) == MouseButtons.Right) &&
((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
You can also check out the MSDN example