Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

winforms - C# Drawing in a Panel

I want to draw in a panel with this method:

protected override void InitOutput(object output)
        {
            if (output is Control)
            {
                Control c = (Control)output;
                g.FillRectangle(hb, 7, 10, 30 - 19, 5);
                ...
            }

With a text I can do this:

protected override void InitOutput(object output)
        {
            if (output is Control)
            {
                Control c = (Control)output;
                lbl.Name = "lbl";
                lbl.Size = new System.Drawing.Size(10, 10);
                lbl.TabIndex = 5;
                lbl.Text = "test";

                panel.Location = new System.Drawing.Point(1, 1);
                panel.Name = "panelSys";
                panel.Size = new System.Drawing.Size(20, 20);
                panel.TabIndex = 5;
                panel.Controls.Add(lbl);
                c.Controls.Add(panelSys);
            }

Hope you can help me thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure why do you need InitOtuput function but if you want to draw from it you could do it like this:

private void InitOutput(object output)
{
    if (output is Control)
    {
        Control c = (Control)output;
        c.Paint += new System.Windows.Forms.PaintEventHandler(c_Paint);
        // Invalidate needed to rise paint event
        c.Invalidate();
    }
}
private void c_Paint(object sender, PaintEventArgs e)
{
    SolidBrush hb =  new SolidBrush(Color.Red);
    e.Graphics.FillRectangle(hb, 7, 10, 30 - 19, 5);
    e.Graphics.DrawString("test", DefaultFont, hb, new PointF(50, 50));
}

Additionaly you don't need to use label to draw text u can draw it using Graphics.DrawSting


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...