zoukankan      html  css  js  c++  java
  • C#画个控件,指定字符特殊颜色显示

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ExerciseUIPrj.controls
    {
        public partial class CustomControl1 : Control
        {
    
            Rectangle picRec = new Rectangle();
            Rectangle NameRec = new Rectangle();
            Rectangle DirRec = new Rectangle();
            Rectangle BtnRec = new Rectangle();
            Rectangle BtnRec1 = new Rectangle();
            Rectangle TimeRec = new Rectangle();
            Rectangle SizeRec = new Rectangle();
    
            public CustomControl1()
            {
                InitializeComponent();
                BackColor = Color.White;
            }
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                Size txtSize = TextRenderer.MeasureText("abc", Font);
                int pwid = txtSize.Height * 2 + 10;
                int y =(int) ((Height - pwid) / 2.0);
                Point p = new Point(5, y);
                picRec = new Rectangle(p, new Size(pwid, pwid));
                int txtwid = (int)(Width / 2.0);
                NameRec = new Rectangle(new Point(p.X+picRec.Width + 2, p.Y), new Size(txtwid, txtSize.Height));
                DirRec = new Rectangle(new Point(p.X + picRec.Width +2, p.Y+txtSize.Height+5), new Size(txtwid, txtSize.Height));
                BtnRec = new Rectangle(new Point(NameRec.Location.X + NameRec.Width + 2, NameRec.Y + (int)(txtSize.Height/2.0)), new Size(txtSize.Height,txtSize.Height));
                BtnRec1 = new Rectangle(new Point(NameRec.Location.X + DirRec.Width + 2+txtSize.Width+2, NameRec.Y + (int)(txtSize.Height / 2.0)), new Size(txtSize.Height, txtSize.Height));
                TimeRec = new Rectangle(new Point(BtnRec1.Location.X + txtSize.Width + 4, NameRec.Y), new Size(Width-picRec.Width-NameRec.Width-BtnRec.Width*2-2*4, txtSize.Height));
                SizeRec = new Rectangle(new Point(BtnRec1.Location.X + txtSize.Width + 4, NameRec.Y+txtSize.Height+2), new Size(Width - picRec.Width - NameRec.Width - BtnRec.Width * 2 - 2 * 4, txtSize.Height));
                
    
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
    
                var g = pe.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.FillRectangle(Brushes.Red, picRec);//图标
    
                var t1 = "这是一句测试文档";
                var t2 = "这是一句测试文档这是一句测试文档这是一句测试文档这是一句测试文档这是一句测试文档这是一句测试文档这是一句测试文档这是一句测试文档";
                DrawTxt(t1, g, NameRec, "");
                DrawTxt(t2, g, DirRec, "");
                g.FillRectangle(Brushes.Green, BtnRec);
                g.FillRectangle(Brushes.Blue, BtnRec1);
                var t3 = string.Format("修改时间:{0}",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                var t4 = "文件大小:4555KB";
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                g.DrawString(t3, Font, Brushes.Black, TimeRec,sf);
                g.DrawString(t4, Font, Brushes.Black, SizeRec,sf);
    
                base.OnPaint(pe);
    
            }
            void DrawTxt(string s, Graphics g, Rectangle rect,string key)
            {
                string[] ress = s.Split(key.ToCharArray());
    
                List<string> res = new List<string>();
                if (s.StartsWith(key))
                    res.Add(key);
                if (ress.Length > 1)
                {
                    foreach (var r in ress)
                    {
                        if (string.IsNullOrEmpty(r))
                            continue;
                        res.Add(r);
                        res.Add(key);
                    }
                    if (!s.EndsWith(key))
                        res.RemoveAt(res.Count - 1);
                }
                else
                {
                    res.Add(s);
                }
    
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                sf.Trimming = StringTrimming.EllipsisCharacter;
                
                int cwid = 0;
                for (int i = 0; i < res.Count; i++)
                {
                    int wid = TextRenderer.MeasureText(g, res[i], Font,new Size(),TextFormatFlags.NoPadding|TextFormatFlags.NoPrefix).Width;
                    Brush b = res[i] == key ? Brushes.Red : Brushes.Black;
                    int x = cwid+wid;
                    if(x>=rect.Width)
                    {
                        wid = rect.Width - cwid;
                        RectangleF rec = new RectangleF(new PointF(rect.Location.X+cwid,rect.Y), new SizeF(wid, rect.Height));
                        g.DrawString(res[i], Font, b, rec, sf);
                        break;
                    }
                    else
                    {
                        g.DrawString(res[i], Font, b, new Point(rect.Location.X + cwid, rect.Y), sf);
                    }
                    cwid += wid;
    
                }
            }
        }
    }

    用自带的控件堆出来的用户控件放在flowlayotpanel里边多了滚动的时候闪的厉害·,这里就用自己画一个··顺便解决特殊字符不同颜色显示的问题··这个堆多了好像也会闪烁···在panel里边堆200个就会闪····应该还是得做分页·····好麻烦···

    用windowsbase里边的库压文件夹为zip文件

            public static bool PackageFolder(string folderName, string compressedFileName, bool overrideExisting)
            {
                if (folderName.EndsWith(@""))
                    folderName = folderName.Remove(folderName.Length - 1);
                bool result = false;
                if (!Directory.Exists(folderName))
                {
                    return result;
                }
                if (!overrideExisting && File.Exists(compressedFileName))
                {
                    return result;
                }
                try
                { 
                    using (Package package = Package.Open(compressedFileName, FileMode.Create))
                    {
                        var fileList = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);
                        foreach (string fileName in fileList)
                        {
                            //The path in the package is all of the subfolders after folderName
                            string pathInPackage = Path.GetDirectoryName(fileName).Replace(folderName, string.Empty) + "/" + Path.GetFileName(fileName);
                            Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(pathInPackage, UriKind.Relative));
                            PackagePart packagePartDocument = package.CreatePart(partUriDocument, "", CompressionOption.Maximum);
                            using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                            {
                                fileStream.CopyTo(packagePartDocument.GetStream());
                            }
                            Console.WriteLine("{0}  done", fileName);
                        }
                    }
                    result = true;
                }
                catch (Exception e)
                {
                    throw new Exception("Error zipping folder " + folderName, e);
                }
    
                return result;
            }
  • 相关阅读:
    C++11 function用法 可调用对象模板类
    2017年团体程序设计天梯赛
    CCCC 以及 hihocoder offer收割赛11 ~~~
    Wannafly Union Goodbye 2016-A//初识随机化~
    Good Bye 2016 //智商再次下线,边界爆炸.....
    连做两场goodbye2016是怎样的体验.....
    2016CCPC 合肥--最大公约数//每一年通向它的路上,多少人折戟沉沙,多少人功败垂成,有人一战成名,有人从头再来。
    2016 CCPC 合肥赛区 平行四边形//打铁记录..... 背锅还是我在行 此处@ctr 233
    补题安排及挂机....
    BZOJ3670:[NOI2014]动物园
  • 原文地址:https://www.cnblogs.com/onegarden/p/7594417.html
Copyright © 2011-2022 走看看