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.