myaspnet666
myaspnet666
采纳率0%
2017-04-25 02:03

C#winform画图怎么解决闪烁问题,也就是闪屏

30

图片说明 public partial class DcChannelPanelItem : UserControl
{
public event Action ClickBodyEvent;

    protected SolidBrush FntBrush = new SolidBrush(Color.Blue);
    protected Font dataFont = new Font("宋体", 10);
    protected SolidBrush hdBrush = new SolidBrush(Color.LightBlue);
    protected Font hdFont = new Font("宋体", 12, FontStyle.Bold);
    DcChannelItem dcChannelItem = new DcChannelItem();
    public DcChannelItem DcChannelItem
    {
        set { dcChannelItem = value; }
        get { return dcChannelItem; }
    }
    public string IpAddress { get; set; }
    public int UnitId { get; set; }
    public int ChId { get; set; }
    public int Index { get; set; }

    bool _selected = false;

    public bool Selected
    {
        get { return _selected; }
        set
        {
            if (value != _selected)
            {
                _selected = value;
                this.Invalidate();
            }
        }
    }

    public DcChannelPanelItem(string ipaddrr,int unitid,int chid)
    {
        IpAddress = ipaddrr;
        UnitId = unitid;
        ChId = chid;
        InitializeComponent();
    }

    public override string ToString()
    {
        return string.Format("{0}-{1}",UnitId,ChId);
    }

    void DrawRectangle(System.Drawing.Graphics g)
    {
        if(_selected)
        {
            Pen p = new Pen(Color.RoyalBlue, 3);
            g.DrawRectangle(p, 0, 0, this.Width - 3, this.Height - 3);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //base.OnPaint(e);
        System.Drawing.Graphics g = e.Graphics;
        DrawRectangle(g);

        // Text.
        StringFormat drawFormat = new StringFormat();

        drawFormat.LineAlignment = StringAlignment.Center;
        drawFormat.Alignment = StringAlignment.Center;
        //11 10 90 26
        g.DrawString(this.ToString(), hdFont, hdBrush, new RectangleF(0, 5, this.Width, 12), drawFormat);

        if (dcChannelItem != null)
        {
            // Set format of string.
            drawFormat = new StringFormat();
            drawFormat.LineAlignment = StringAlignment.Center;
            drawFormat.Alignment = StringAlignment.Far;
            //数据区
            //10, 83 -75, 18
            g.DrawString(string.Format("{0}", dcChannelItem.voltager), dataFont, FntBrush, new RectangleF(13, 36, 50, 14), drawFormat);
            g.DrawString(string.Format("{0}", dcChannelItem.current),  dataFont, FntBrush, new RectangleF(13, 52, 50, 14), drawFormat);
            g.DrawString(string.Format("{0}", dcChannelItem.cap),      dataFont, FntBrush, new RectangleF(13, 66, 50, 14), drawFormat);

            drawFormat = new StringFormat();
            drawFormat.LineAlignment = StringAlignment.Center;
            drawFormat.Alignment = StringAlignment.Near;
            //单位
            g.DrawString(string.Format("{0}", "v"),  dataFont, FntBrush, new RectangleF(65, 36, 18, 14), drawFormat);
            g.DrawString(string.Format("{0}", "A"), dataFont, FntBrush, new RectangleF(65, 52, 18, 14), drawFormat);
            g.DrawString(string.Format("{0}", "Ah"), dataFont, FntBrush, new RectangleF(65, 66, 18, 14), drawFormat);
        }
        else
        {
            drawFormat = new StringFormat();
            drawFormat.LineAlignment = StringAlignment.Center;
            drawFormat.Alignment = StringAlignment.Near;

            //单位
            //88, 83 - 28, 18
            //g.DrawString(string.Format("{0}", AppConfigration.Instance.DataItem._itemSelectUnit[0]), myFont, FntBrush, new RectangleF(13, 26, 25, 14), drawFormat);
            //g.DrawString(string.Format("{0}", AppConfigration.Instance.DataItem._itemSelectUnit[1]), myFont, FntBrush, new RectangleF(13, 42, 25, 14), drawFormat);
            //g.DrawString(string.Format("{0}", AppConfigration.Instance.DataItem._itemSelectUnit[2]), myFont, FntBrush, new RectangleF(13, 44, 56, 14), drawFormat);
        }
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

相关推荐