30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16 Graphics and Multimedia 687<br />

Calling the OnPaint method raises the Paint event. Instead of overriding the<br />

OnPaint method, programmers can add an event handler for the Paint event. First,<br />

write the code for the Paint event handler in this form:<br />

Public Sub MyEventHandler_Paint( _<br />

ByVal sender As Object, ByVal e As PaintEventArgs) _<br />

Handles MyBase.Paint<br />

<strong>Program</strong>mers seldom call the OnPaint method directly, because the drawing of<br />

graphics is an event-driven process. An event—such as the covering, uncovering or resizing<br />

of a window—calls the OnPaint method of that form. Similarly, when any control (such as<br />

a TextBox or Label) is displayed, the program calls that control’s Paint method.<br />

If programmers need <strong>to</strong> invoke method OnPaint explicitly, they can call the<br />

Invalidate method (inherited from Control). This method refreshes a control’s<br />

client area and repaints all graphical components. <strong>Visual</strong> <strong>Basic</strong> contains several overloaded<br />

Invalidate methods that allow programmers <strong>to</strong> update portions of the client area.<br />

Performance Tip 16.1<br />

Calling the Invalidate method <strong>to</strong> refresh the Control often is inefficient. Instead, call<br />

Invalidate with a Rectangle parameter <strong>to</strong> refresh only the area designated by the<br />

rectangle. This improves program performance. 16.1<br />

Controls, such as Labels and But<strong>to</strong>ns, also have their own graphics contexts. To<br />

draw on a control, first obtain its graphics object by invoking the CreateGraphics<br />

method:<br />

Dim graphicsObject As Graphics = label1.CreateGraphics()<br />

Then, you can use the methods provided in class Graphics <strong>to</strong> draw on the control.<br />

16.3 Color Control<br />

Colors can enhance a program’s appearance and help convey meaning. For example, a red<br />

traffic light indicates s<strong>to</strong>p, yellow indicates caution and green indicates go.<br />

Structure Color defines methods and constants used <strong>to</strong> manipulate colors. Because it<br />

is a lightweight object that performs only a handful of operations and s<strong>to</strong>res Shared fields,<br />

Color is implemented as a structure, rather than as a class.<br />

Every color can be created from a combination of alpha, red, green and blue components.<br />

Together, these components are called ARGB values. All four ARGB components<br />

are Bytes that represent integer values in the range from 0 <strong>to</strong> 255. The alpha value determines<br />

the intensity of the color. For example, the alpha value 0 results in a transparent<br />

color, whereas the value 255 results in an opaque color. Alpha values between 0 and 255<br />

result in a weighted blending effect of the color’s RGB value with that of any background<br />

color, causing a semi-transparent effect. The first number in the RGB value defines the<br />

amount of red in the color, the second defines the amount of green and the third defines the<br />

amount of blue. The larger the value, the greater the amount of that particular color. <strong>Visual</strong><br />

<strong>Basic</strong> enables programmers <strong>to</strong> choose from almost 17 million colors. If a particular computer<br />

cannot display all these colors, it will display the color closest <strong>to</strong> the one specified.<br />

Figure 16.3 summarizes some predefined color constants, and Fig. 16.4 describes several<br />

Color methods and properties.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!