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 * + ;
int y =(int) ((Height - pwid) / 2.0);
Point p = new Point(, y);
picRec = new Rectangle(p, new Size(pwid, pwid));
int txtwid = (int)(Width / 2.0);
NameRec = new Rectangle(new Point(p.X+picRec.Width + , p.Y), new Size(txtwid, txtSize.Height));
DirRec = new Rectangle(new Point(p.X + picRec.Width +, p.Y+txtSize.Height+), new Size(txtwid, txtSize.Height));
BtnRec = new Rectangle(new Point(NameRec.Location.X + NameRec.Width + , NameRec.Y + (int)(txtSize.Height/2.0)), new Size(txtSize.Height,txtSize.Height));
BtnRec1 = new Rectangle(new Point(NameRec.Location.X + DirRec.Width + +txtSize.Width+, NameRec.Y + (int)(txtSize.Height / 2.0)), new Size(txtSize.Height, txtSize.Height));
TimeRec = new Rectangle(new Point(BtnRec1.Location.X + txtSize.Width + , NameRec.Y), new Size(Width-picRec.Width-NameRec.Width-BtnRec.Width*-*, txtSize.Height));
SizeRec = new Rectangle(new Point(BtnRec1.Location.X + txtSize.Width + , NameRec.Y+txtSize.Height+), new Size(Width - picRec.Width - NameRec.Width - BtnRec.Width * - * , 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 > )
{
foreach (var r in ress)
{
if (string.IsNullOrEmpty(r))
continue;
res.Add(r);
res.Add(key);
}
if (!s.EndsWith(key))
res.RemoveAt(res.Count - );
}
else
{
res.Add(s);
} StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
sf.Trimming = StringTrimming.EllipsisCharacter; int cwid = ;
for (int i = ; 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 - );
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#画个控件,指定字符特殊颜色显示的更多相关文章
- Lodop打印控件指定打印任务某几页
使用Lodop打印控件进行打印开发,有时候一个任务里有多页,例如各种合同之类的,客户端用户在使用过程中,可能有某一页打印后发现需要修改,这时候不必再把整个任务重新打印一遍,只需要打印需要修改的那页重新 ...
- DevExpress相关控件中非字符数值居左显示
用了这么长时间的DevExpress控件,今天遇到俩问题. 一个是从头到尾看了一遍编译成功的例子,只能感慨,功能太丰富了,自己所用的不过是冰山一角.有些自己一直想实现的效果,原来早就有现成的可用,汗颜 ...
- VC++使用WebBrowser控件,强制给控件指定版本显示网页
转载:http://www.cnblogs.com/1175429393wljblog/p/5398928.html 最近为了抓取淘宝的成交数据,用C#的WebBrowser控件开发了一个简单的程序. ...
- Jquery设置select控件指定text的值为选中项
<select name="streetid" id="streetid"> <option value="4">北 ...
- Qt 设置背景图片3种方法(QPalette可以做无数的事情,一旦控件指定了调色板,就要乖乖听它的话;QPainter当场绘制当然也没有问题,还有就是QSS)
方法1. setStylSheet{"QDialog{background-image:url()"}} //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...
- Shape画圆形控件
这里涉及到shape的运用,这仅仅是一个实例 circle.xml <?xml version="1.0" encoding="utf-8"?> & ...
- MSCHART控件中长字符的X轴坐标标注全部显示
X轴坐标如果超过9位的话,就不能完全显示了,就会一个隔一个的显示,解决的办法: Chart1.ChartAreas[].AxisX.Interval = ; //设置X轴坐标的间隔为1 Chart1. ...
- 强制IE浏览器或WebBrowser控件使用指定版本显示网页
自从装了IE10之后,就发现好些个网站显示都不是那么的正常,网站上有些功能竟然还会出现一些意想不到的BUG——本来就是针对IE开发的,现在IE下竟然用不起来了,让用户情何以堪?但是就为少量用户使用的系 ...
- Delphi 7学习开发控件(继承TGraphicControl只画一条线)
我们知道使用Delphi快速开发,很大的一方面就是其强大的VCL控件,另外丰富的第三方控件也使得Delphi程序员更加快速的开发出所需要的程序.在此不特别介绍一些概念,只记录自己学习开发控件的步骤.假 ...
随机推荐
- 手机APP测试点总结
一.功能性测试: (1)根据产品需求文档编写测试用例 (2)软件设计文档编写用例 二.兼容性适配性测试: (1)Android.iOS版本的兼容性 (2)手机分辨率兼容性 (3)网络的兼容性:2G/3 ...
- 数据分析:pandas 基础
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...
- day 13 课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/7 18:00# @Author : Endless-cloud# @Site : # @File : day 13 课 ...
- ubuntu16.04 安装 mysql-level
ubuntu16.04 安装 mysql-level 1.下载 mysql-level(直接去官网下载rpm包,我的mysql-server是5.7.9.你下载自己对应的就可以了) 下载连接 2.rp ...
- SQL Server——存储过程(Stored Procedure)、事物、触发器
存储过程(proc 或 procedure) 存储过程(Stored Procedure),计算机用语,是一组为了完成特定功能的SQL语句集,是利用SQL Server所提供的Transact-SQL ...
- ASP.NET:Application,Session,Cookie,ViewState和Cache之间的区别(转)
在ASP.NET中,有很多种保存信息的对象.例如:Application,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 为了更清楚 ...
- MyBatis学习笔记(一)创建第一个MyBatis项目
一.新建Maven项目 http://www.mybatis.org/mybatis-3/zh/index.html 该链接为MyBatis官方地址 创建MyBatis项目主要有两种办法,一种是导入j ...
- [Re:从零开始的分布式] 0.x——分布式锁概述
为什么需要分布式锁 Martin Kleppmann是英国剑桥大学的分布式系统的研究员,Martin认为一般我们使用分布式锁有两个场景: 效率:使用分布式锁可以避免不同节点重复相同的工作,这些工作会浪 ...
- SBC应用
在VoIP呼叫中主要使用会话发起协议(SIP),H.323和MGCP呼叫信令协议,Sbc 在主叫和被叫的信令/媒体路径之间引入. 通常,SBC隐藏网络拓扑,接管呼入并生成到新的请求分支到被叫.技术上叫 ...
- 小程序中实时将less编译成wxss
1.npm或者yarn全局安装wxss-cli npm install -g wxss-cli 2.运行wxss-cli命令(weuiTest为小程序目录) wxss ./weuiTest 实时监听w ...