在c#中绘制矩形

时间:2017-06-27 20:51:25

标签: asp.net webforms

我需要绘制一组矩形,类似于由28-31个矩形组成的日历,但是基于它们的坐标和宽度/高度(来自数据库表)。我需要能够在每个矩形上添加/绘制,例如在原始矩形的顶部添加较小的正方形加上一些文本。我的问题是我应该用什么来实现这个目标?

1 个答案:

答案 0 :(得分:0)

private void DoSomething()
{
    //these values are just example, should be from a source
    int x=15, y=20, width=100, height=80;
    string bgColor = "#fafafa";

    Panel pnOuter = new Panel();
    pnOuter.Attributes.Add("style", "position:absolute; text-align:center; left:" + x + "px; top:" + y + "px; width:" + width + "px; height:" + height + "px; background-color:" + bgColor + ";");
    Label lbHeader = new Label();
    lbHeader.Text = "Some Header";
    lbHeader.Attributes["class"] = "Header";
    pnOuter.Controls.Add(lbHeader);

    Panel pnInner = new Panel();
    pnInner.Attributes.Add("style", "margin-left:auto; margin-right:auto; width:50px; height:40px; text-align:center;");
    Label lbText = new Label();
    lbText.Text = "some text";
    lbText.Attributes["class"] = "SomeStyle";
    pnInner.Controls.Add(lbText);

    //add inner panel to outer panel
    pnOuter.Controls.Add(pnInner);
}
相关问题