Beautify the WinForm Control Using redrawing items and the winform Control

Source: Internet
Author: User

Beautify the WinForm Control Using redrawing items and the winform Control

If you think that the ComboBox, ListBox, or other Winforms controls in the project cannot meet your display requirements, many controls, including forms, support redrawing and modifying the display style. The following example Re-draws the ComBox data items, hoping to play a leading role.

Show source code

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace SimpleDemo {public partial class frmDrawItem: Form {public frmDrawItem () {InitializeComponent (); // specify the painting mode. This parameter must be set to OwnerDrawFixed, ownerDrawVariable // Normal is drawn by the operating system and the element sizes are equal. // The OwnerDrawFixed is manually drawn and the element size is equal. // The OwnerDrawVariable can be manually drawn, and the element size may not be equal. ComboBox2.DrawMode = DrawMode. ownerDrawFixed;} // redraw item event private void comboBox2_DrawItem (object sender, DrawItemEventArgs e) {// obtain the graphic surface Graphics g = e on which to draw items. graphics; // obtain the rectangle System that represents the boundary of the drawn item. drawing. rectangle rect = e. bounds; // defines the icon Image to be drawn to the control. Image ico = Image. fromFile ("head.png"); // defines the font object System. drawing. font font = new System. drawing. font (new FontFamily (""), 12); if (e. index> = 0) {// obtain the text string tempString = comboBox2.Items [e. index]. toString (); // if the current item is a normal item without status if (e. state = DrawItemState. none) {// draw a rectangle g on the current graph surface. fillRectangle (new SolidBrush (Color. fromArgb (200,230,255), rect); // Mark g on the current graph surface. drawImage (ico, new Point (rect. left, rect. top); // draw the Text g of the current Item on the current Item graph surface. drawString (tempString, font, new SolidBrush (Color. black), rect. left + ico. size. width, rect. top); // draws the focus box e. drawFocusRectangle ();} else {e. graphics. fillRectangle (new SolidBrush (Color. lightBlue), rect); g. drawImage (ico, new Point (rect. left, rect. top); g. drawString (tempString, font, new SolidBrush (Color. black), rect. left + ico. size. width, rect. top); e. drawFocusRectangle ();}}}}}

Conclusion: You can choose to use skin controls for beautification, but the re-painting method is more flexible and can meet some functional requirements. In addition, there are some different re-painting events for different controls. You can refer to MSDN.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.