其實我試圖做一個程序,告訴你是否在我的祕密半徑內。 我會設置一個點,例如x = 1000和y = 400,我會設置半徑爲例如50,如果我的光標在距離點50的半徑上,它會寫入控制檯「你在半徑內,x是1000,y是400」。檢查光標是否在一定範圍內
這不是一個家庭作業或類似的東西,我只是在嘗試新的東西。
好吧,所以這裏是我試過的,不能再移動了。
- 注意:半徑應該是圓形,所以我想我需要實現[R和PI但我不太確定如何做到這一點編程:
class Program
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetCursorPos(out System.Drawing.Point point);
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);
static void Main(string[] args){
Point cursorPoint;
Point myPos = Point.Empty;
myPos.X = 1000;
myPos.Y = 400;
int myRadius = 50;
while (true){
//if pressed ESC let me end the program.
if (GetAsyncKeyState(Keys.Escape) > 0)
return;
//get cursor pos and store it in cursorPoint
GetCursorPos(out cursorPoint);
//here should be a check with some calculation about radius, but I can't do that..
Console.WriteLine("You are in radius and your cursor coordinations are X:{0}|Y:{1}",cursorPoint.X,cursorPoint.Y);
//help cpu
Thread.Sleep(16);
}
}
}
感謝您的幫助。
這是一個數學問題。距離= sqrt [(x2-x1)^ 2 +(y2-y1)^ 2] – Szer
這是一個winforms還是wpf項目? – mike
以winforms作爲參考的控制檯應用程序 –