Beautify the controls in winform using redrawing items

Source: Internet
Author: User

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 draw mode. This parameter must be set to ownerdrawfixed and ownerdrawvariable.
// Normal is drawn by the operating system, and the elements are of the same size.
// The ownerdrawfixed is manually drawn and the element sizes are equal.
// The ownerdrawvariable is manually drawn, and the element size may be different.
Combobox2.drawmode = Drawmode. ownerdrawfixed;
}

// Redraw item event
Private   Void Combobox2_drawitem ( Object Sender, drawitemeventargs E)
{
// Obtain the graphic surface on which you want to draw items
Graphics g = E. graphics;
// Obtains the rectangle representing the boundary of the drawn item.
System. Drawing. Rectangle rect = E. bounds;
// Define the icon image to be drawn to the control
Image ICO = Image. fromfile ( " Head.png " );
// Define font objects
System. Drawing. Font font =   New System. Drawing. Font ( New Fontfamily ( " " ), 12 );
If (E. Index > =   0 )
{
// Obtain the text of the current item
String Tempstring = Combobox2.items [E. Index]. tostring ();
// If the current item is a normal item with no status
If (E. State = Drawitemstate. None)
{
// Draw a rectangle on the current graph Surface
G. fillrectangle ( New Solidbrush (color. fromargb ( 200 , 230 , 255 ), Rect );
// Mark the current item on the graphic Surface
G. drawimage (ICO, New Point (rect. Left, rect. Top ));
// Draw the text of the current item on the current item graph Surface
G. drawstring (tempstring, Font, New Solidbrush (color. Black), rect. Left + ICO. Size. Width, rect. Top );
// Will draw a 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.

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.