【问题标题】:Creating vertical lines inside panel in WinForm在 WinForm 中的面板内创建垂直线
【发布时间】:2021-09-16 09:47:18
【问题描述】:

我正在尝试在我的 winform 应用程序的面板内创建垂直线。我能够画出线条,但它们不是我所期望的。我的要求是:

  1. 必须从面板中间垂直绘制线条。
  2. 必须在两侧以相等的高度绘制线条。

问题是当我尝试绘制从面板顶部绘制的线条时,它们是颠倒的。

我的目标是得到类似的东西:

这就是我尝试的方式:

public void DrawLines(System.Drawing.Graphics g, float height)
{
    Pen thePen = new Pen(Color.Red, 1.0F);            

    PointF[] points1 =
     {
        new PointF(5,5),
        new PointF(5, 50)
     };

    PointF[] points2 =
     {
        new PointF(7,5),
        new PointF(7, 60)
     };

    PointF[] points3 =
     {
        new PointF(9,5),
        new PointF(9, 55)
     };

    //Tried this as well
    //PointF[] points1 =
    // {
    //    new PointF(5,50),
    //    new PointF(5, 5)
    // };

    //PointF[] points2 =
    // {
    //    new PointF(7,60),
    //    new PointF(7, 5)
    // };

    //PointF[] points3 =
    // {
    //    new PointF(9,55),
    //    new PointF(9, 5)
    // };

    g.DrawLines(thePen, points1);
    g.DrawLines(thePen, points2);
    g.DrawLines(thePen, points3);
}

现在我在点击按钮时调用这个函数。

DrawLines(panel1.CreateGraphics(), 20.0F);

这将在循环中实时调用,行高将作为参数传递。

【问题讨论】:

  • 面板中的坐标(一般的 GUI 元素)去 X:从左到右,0 是左,Y:从上到下,0 是顶部
  • ^^ 所以要绘制“从中间到任何地方”的东西,您需要从 panel.height/2 开始,然后“向上”绘制到 panel.height/2 - lineLength 和“向下”绘制到 panel.Heigt/2 + lineLength.
  • ^^ 或者您可以通过从 start = (panel.Height - lineLength)/2 到 end = start + lineLength 绘制来居中。考虑到线条长度也应该尊重面板测量值(否则它们将被剪裁)。因此,您可能希望将值标准化为高度百分比并从那里开始。
  • 您能否将其添加为答案以便我接受?我想我明白你在说什么。
  • 请注意,您可以TranslateTranform() 世界坐标,因此 Y 的增量将 向上 而不是 向下 增加 -- 你永远不会使用height 值,因此不清楚您在做什么或何时绘制这些线(触发新绘图或添加到现有绘图的原因) - 不要使用 Panel 进行绘图,使用 PictureBox 或(非系统)平面标签或双缓冲自定义控件。 -- 不要使用CreateGraphics(),使用您的canvasPaint 事件处理程序提供的Graphics 对象。

标签: c# winforms graphics


【解决方案1】:

如前所述,在面板上绘制时 Y 坐标会翻转。您可以通过简单地显示鼠标坐标来进行测试。

要绘制线条,您需要获取面板的中间并从那里开始工作。在您的示例中,您有一个未使用的高度变量,因此我为其添加了功能。这是一个工作示例。

public MyForm()
{
    InitializeComponent();
    _myPen = new Pen(Color.Red, 1.0F);
}
private Pen _myPen;
private void DrawLines(Graphics g, int width = 0, int height = 0)
{
    // Get the middle of the panel
    int panelMiddle = panel.Height / 2;

    // Lines going up from the mittle
    g.DrawLines(_myPen,
        new PointF[]
        {
            new PointF(width + 5, height + panelMiddle -5),
            new PointF(width + 5, height + panelMiddle - 50) 
        });

    g.DrawLines(_myPen,
       new PointF[]
       {
            new PointF(width + 7, height + panelMiddle-5),
            new PointF(width + 7, height + panelMiddle - 60)
       });

    g.DrawLines(_myPen,
       new PointF[]
       {
            new PointF(width + 9, height + panelMiddle-5),
            new PointF(width + 9, height + panelMiddle - 55)
       });

    // Lines going down from the middle
    g.DrawLines(_myPen,
        new PointF[]
        {
            new PointF(width + 5, height + panelMiddle +5),
            new PointF(width + 5, height + panelMiddle + 50)
        });

    g.DrawLines(_myPen,
       new PointF[]
       {
            new PointF(width + 7, height + panelMiddle+5),
            new PointF(width + 7, height + panelMiddle + 60)
       });

    g.DrawLines(_myPen,
       new PointF[]
       {
            new PointF(width + 9, height + panelMiddle+5),
            new PointF(width + 9, height + panelMiddle + 55)
       });
}

private void panel_Paint(object sender, PaintEventArgs e)
{
    DrawLines(e.Graphics);
    DrawLines(e.Graphics,panel.Width - 14);

}

【讨论】:

  • 这样更好。 如何Invalidate() 画布 部分丢失了,但是,嗯...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2019-08-15
  • 2019-09-21
  • 1970-01-01
  • 2014-01-02
  • 2021-08-27
相关资源
最近更新 更多