注意:在Visual Studio 2017中使用c#Windows窗体
下面的代码现在对将来使用是正确的。
我正在尝试开发代码,以帮助我检测鼠标每0.5秒在哪个象限中。我首先尝试使用0,但是我发现c#中的鼠标位置没有负值。所以我以为我会使用我计算出的宽度和高度:
public void setScreenSize()
{
int W = Screen.PrimaryScreen.Bounds.Width;
int H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
完整代码在这里:
public partial class Recording : Form
{
private System.Timers.Timer mousePositionTimer;
int W, H;
public Recording()
{
InitializeComponent();
}
private void Recording_Load(object sender, EventArgs e)
{
setScreenSize();
SetTimer(mousePositionTimer, 250,OnTimedMousePositionEvent);
}
public void setScreenSize()
{
W = Screen.PrimaryScreen.Bounds.Width;
H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
private void SetTimer(System.Timers.Timer t, int millis, ElapsedEventHandler f)
{
t = new System.Timers.Timer(millis);
t.Elapsed += f;
t.AutoReset = true;
t.Enabled = true;
}
private void OnTimedMousePositionEvent(Object source, ElapsedEventArgs e)
{
record.addMousePosition(W,H,MousePosition.X,MousePosition.Y);
}
}
然后,我使用以下方法检测象限:
//getting position of mouse in a given quadrant
public static class record
{
public static void addMousePosition(int w, int h, int x, int y)
{
if (x > w/2 && y > h/2)
mousePositions.Add(1);
else if (x > w/2 && y <= h/2)
mousePositions.Add(2);
else if (x <= w/2 && y <= h/2)
mousePositions.Add(3);
else if (x <= w/2 && y > h/2)
mousePositions.Add(4);
}
}
但是,当我打印列表时,总是得到象限1。有没有更好的方法来获取屏幕中心位置,以便我可以正确地处理自己的条件?
请记住,我正在尝试在表格外执行此操作,而不一定在表格上执行。
答案 0 :(得分:1)
范围问题?
public void setScreenSize()
{
int W = Screen.PrimaryScreen.Bounds.Width;
int H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
到
public void setScreenSize()
{
W = Screen.PrimaryScreen.Bounds.Width;
H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
答案 1 :(得分:0)
我将创建4个Rectangle
,然后使用Rectangle.Contains
检查鼠标在哪里:
Rectangle topLeft, topRight, bottomLeft, bottomRight;
void CalculateQuadrants(Form mainForm)
{
Screen myScreen = Screen.FromControl(mainForm);
Rectangle area = myScreen.WorkingArea;
topLeft = new Rectangle(area.Left, area.Top, area.Right/2, area.Bottom/2);
topRight = new Rectangle(area.Right/2, area.Top, area.Right/2, area.Bottom/2);
bottomLeft = new Rectangle(area.Left, area.Bottom/2, area.Right/2, area.Bottom/2);
bottomRight = new Rectangle(area.Right/2, area.Bottom/2, area.Right/2, area.Bottom/2);
}
然后您可以做一个简单的。包含检查鼠标指针:
public static void addMousePosition(Point mousePosition)
{
if (topLeft.Contains(mousePosition))
mousePositions.Add(1);
else if (topRight.Contains(mousePosition))
mousePositions.Add(2);
else if (bottomLeft.Contains(mousePosition))
mousePositions.Add(3);
else if (bottomRight.Contains(mousePosition))
mousePositions.Add(4);
}
答案 2 :(得分:0)
总结我的意见:
从setScreenSize()
调用OnFormLoad()
时,您将只设置在方法中定义的局部变量W
和H
。
public void setScreenSize()
{
int W = Screen.PrimaryScreen.Bounds.Width;
int H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
因此这些变量将隐藏您在
中声明的字段public partial class Recording : Form
{
private System.Timers.Timer mousePositionTimer;
int W, H;
...
}
因此,由于未初始化类字段,因此它们具有default(int)
值0
。
现在致电addMousePosition()
时,将始终使用默认的0
而不是您存储在本地int H
和int W
中的实际值
private void OnTimedMousePositionEvent(Object source, ElapsedEventArgs e)
{
record.addMousePosition(W,H,MousePosition.X,MousePosition.Y);
}
public static void addMousePosition(int w, int h, int x, int y)
{
if (x > w/2 && y > h/2)
mousePositions.Add(1);
else if (x > w/2 && y <= h/2)
mousePositions.Add(2);
else if (x <= w/2 && y <= h/2)
mousePositions.Add(3);
else if (x <= w/2 && y > h/2)
mousePositions.Add(4);
}
,因此您的代码通常会Add(1)
。仅应在屏幕的左上角进入Add(3)
分支。
因此,删除本地int
和W
前面的H
将解决此问题。
public void setScreenSize()
{
W = Screen.PrimaryScreen.Bounds.Width;
H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + H + "width is: " + W);
}
为澄清起见,我建议使用this.
:
public void setScreenSize()
{
this.W = Screen.PrimaryScreen.Bounds.Width;
this.H = Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine("Height is: " + this.H + "width is: " + this.W);
}
站点节点:
糟糕,您说得对!我想知道为什么它没有抱怨
我也认为VS2017会对此有所提示,但事实并非如此。 Resharper提供了一个提示:
局部变量
H
隐藏字段int Recording.H