5.
Projects using Intrinsic and Professional ActiveX Controls(4)
In this lesson we will consider the
construction of the following Graphic Projects:
·
1. Drawing the graph for any function
·
2. Drawing Pie Chart
·
3. Colored Pixels
·
4. Color Gradient
One can place Graphics on three controls:
- Form
- PictureBox
- ImageBox
The main
difference between these three controls is that the ImageBox is specially
designed for displaying bitmaps, whereas the other two controls provide drawing
methods, which let you design graphics at run time.
1. Project for Graphing any
Function:
Drag a Picture
box, and two command button in the
form, size them, caption them and position them as shown in the figure 1. Open the code window of the command
button and enter the codes as shown in the figure 1a. Save and run the project.
You will get the graph for the function cos(3x)*sin(5x). You can try with different types of
functions and see how the scale properties are calculated for each function and
the graphs are drawn. A typical output
is shown in the figure 2.

Figure 1
Private
Sub Command1_Click()
Dim
t, functionval As Double
Dim
xmin,
xmax,
ymin, ymax As Double
ymin
= 1E+33: ymax = -1E+33
xmin
= 2: xmax = 10
Picture1.Cls
Picture1.ScaleMode
= 3
xpixels
= Picture1.ScaleWidth - 1
'calculate
min and max for y axis
For
i = 1 To xpixels
t =
xmin +
(xmax -
xmin) * i / xpixels
functionval
= functioneval1(t)
If
functionval > ymax Then ymax = functionval
If
functionval < ymin Then ymin = functionval
Next
'set
a user defined scale mode
Picture1.Scale
(xmin,
ymin)-(xmax,
ymax)
'plot
the function
For
i = 0 To xpixels
t =
xmin +
(xmax -
xmin) * i / xpixels
Picture1.Line
-(t, functioneval1(t))
Next
End
Sub
Function
functioneval1(ByVal x As Double) As Double
functioneval1
= Cos(3 * x) * Sin(5 * x)
End
Function
Private
Sub Command2_Click()
End
End
Sub
Figure 1a

Figure 2
   |