private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
int X = e.X;
int Y = e.Y;
if ((X >= pictureBox2.Left && X <= pictureBox2.Left pictureBox2.Width) && (Y >= pictureBox2.Top && Y <= pictureBox2.Top pictureBox2.Height))
{
Text = "Mouse over picturebox";
}
else
{
Text = "Mouse is not over picturebox";
}
}
有時它正在寫入尚未結束的文本,但是當將滑鼠移回 pictureBox2 區域時,它不會更改文本。
mousemove 完整代碼:
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
int X = e.X;
int Y = e.Y;
if ((X >= 0 && X <= pictureBox2.Width) && (Y >= 0 && Y <= pictureBox2.Height))
{
if (e.Button != MouseButtons.Left) return;
if (pictureBox2.Image != null && selectedPath != null)
{
var dr = DrawingRects[DrawingRects.Count - 1];
if (e.Y < dr.StartPosition.Y) { dr.Location = new Point(dr.Rect.Location.X, e.Y); }
if (e.X < dr.StartPosition.X) { dr.Location = new Point(e.X, dr.Rect.Location.Y); }
dr.Size = new Size(Math.Abs(dr.StartPosition.X - e.X), Math.Abs(dr.StartPosition.Y - e.Y));
pictureBox2.Invalidate();
}
}
}
所有滑鼠事件:
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (pictureBox2.Image != null && selectedPath != null)
{
DrawingRects.Add(new DrawingRectangle()
{
Location = e.Location,
Size = Size.Empty,
StartPosition = e.Location,
Owner = (Control)sender,
DrawingcColor = SelectedColor // <= Shape's Border Color
});
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
int X = e.X;
int Y = e.Y;
if ((X >= 0 && X <= pictureBox2.Width) && (Y >= 0 && Y <= pictureBox2.Height))
{
if (e.Button != MouseButtons.Left) return;
if (pictureBox2.Image != null && selectedPath != null)
{
var dr = DrawingRects[DrawingRects.Count - 1];
if (e.Y < dr.StartPosition.Y) { dr.Location = new Point(dr.Rect.Location.X, e.Y); }
if (e.X < dr.StartPosition.X) { dr.Location = new Point(e.X, dr.Rect.Location.Y); }
dr.Size = new Size(Math.Abs(dr.StartPosition.X - e.X), Math.Abs(dr.StartPosition.Y - e.Y));
pictureBox2.Invalidate();
}
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (DrawingRects.Count > 0 && pictureBox2.Image != null && selectedPath != null)
{
// The last drawn shape
var dr = DrawingRects.Last();
if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
{
rectImage = cropAtRect((Bitmap)pictureBox2.Image, dr.Rect);
if (saveRectangles)
{
rectangleName = GetNextName(Path.Combine(selectedPath, "Rectangle"), ".bmp");
FileList.Add($"{dr.Location}, {dr.Size}", rectangleName);
string json = JsonConvert.SerializeObject(
FileList,
Formatting.Indented // this for pretty print
);
using (StreamWriter sw = new StreamWriter(Path.Combine(selectedPath, "rectangles.txt"), false))
{
sw.Write(json);
sw.Close();
}
rectImage.Save(rectangleName);
saveRectanglesCounter ;
}
pixelsCounter = rect.Width * rect.Height;
pictureBox1.Invalidate();
listBox1.DataSource = FileList.ToList();
listBox1.SelectedIndex = listBox1.Items.Count - 1;
pictureBox2.Focus();
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);
}
}
}
uj5u.com熱心網友回復:
e.X和e.Y屬性與引發事件的控制元件相關,而和Left的Top屬性PictureBox與表單相關。如果您打算按照自己的方式做事,那么:
if ((X >= pictureBox2.Left && X <= pictureBox2.Left pictureBox2.Width) && (Y >= pictureBox2.Top && Y <= pictureBox2.Top pictureBox2.Height))
實際上應該是這樣的:
if ((X >= 0 && X <= pictureBox2.Width) && (Y >= 0 && Y <= pictureBox2.Height))
不過你不會那樣做。如果PictureBox提高 aMouseMove那么您可以保證滑鼠指標在 上PictureBox,因此不需要或不值得檢查。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516760.html
標籤:C#表格
