【问题标题】:Printing a String at a specific Coordinate in c#在 C# 中的特定坐标处打印字符串
【发布时间】:2013-02-09 11:45:07
【问题描述】:

我在这里有一个小事情,我必须创建一个程序,在特定坐标处打印出一个字符串。所以,我知道我需要 2 个用于字符串位置的变量和一个用于字符串的字符串变量,但是我不知道如何继续。有什么想法吗?

编辑 这就是我的进步:

class ColoredText
{
    public int x, y; // koordinaterna
    public string hello;
    ConsoleColor färg;

    public ColoredText(int x, int y, string Position)
    {

    }
}

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?有代码吗?
  • 这将完全取决于您所说的“在特定坐标处打印字符串”的含义。在网页上?在控制台上?在 Windows 窗体上?在报纸上>

标签: c# string position


【解决方案1】:

试试这个:http://msdn.microsoft.com/en-us/library/76c5db29.aspx

public void DrawStringPointF(PaintEventArgs e)
{

    // Create string to draw.
    String drawString = "Sample Text";

    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // Create point for upper-left corner of drawing.
    PointF drawPoint = new PointF(150.0F, 150.0F);

    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
}

【讨论】:

    【解决方案2】:

    好吧,如果您要在 FORM 上的特定坐标处打印字符串,那么您可以执行以下操作...

    创建一个新标签并将其放在您的表单上,然后执行类似...

    现在您可以通过执行以下操作来设置标签坐标..

            int xCoordinate = 0;
            int yCoordinate = 0;
            string labelText = "Enter your text here";
    
            label1.Location = new Point(xCoordinate, yCoordinate);
            label1.Text = labelText;
    

    【讨论】:

      【解决方案3】:

      基于this

      class ColoredText
      {
        public int x, y; // koordinaterna
        public string hello;
        ConsoleColor farg;  // removed the utf-8 char
      
          public ColoredText(int x, int y, string Position)
          {
             Console.ForegroundColor = farg;
             Console.SetCursorPostion(x,y);
             Console.Write(Position);
             Console.ResetColor();
          }
      }
      

      【讨论】:

        【解决方案4】:

        它被标记为 C#。所以我会选择我所知道的。在 WPF 中,您可能想要使用 Canvas 或 Grid。
        使用 Grid 您想设置子元素的边距。
        使用 Canvas,您想设置 Top 和 Left 属性。

        // In XAML
        <Grid>
            <TextBlock Name="TB"/>
        </Grid>
        
        // In code-behind:
        Point p = new Point(x, y);
        TB.Margin = new Thickness(p.x, p.y, 0, 0);
        TB.Text = "Lorem ipsum";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多