C # Draw a widget to specify the special color of characters for display,

Source: Internet
Author: User

C # Draw a widget to specify the special color of characters for display,

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 (); Recta Ngle 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) (H Eight-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 + Name Rec. 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-B TnRec. 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); // icon var t1 = "this is a test document "; var t2 = "this is a test document. This is a test document. A test document "; drawTxt (t1, g, NameRec, "this"); DrawTxt (t2, g, DirRec, "this"); g. fillRectangle (Brushes. green, BtnRec); g. fillRectangle (Brushes. blue, BtnRec1); var t3 = string. format ("modification time: {0}", DateTime. now. toString ("yyyy-MM-dd hh: mm: ss"); var t4 = "file size: 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 ). wi Dth; 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 ;}}}}

The User Controls heap from their own controls are placed in flowlayotpanel. The scrolling process is even worse ·, here we will draw one by ourselves. By the way, we can solve the problem of displaying different colors of special characters. This heap seems to flash as well. The heap of 200 characters in the panel will flash ·· · we should still have to do paging... so troublesome ···

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.