-1
所以我一直在自學c#教程和下面的教程。直到這個時候沒有什麼大問題。 Bun現在我不能自己找到解決方案。試圖在點擊上畫矩形
所以我列出了一個簡單的表單,它應該可以在不同的鼠標點擊上創建矩形和吸引點。但是當我運行代碼時,沒有任何反應。
我無法理解什麼是錯的..請幫助(=
public partial class Form1 : Form
{
float mRectSize = 50;
List<RectangleF> mRectangles = new List<RectangleF>();
Random rnd = new Random();
PointF mForcePoint = new PointF(-1, -1);
bool mForcePush = false;
int mRandomSeed = 10;
public Form1()
{
InitializeComponent();
Timer timer1 = new Timer();
timer1.Interval = 1;
timer1.Tick += Timer1_Tick;
timer1.Start();
}
private void AddRectangle(PointF pos)
{
mRectangles.Add(new RectangleF(pos.X - mRectSize/2, pos.Y - mRectSize/2, mRectSize, mRectSize));
}
private void AddForcePoint(PointF pos)
{
mForcePoint = pos;
}
private void DrawRectangle(Graphics g, RectangleF rect)
{
Color c = Color.FromArgb(255, rnd.Next(255), rnd.Next(255), rnd.Next(255));
g.FillRectangle(new SolidBrush(c), rect);
}
private PointF RandomiseDirection(PointF dir)
{
dir.X += rnd.Next(2 * mRandomSeed) - mRandomSeed;
dir.Y += rnd.Next(2 * mRandomSeed) - mRandomSeed;
return dir;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
foreach (RectangleF rect in mRectangles)
{
DrawRectangle(g, rect);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
AddRectangle(e.Location);
//MessageBox.Show("test");
Invalidate();
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
AddForcePoint(e.Location);
}
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
mForcePush = !mForcePush;
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (mForcePoint.X != -1 && mForcePoint.Y != -1)
{
for (int i = 0; i < mRectangles.Count; i++)
{
RectangleF rect = mRectangles[i];
PointF direction = new PointF(mForcePoint.X - rect.Location.X, mForcePoint.Y - rect.Location.Y);
direction = RandomiseDirection(direction);
if (mForcePush)
{
rect.Location = new PointF(rect.Location.X - direction.X * 0.1f, rect.Location.Y - direction.Y * 0.1f);
}
else
{
rect.Location = new PointF(rect.Location.X + direction.X * 0.1f, rect.Location.Y + direction.Y * 0.1f);
}
mRectangles[i] = rect;
}
Invalidate();
}
}
}
}
這似乎是學習調試的好時機。嘗試設置一些斷點,看看發生了什麼。 – Blorgbeard
mForcePoint是否被更改過? –
它假設改按人民幣..但還是一無所獲。 –