首页 > 编程语言 >C# UI控件输出日志

C# UI控件输出日志

时间:2022-11-14 10:47:28浏览次数:48  
标签:控件 C# text void Color UI Items public

实现代码:

public partial class ui_log : ListBox
    {
        public ui_log()
        {
            InitializeComponent();

            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.BackColor = Color.Black;
            this.Font = new Font("黑体", 12);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (e.Index >= 0 && this.Items.Count>0)
            {
                dynamic item = this.Items[e.Index];
                Brush brush = new SolidBrush(item.Color);
                e.Graphics.DrawString(item.Text, e.Font, brush, e.Bounds, StringFormat.GenericDefault);
            }
        }

        public  void Log(string text, Color color)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => { Log(text, color); }));
                return;
            }
            this.Items.Add(new { Color = color, Text = text });
            this.SelectedIndex = this.Items.Count - 1;
        }
        public void LogInfo(string text)
        {
            Log(text, Color.Green);
        }
        public void LogError(string text)
        {
            Log(text, Color.Red);
        }
        public void LogWarinig(string text)
        {
            Log(text, Color.Yellow);
        }

        public void ClearLog()
        {
            this.Items.Clear();
        }
    }

  

 private void button1_Click(object sender, EventArgs e)
        {
            ui_log1.LogInfo("Info");
            Thread.Sleep(300);
            ui_log1.LogError("Error");
            Thread.Sleep(300);
            ui_log1.LogWarinig("Warinig");
            Thread.Sleep(300);
            ui_log1.Log("White", Color.White);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ui_log1.ClearLog();
        }

  

代码解析:首先是写了一个自定义控件,继承自ListBox;然后设置下DrawMode属性,这个很重要,否则不会触发DrawItem;最后在DrawItem事件中,对数据进行重绘。

做完上述处理后,就不要直接使用Items.Add了,需要对Items.Add也进行一次封装,将颜色也传进去,即:this.Items.Add(new { Color = color, Text = text });

使用方法:

如果将以下自定义控件放到控件库中(即在新建项目的时候选择Windows窗体控件库),在其他程序中使用起来就很方便了,只要将这个dll拖到工具箱面板中,就可以在工具箱中看到这个控件。使用的时候直接从工具箱中拖出来就可以了。

标签:控件,C#,text,void,Color,UI,Items,public
From: https://www.cnblogs.com/xinhuawei/p/16888262.html

相关文章

  • ACM预备队-week3(线性表)
    1.寄存柜题目链接:P3613【深基15.例2】寄包柜-洛谷|计算机科学教育新生态(luogu.com.cn)二维map学到了  stl大法好1#include<bits/stdc++.h>2usingname......
  • logback-spring.xml日志配置
    遇到任何事情,可以放弃,但是永远要有面对的勇气引入对应pom依赖<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spr......
  • argo-rollouts结合Istio进行Canary流行迁移
     给default打标签,让pod自动注入istiosidecar[root@master08-argo-rollouts]#kubectllabelnamespacedefaultistio-injection=enablednamespace/defaultlabeled[......
  • DB2 .NET Data Provider, reason code 10
    最近使用DB2.NETDataProvider访问DB2报了reasoncode10,解决废了很大力气,记录一下错误现象:SQL1159InitializationerrorwithDB2.NETDataProvider,reasoncode......
  • 从阿里跳槽来的工程师,写个Controller都这么优雅!
    目录一个优秀的Controller层逻辑从现状看问题改造Controller层逻辑总结一个优秀的Controller层逻辑说到Controller,相信大家都不陌生,它可以很方便地对外提供数据接口。它......
  • CROS 跨域请求原理
    cros分为两种请求简单请求浏览器将CORS请求分成两类:简单请求(simplerequest)和非简单请求(预检请求)(not-so-simplerequest)。只要同时满足以下两大条件,就属于简单请求。(1)请......
  • 工具篇 - 再探 Logcat
    LZ-Says:最近的Cocos真是让人无处下手,完全蒙蔽状态,无爱了,此时此刻,只想吟诗一首:生亦何欢,死亦何苦~前言Android开发数十载,人来人往,又如何?回想过去的路,不禁感慨万千~而今,借......
  • 使用雪花id或uuid作为Mysql主键,被老板怼了一顿!
    前言:在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采......
  • OpenStack Install
    Glanceglanceimage-create--name"cirros"--filecirros-0.4.0-x86_64-disk.img--disk-formatqcow2--container-formatbare--visibility=public出现Invalid......
  • 《STM32MP1 M4裸机HAL库开发指南》第二十一章 Systick高精度延时实验
    第二十一章Systick高精度延时实验前面章节的实验我们使用的是HAL库里自带的API函数HAL_Delay来实现毫秒级别延时的,如果使用到更高精度的延时,例如us级别的延时,我们可以使用......