我想像这样设计一个Gui:
我试图用X,-X,Y,-Y轴制作一个图形,但它并不像预期的那样。原点(0,0)应该在中间。我想在我的图表上添加不同位置的对象(A1,A2,A3,A4),如图所示。 T是可移动物体,它应该能够将其位置显示为点T(x,y)。图表比例应以米为单位。
必须添加"危险"距离原点半径15米的范围。必须添加"警告"半径距离原点25米的范围。超过25米将是一个安全区。如何使用Visual c#或代码拖放来实现它。
我对visual c#GUI开发很新。
答案 0 :(得分:0)
我不认为你可以使用WinForms控件(如果那是你计划使用的那些)给你的。 您最好的选择是创建一个自定义控件,您可以完成所有这些操作。
这不容易,但它可能是你唯一的解决方案。
答案 1 :(得分:0)
也许这段代码可以帮助你入门。
创建Windows窗体应用程序并添加名为“RadarForm”的新窗体。在表单中添加两个按钮。单击一个按钮将添加随机光点,单击另一个按钮将清除所有光点。
此代码将表单的原点重置为其中心,并且所有x,y坐标都基于该坐标。
一个按钮会在表单中添加一个随机RadarBlip
,它有50/50的机会成为“敌人”。友好的blips以蓝色绘制,敌人的blip以红色绘制。
对于Windows窗体,所有绘图都应使用OnPaint
覆盖方法完成。
public partial class RadarForm : Form
{
//A List to hold our blips
private List<RadarBlip> blips = new List<RadarBlip>();
//Random number generator
private Random random = new Random();
public RadarForm()
{
InitializeComponent();
//Setting to prevent flickering when redrawing the graphics
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Set the graphics quality to high quality
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//Set the origin of the Cartesian plane to the center of the form
resetOrigin(e.Graphics);
//draw "danger" circle. This would be your 15 meter circle
drawCircle(new Point(0, 0), 200, 3, Color.Red, e.Graphics);
//Draw X and Y Axes
using (var p = new Pen(Brushes.Black, 3.0F))
{
//Draw X axis
e.Graphics.DrawLine(p, new Point(-Width / 2, 0), new Point(Width / 2, 0));
//Draw Y axis
e.Graphics.DrawLine(p, new Point(0, -Height / 2), new Point(0, Height / 2));
}
//Draw all the radar blips in the collection red = enemy blue = friendly.
//Each blip is drawn 10 pixels in size.
foreach (var blip in blips)
{
Brush b = blip.IsEnemy ? Brushes.Red : Brushes.Blue;
e.Graphics.FillEllipse(b, blip.X - 5, blip.Y - 5, 10, 10);
}
}
//Create a random radar blip. 50/50 chance of being an enemy
private RadarBlip createRandomBlip()
{
return new RadarBlip
{
X = random.Next(-Width / 2, Width / 2 + 1),
Y = random.Next(-Height / 2, Height / 2 + 1),
IsEnemy = random.Next(0, 101) > 50
};
}
private void resetOrigin(Graphics g)
{
// Flip the Y-Axis so that positive values are upward on the axis
g.ScaleTransform(1.0F, -1.0F);
// Translate the drawing area accordingly so the origin is in the center of the form
g.TranslateTransform(Width / 2.0F, -Height / 2.0F);
}
private void drawCircle(Point center, int radius, int thickness, Color color, Graphics g)
{
var rect = new Rectangle(center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
using (var p = new Pen(color, thickness))
{
g.DrawEllipse(p, rect);
}
}
private void button1_Click(object sender, EventArgs e)
{
blips.Add(createRandomBlip());
this.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
blips.Clear();
this.Refresh();
}
}
public class RadarBlip
{
public float X { get; set; }
public float Y { get; set; }
public bool IsEnemy { get; set; }
}