所以我做了很多研究,发现solution。几乎我发现的每个帖子都会导致这个链接。我直接在帖子的评论中问过,但似乎没有人对此感兴趣,所以我来到这里。
解决方案工作正常,但不是我希望它工作的方式。当Form加载时,它会在控件的顶部绘制我的线条,但之后它不再执行任何操作。我想尽可能多地刷新它。
我已经从上面的Code Project链接中复制了代码,并根据我的需要稍微改了一下,所以这就是我到目前为止所做的:(作为绘制我的线条的函数)
private void graphicalOverlay_Paint(object sender, PaintEventArgs e)
{
Pen myPen;
myPen = new Pen(Color.Red, 10);
Graphics formGraphics = this.CreateGraphics();
formGraphics.Clear(Color.White);
formGraphics.DrawLine(myPen, Arm_1.PointA.X, Arm_1.PointA.Y, Arm_1.PointB.X, Arm_1.PointB.Y);
myPen.Color = Color.Blue;
formGraphics.DrawLine(myPen, Arm_2.PointA.X, Arm_2.PointA.Y, Arm_2.PointB.X, Arm_2.PointB.Y);
Rectangle rect = this.ClientRectangle;
rect.Inflate(-10, -10);
Pen pen;
pen = new Pen(Color.Red, 5);
e.Graphics.DrawLine(pen, Arm_1.PointA.X, Arm_1.PointA.Y, Arm_1.PointB.X, Arm_1.PointB.Y);
pen = new Pen(Color.Blue, 5);
e.Graphics.DrawLine(pen, Arm_2.PointA.X, Arm_2.PointA.Y, Arm_2.PointB.X, Arm_2.PointB.Y);
pen.Dispose();
e.Dispose();
myPen.Dispose();
formGraphics.Dispose();
}
这是graphOverlay控件(直接从Code Project复制)
namespace CodeProject
{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public partial class GraphicalOverlay : Component
{
public event EventHandler<PaintEventArgs> Paint;
private Form form;
public GraphicalOverlay()
{
InitializeComponent();
}
public GraphicalOverlay(IContainer container)
{
container.Add(this);
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Form Owner
{
get { return form; }
set
{
// The owner form cannot be set to null.
if (value == null)
throw new ArgumentNullException();
// The owner form can only be set once.
if (form != null)
throw new InvalidOperationException();
// Save the form for future reference.
form = value;
// Handle the form's Resize event.
form.Resize += new EventHandler(Form_Resize);
// Handle the Paint event for each of the controls in the form's hierarchy.
ConnectPaintEventHandlers(form);
}
}
private void Form_Resize(object sender, EventArgs e)
{
form.Invalidate(true);
}
private void ConnectPaintEventHandlers(Control control)
{
// Connect the paint event handler for this control.
// Remove the existing handler first (if one exists) and replace it.
control.Paint -= new PaintEventHandler(Control_Paint);
control.Paint += new PaintEventHandler(Control_Paint);
control.ControlAdded -= new ControlEventHandler(Control_ControlAdded);
control.ControlAdded += new ControlEventHandler(Control_ControlAdded);
// Recurse the hierarchy.
foreach (Control child in control.Controls)
ConnectPaintEventHandlers(child);
}
private void Control_ControlAdded(object sender, ControlEventArgs e)
{
// Connect the paint event handler for the new control.
ConnectPaintEventHandlers(e.Control);
}
private void Control_Paint(object sender, PaintEventArgs e)
{
// As each control on the form is repainted, this handler is called.
Control control = sender as Control;
Point location;
// Determine the location of the control's client area relative to the form's client area.
if (control == form)
// The form's client area is already form-relative.
location = control.Location;
else
{
// The control may be in a hierarchy, so convert to screen coordinates and then back to form coordinates.
location = form.PointToClient(control.Parent.PointToScreen(control.Location));
// If the control has a border shift the location of the control's client area.
location += new Size((control.Width - control.ClientSize.Width) / 2, (control.Height - control.ClientSize.Height) / 2);
}
// Translate the location so that we can use form-relative coordinates to draw on the control.
if (control != form)
e.Graphics.TranslateTransform(-location.X, -location.Y);
// Fire a paint event.
OnPaint(sender, e);
}
private void OnPaint(object sender, PaintEventArgs e)
{
// Fire a paint event.
// The paint event will be handled in Form1.graphicalOverlay1_Paint().
if (Paint != null)
Paint(sender, e);
}
}
}
namespace System.Windows.Forms
{
using System.Drawing;
public static class Extensions
{
public static Rectangle Coordinates(this Control control)
{
// Extend System.Windows.Forms.Control to have a Coordinates property.
// The Coordinates property contains the control's form-relative location.
Rectangle coordinates;
Form form = (Form)control.TopLevelControl;
if (control == form)
coordinates = form.ClientRectangle;
else
coordinates = form.RectangleToClient(control.Parent.RectangleToScreen(control.Bounds));
return coordinates;
}
}
}
所以我的问题是在启动程序时只调用一次“graphicalOverlay_Paint”函数。有人知道怎么称呼它吗?
我希望我一直都是具体的。这是我在这个论坛上的第一个问题,告诉我,如果我做错了什么。另外,英语不是我的母亲,对不起的错误。 我希望有一个人可以帮助我。非常感谢阅读,并度过了美好的一天!
二万
编辑1:代码项目链接中RocketScientist的所有信用。