Wednesday, October 6, 2010

Visual Basic to create "five star" level control





Figure 1 "five-star" level control

This control looks great and provides a good graphical way to view the grade, but according to editorial experience, I find it especially cool. When the mouse hover over the bar when, Windows Media Player highlights one of the stars to show you the current float of its value on the bar, thus providing a good graphical feedback. In a variety of Web sites (including Netflix and Amazon), you can find the same type of user interface, and I think in their own applications have this feature, so I decided to create my own control. I'll use Windows Forms controls that the user interface elements to simulate the same time try to make it custom with sufficient capacity to be used in various environments.

Getting Started

The first step is to create a new Class Library project to hold the controls and empty Windows application to a test project. Although the Windows Control Library project template appears to be more appropriate, and it is well to work, but by default, the project includes user controls (user controls 鈹?General for composite control contains one or more controls for Windows Forms control), and I need is an empty class file. Next, you must present a new empty class inherits from the System.Windows.Forms.Control, which simply add a single class declaration can be achieved:

Public Class Ratings
Inherits System.Windows.Forms.Control End Class

If you try to use only IntelliSense? Add Inherits statement, will encounter a small problem: use Class Library template to start your project will not add a reference to the System.Windows.Forms assembly, so you need to manually add. At this point, be the first to add a reference to an System.Drawing.dll, because the final drawing to use custom controls. From now on, I generally follow the following steps to carry out all of the control development:

1. To add custom drawing control standards constructor, set the control style of all the necessary controls in order to correct the drawing and smooth as possible.

Public Sub New ()
Me.SetStyle (ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle (ControlStyles.DoubleBuffer, True)
Me.SetStyle (ControlStyles.ResizeRedraw, True)
Me.SetStyle (ControlStyles.UserPaint, True)
...''Add any additional initialization code here
End Sub

2. Paper record configuration control behavior and appearance may need to list the public properties.

3. All these attributes add to the private member variable (I prefer to use Hungarian notation in front of each variable to add the prefix "m_" to represent the internal variables), and includes the appropriate default values, as shown below.

Private m_FilledImage As Image
Private m_EmptyImage As Image
Private m_HoverImage As Image
Private m_ImageCount As Integer = 5
Private m_TopMargin As Integer = 2
Private m_LeftMargin As Integer = 4
Private m_BottomMargin As Integer = 2
Private m_RightMargin As Integer = 4
Private m_ImageSpacing As Integer = 8
Private m_ImageToDraw As Integer = 1
Private m_SelectedColor As Color = Color.Empty
Private m_HoverColor As Color = Color.Empty
Private m_EmptyColor As Color = Color.Empty
Private m_OutlineColor As Color = Color.Empty

Private m_selectedItem As Integer = 3
Private m_hoverItem As Integer = 1
Private m_hovering As Boolean = False

Private ItemAreas () As Rectangle

4. The properties of these variables into the process, although most of the process is fairly simple (get value, set value), but some of the process will require some additional code, I will discuss later.

5. Began to design and custom graphics code.

6. Finally, add a new event, such as clicking a specific control process or the requirements of the specific event.

The default value of properties with a specific routine

I want some of my properties

鈹?handling those attributes of color

鈹?reflect the control of other properties of the default values (such as ForeColor) and to reflect the user's system color of other values. For example, let us use only one of them can produce color HoverColor to see the default value of different methods.

The first is obvious, just in the variable declaration (or constructor) to set default values:

Private m_HoverColor As Color = _
Color.FromKnownColor (KnownColor.Highlight)

In most cases, this can work well, but there are two problems. The first question is, if the user running the application to change their system colors, how to do? After you restart the program, the control will reflect the correct color, but was not. The second question is, if the user to programmatically set the color to the default colors, how to do? There is no practical way to clear the color settings and set it to the appropriate system color. Users will naturally be set correctly for the appropriate system color, but then will return to first question. Another method is to color changes in the user's system when the traps, and the corresponding changes to your property value:

Protected Overrides Sub OnSystemColorsChanged (_
ByVal e As System.EventArgs)
Me.HoverColor = Color.FromKnownColor (KnownColor.Highlight)
Me.Invalidate ()







相关链接:



BI the NEW World: semi-finals games, who the winner?



Easy to change driving



Introduction To BREW SDK Papers (2)



comparison GRAPHIC Editors



Understanding encrypting file system feature of



How to write text to soft fast sellers?



C # event mechanism of Induction (B)



The new VPN strength



SIMPLE Games Arcade



Thunder Raise His Flag Charges, Cool 6 Has The Intention



OGM to MP4



FLV to PSP



OGM To MKV



Britain Invented The Robot Can Be Swallowed Pills Belly Detection Of Cancer



Tissot wave THROUGH the "Dakar"



No comments:

Post a Comment