UNIT-III Graphics in VB

Graphics

      In Visual Basic 6.0, various graphics methods and properties are used to draw on a Form or PictureBox control. Graphics in Visual Basic 6.0 are based on the Windows Graphics Device Interface (GDI) APIs.
In Visual Basic 6.0, graphics methods apply only to the Form object and to the PictureBox control.
AutoRedraw Property :
                Graphics methods can be called from any event procedure. The AutoRedraw  property is used to persist graphics when graphics methods are called from an event other than the Paint event.

ClipControls Property :
                                        The ClipControls property is used to control the painting of a form or control. When set to True, only newly exposed areas are repainted, in theory, improving performance.

DrawMode Property :
                                      The DrawMode property controls the color of a graphics object when drawing one pattern on top of another. This property only affects monochrome or low-resolution displays (256 colors or less).

DrawStyle Property :
                                  The DrawStyle property controls the appearance of a line drawn using the Line method. If the DrawWidth property is set to a value greater than 1, the DrawStyle property has no effect and the line will always be solid.

DrawWidth Property :
                                  The DrawWidth property determines the thickness of a line in pixels; the DrawWidth property is typically set before performing a graphics method.

Image Property :
                           The Image property of a form or PictureBox control returns a handle to a bitmap. The handle can be assigned to the Picture property or used as a value to pass to Windows API calls.

Line Method :
            The Line method is used to draw a rectangle by specifying the top left and lower coordinates, along with an optional argument. TheFillColor property is used to fill a rectangle with a solid color, and the FillStyle property fills the rectangle with a crosshatch pattern.

Point Method :
                       In Visual Basic 6.0, the Point method of a form or PictureBox control is used to return a color value for the pixel at a specified point. Although the Pointmethod can be used for forms or controls that do not contain a picture, it is most often used to retrieve a color from a bitmap assigned to the Pictureproperty.

Print Method :
                         In Visual Basic 6.0, the Print method is used to display text on a form or PictureBox control. The font used to display the text is determined by the Fontproperties of the form or control, and the color is determined by the ForeColor property. The Print method offers no control for the location of the text and can only display text horizontally.

PSet Method :
                       In Visual Basic 6.0, the PSet method is used to change the color of a pixel on a form or PictureBox control. If the DrawWidth property is set to a value greater than 1, the PSet method draws a filled circle. An optional parameter is used to specify the color; if omitted ForeColor is used.

Different Graphical representations :

1. Drawing a Simple Line, Single Pixel and a Dotted Line :
                                                       For changing the color of a single pixel on a form at run time, uses the PSet method and drawing a line on a form at run time use Line method.
It takes the X and Y coordinates of the starting and ending points and, optionally, a color as arguments.
In Visual Basic 6.0, the default unit of measurement is twips.
      Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                      Private Sub Form_Paint()
                                      ' Draw a solid red  line 200 twips from the top of the form.
                               Line (0, 200) - (ScaleWidth, 200), vbRed
                                       ‘For Dotted line
                               Me.DrawStyle = vbDot
                               Line (1, 500)-(ScaleWidth, 500), vbBlack
                                        ‘single pixel
                               Me.DrawWidth = 8
                               PSet (1000, 1000), vbGreen
                      End Sub
Now execute the program by pressing F5.Hence the output is shown below :    

                           
2. Controlling Line Thickness :
                                       The following code demonstrates drawing lines of different thicknesses on a form at run time. In the Visual Basic 6.0 example, the DrawWidth property is used.
 Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                  Private Sub Form_Paint()
                                                        ' Draw a line with a thickness of 1 pixel.
                           DrawWidth = 1
                           Line (0, 200)-(ScaleWidth, 200), vbBlack
                                                       ' Draw a line with a thickness of 5 pixels.
                            DrawWidth = 5
                            Line (0, 400)-(ScaleWidth, 400), vbRed
                                                      ' Draw a line with a thickness of 10 pixels.
                           DrawWidth = 10
                           Line (0, 800)-(ScaleWidth, 800), vbGreen
                   End Sub
Now execute the program by pressing F5.Hence the output is shown below.
                   
3. Drawing a Circle :
                                 The following code demonstrates drawing a circle on a form at run time.
The Circle method is used.
It takes the X and Y coordinates of the center point, the radius, and, optionally, a color, as arguments.
          Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                                     Private Sub Form_Paint()
                                                    DrawWidth = 3
                                           Circle (1600, 1600), 500, vbRed
                                                    DrawWidth = 6
                                           Circle (1000, 1000), 700, vbGreen
                                      End Sub
Now execute the program by pressing F5.Hence the output is shown below
                                       
4. Drawing a Filled Rectangle :
                                          The following code demonstrates drawing two rectangles on a form at run time, one with a solid fill and the other with a cross-hatched pattern.
               In the Visual Basic 6.0 example, the FillColor and FillStyle properties are used along with the Line method. Calling the Line method with the B parameter draws a rectangle.
Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                              Private Sub Form_Paint()
                                                            ' Draw a solid red rectangle.
                                     FillColor = vbRed
                                     FillStyle = vbSolid
                                           Line (100, 100)- (1000, 500), vbRed, B
                                                           ' Draw a rectangle filled with a crosshatch pattern.
                                     FillColor = vbGreen
                                     FillStyle = vbCross
                                            Line (100, 500)- (1000, 1000), vbGreen, B
                              End Sub
Now execute the program by pressing F5.Hence the output is shown below.

                   

5. Displaying an Image on a Form with a text and Determining the Color of a Single Pixel :
                   For displaying an image on a form at run time  we use LoadPicture and for displaying a text string on a form at run time uses the Print method. Graphics method for determining the color of a pixel at a specified location in an image on a form at run time, and then drawing a rectangle filled with that color uses the PixelColor and Point method to retrieve the color value.
            Ex :
Open a new standard EXE project and the Form
Place a Picturebox on the form and change the Autosize property to “True ”.
Enter the following code in the Form_Paint() procedure
                              Private Sub Form_Paint()
                          Dim PixelColor As Long
                                                     ‘for loading a picture at runtime                        
                                  Set Picture1.Picture = LoadPicture("D:\TUTORIALS\sample                                                   pictures\picture\pictures\TWEETY1.gif")
                                  PixelColor = Picture1.Point(1000, 1000)
                                  DrawWidth = 5
                                  FillColor = PixelColor
                                  Line (4000, 3000)-(700, 700), PixelColor, B
                                                ‘for displaying the text                      
                                 Me.Font.Size = 24
                                 Me.Font.Bold = True
                                 Me.ForeColor = vbRed
                                  Print "Hai welcome !"
                                       End Sub
 Now execute the program by pressing F5.Hence the output is shown below.

                         
6. Moving of an Image :
An image can be moved from one position to another position using the top, left and the state of the image.
               Ex :
Open a new standard EXE project and the Form
Place a command button and an image control on the Form. In properties window change the stretch property to True and place an image.
Enter the following code in the command1_click()
                              Private Sub Command1_Click()
                                      Dim counter As Integer
                                      For counter = 1 To 10 Step 1
                                      Image1.Top = Image1.Top - 20
                                      Image1.Left = Image1.Left - 20
                                      Next counter
                              End Sub
Enter the following code in Form_Load() event
Private Sub Form_Load()
           Form1.WindowState = 2
End Sub
 Now execute the program by pressing F5.Hence the output is shown below.
   

FlexGrid
FlexGrid :
   A MS FlexGrid control is used to create applications that present information in rows and columns. It displays information in cells. A cell is a location in the MSFlexGrid at which a row and a column intersect.
The MSFlexGrid control displays and operates on tabular data. It allows complete flexibility to sort, merge and format tables containing strings and pictures. When bound to a Data control, it displays a read-only data.
We can place text, or a picture, or both in any cell. The Row and Col properties specify the current cell. We can specify the current cell in code, or the user can change it at run-time. If a cell’s text is too long to be displayed in the cell then the WordWrap property is set to True, the text wraps to the next line within the same cell. We need to increase the cell’s column width (ColWidth property) and row height(RowHeight property) to display the wrapped text.
Types :
Two kinds of rows and columns are created in MSFlexGrid control.
They are :        
Fixed : A fixed row or column does not scroll at any time. These are generally  used for displaying headings.
Non-Fixed : A non-fixed row or column scrolls when the scroll bars are active.
Rows and Columns are created by setting the four properties of the MSFlexGrid control as Rows, Cols, FixedRows, and FixedCols.
            Ex :
Open a new standard EXE project and the Form.
To add a FlexGrid control on the Form, Choose Components from the Project menu and put a check on the Microsoft FlexGrid Control 6.0 as shown below.
 
Click on Apply and then Ok. Then a FlexGrid control is added to the toolbox.
Design a Form as shown below.
Place three Labels, two comboboxes, one textbox, one commandbutton and a FlexGrid control on the form.

In properties window change the MSFlexGrid  name as itmdet and change the rows as 8 and cols as 4.
In the general declarations section of the form, enter the following code :
                                      Dim arr(7) As String
                                      Dim itm(3) As String
                                      Dim i As Integer
Enter the following code in command1_Click() :
                        Private Sub Command1_Click()
                                     itmdet.Row = Combo1.ListIndex + 1
                                     itmdet.Col = Combo2.ListIndex + 1
                                     itmdet.Text = Str(Val(itmdet.Text) + Val(Text1.Text))
                         End Sub
Enter the following code in Form_Load event :
                        Private Sub Form_Load()
                               itm(0) = "pens"
                               itm(1) = "pencils"
                               itm(2) = "erasers"
                               arr(0) = "sunday"
                               arr(1) = "monday"
                               arr(2) = "tuesday"
                               arr(3) = "wednesday"
                               arr(4) = "thursday"
                               arr(5) = "friday"
                               arr(6) = "saturday"
                           

                              itmdet.Row = 0
                              For i = 0 To 2
                                      itmdet.Col = i + 1
                                      itmdet.Text = arr(i)
                                     Combo2.AddItem arr(i)
                              Next
                           
                              itmdet.Col = 0
                              For i = 0 To 2
                                      itmdet.Row = i + 1
                                      itmdet.Text = arr(i)
                                     Combo1.AddItem arr(i)
                               Next
                        End Sub      
                         

No comments:

UNIT-III Graphics in VB

Graphics

      In Visual Basic 6.0, various graphics methods and properties are used to draw on a Form or PictureBox control. Graphics in Visual Basic 6.0 are based on the Windows Graphics Device Interface (GDI) APIs.
In Visual Basic 6.0, graphics methods apply only to the Form object and to the PictureBox control.
AutoRedraw Property :
                Graphics methods can be called from any event procedure. The AutoRedraw  property is used to persist graphics when graphics methods are called from an event other than the Paint event.

ClipControls Property :
                                        The ClipControls property is used to control the painting of a form or control. When set to True, only newly exposed areas are repainted, in theory, improving performance.

DrawMode Property :
                                      The DrawMode property controls the color of a graphics object when drawing one pattern on top of another. This property only affects monochrome or low-resolution displays (256 colors or less).

DrawStyle Property :
                                  The DrawStyle property controls the appearance of a line drawn using the Line method. If the DrawWidth property is set to a value greater than 1, the DrawStyle property has no effect and the line will always be solid.

DrawWidth Property :
                                  The DrawWidth property determines the thickness of a line in pixels; the DrawWidth property is typically set before performing a graphics method.

Image Property :
                           The Image property of a form or PictureBox control returns a handle to a bitmap. The handle can be assigned to the Picture property or used as a value to pass to Windows API calls.

Line Method :
            The Line method is used to draw a rectangle by specifying the top left and lower coordinates, along with an optional argument. TheFillColor property is used to fill a rectangle with a solid color, and the FillStyle property fills the rectangle with a crosshatch pattern.

Point Method :
                       In Visual Basic 6.0, the Point method of a form or PictureBox control is used to return a color value for the pixel at a specified point. Although the Pointmethod can be used for forms or controls that do not contain a picture, it is most often used to retrieve a color from a bitmap assigned to the Pictureproperty.

Print Method :
                         In Visual Basic 6.0, the Print method is used to display text on a form or PictureBox control. The font used to display the text is determined by the Fontproperties of the form or control, and the color is determined by the ForeColor property. The Print method offers no control for the location of the text and can only display text horizontally.

PSet Method :
                       In Visual Basic 6.0, the PSet method is used to change the color of a pixel on a form or PictureBox control. If the DrawWidth property is set to a value greater than 1, the PSet method draws a filled circle. An optional parameter is used to specify the color; if omitted ForeColor is used.

Different Graphical representations :

1. Drawing a Simple Line, Single Pixel and a Dotted Line :
                                                       For changing the color of a single pixel on a form at run time, uses the PSet method and drawing a line on a form at run time use Line method.
It takes the X and Y coordinates of the starting and ending points and, optionally, a color as arguments.
In Visual Basic 6.0, the default unit of measurement is twips.
      Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                      Private Sub Form_Paint()
                                      ' Draw a solid red  line 200 twips from the top of the form.
                               Line (0, 200) - (ScaleWidth, 200), vbRed
                                       ‘For Dotted line
                               Me.DrawStyle = vbDot
                               Line (1, 500)-(ScaleWidth, 500), vbBlack
                                        ‘single pixel
                               Me.DrawWidth = 8
                               PSet (1000, 1000), vbGreen
                      End Sub
Now execute the program by pressing F5.Hence the output is shown below :    

                           
2. Controlling Line Thickness :
                                       The following code demonstrates drawing lines of different thicknesses on a form at run time. In the Visual Basic 6.0 example, the DrawWidth property is used.
 Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                  Private Sub Form_Paint()
                                                        ' Draw a line with a thickness of 1 pixel.
                           DrawWidth = 1
                           Line (0, 200)-(ScaleWidth, 200), vbBlack
                                                       ' Draw a line with a thickness of 5 pixels.
                            DrawWidth = 5
                            Line (0, 400)-(ScaleWidth, 400), vbRed
                                                      ' Draw a line with a thickness of 10 pixels.
                           DrawWidth = 10
                           Line (0, 800)-(ScaleWidth, 800), vbGreen
                   End Sub
Now execute the program by pressing F5.Hence the output is shown below.
                   
3. Drawing a Circle :
                                 The following code demonstrates drawing a circle on a form at run time.
The Circle method is used.
It takes the X and Y coordinates of the center point, the radius, and, optionally, a color, as arguments.
          Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                                     Private Sub Form_Paint()
                                                    DrawWidth = 3
                                           Circle (1600, 1600), 500, vbRed
                                                    DrawWidth = 6
                                           Circle (1000, 1000), 700, vbGreen
                                      End Sub
Now execute the program by pressing F5.Hence the output is shown below
                                       
4. Drawing a Filled Rectangle :
                                          The following code demonstrates drawing two rectangles on a form at run time, one with a solid fill and the other with a cross-hatched pattern.
               In the Visual Basic 6.0 example, the FillColor and FillStyle properties are used along with the Line method. Calling the Line method with the B parameter draws a rectangle.
Ex :
Open a new standard EXE project and the Form
Enter the following code in the Form_Paint() procedure
                              Private Sub Form_Paint()
                                                            ' Draw a solid red rectangle.
                                     FillColor = vbRed
                                     FillStyle = vbSolid
                                           Line (100, 100)- (1000, 500), vbRed, B
                                                           ' Draw a rectangle filled with a crosshatch pattern.
                                     FillColor = vbGreen
                                     FillStyle = vbCross
                                            Line (100, 500)- (1000, 1000), vbGreen, B
                              End Sub
Now execute the program by pressing F5.Hence the output is shown below.

                   

5. Displaying an Image on a Form with a text and Determining the Color of a Single Pixel :
                   For displaying an image on a form at run time  we use LoadPicture and for displaying a text string on a form at run time uses the Print method. Graphics method for determining the color of a pixel at a specified location in an image on a form at run time, and then drawing a rectangle filled with that color uses the PixelColor and Point method to retrieve the color value.
            Ex :
Open a new standard EXE project and the Form
Place a Picturebox on the form and change the Autosize property to “True ”.
Enter the following code in the Form_Paint() procedure
                              Private Sub Form_Paint()
                          Dim PixelColor As Long
                                                     ‘for loading a picture at runtime                        
                                  Set Picture1.Picture = LoadPicture("D:\TUTORIALS\sample                                                   pictures\picture\pictures\TWEETY1.gif")
                                  PixelColor = Picture1.Point(1000, 1000)
                                  DrawWidth = 5
                                  FillColor = PixelColor
                                  Line (4000, 3000)-(700, 700), PixelColor, B
                                                ‘for displaying the text                      
                                 Me.Font.Size = 24
                                 Me.Font.Bold = True
                                 Me.ForeColor = vbRed
                                  Print "Hai welcome !"
                                       End Sub
 Now execute the program by pressing F5.Hence the output is shown below.

                         
6. Moving of an Image :
An image can be moved from one position to another position using the top, left and the state of the image.
               Ex :
Open a new standard EXE project and the Form
Place a command button and an image control on the Form. In properties window change the stretch property to True and place an image.
Enter the following code in the command1_click()
                              Private Sub Command1_Click()
                                      Dim counter As Integer
                                      For counter = 1 To 10 Step 1
                                      Image1.Top = Image1.Top - 20
                                      Image1.Left = Image1.Left - 20
                                      Next counter
                              End Sub
Enter the following code in Form_Load() event
Private Sub Form_Load()
           Form1.WindowState = 2
End Sub
 Now execute the program by pressing F5.Hence the output is shown below.
   

FlexGrid
FlexGrid :
   A MS FlexGrid control is used to create applications that present information in rows and columns. It displays information in cells. A cell is a location in the MSFlexGrid at which a row and a column intersect.
The MSFlexGrid control displays and operates on tabular data. It allows complete flexibility to sort, merge and format tables containing strings and pictures. When bound to a Data control, it displays a read-only data.
We can place text, or a picture, or both in any cell. The Row and Col properties specify the current cell. We can specify the current cell in code, or the user can change it at run-time. If a cell’s text is too long to be displayed in the cell then the WordWrap property is set to True, the text wraps to the next line within the same cell. We need to increase the cell’s column width (ColWidth property) and row height(RowHeight property) to display the wrapped text.
Types :
Two kinds of rows and columns are created in MSFlexGrid control.
They are :        
Fixed : A fixed row or column does not scroll at any time. These are generally  used for displaying headings.
Non-Fixed : A non-fixed row or column scrolls when the scroll bars are active.
Rows and Columns are created by setting the four properties of the MSFlexGrid control as Rows, Cols, FixedRows, and FixedCols.
            Ex :
Open a new standard EXE project and the Form.
To add a FlexGrid control on the Form, Choose Components from the Project menu and put a check on the Microsoft FlexGrid Control 6.0 as shown below.
 
Click on Apply and then Ok. Then a FlexGrid control is added to the toolbox.
Design a Form as shown below.
Place three Labels, two comboboxes, one textbox, one commandbutton and a FlexGrid control on the form.

In properties window change the MSFlexGrid  name as itmdet and change the rows as 8 and cols as 4.
In the general declarations section of the form, enter the following code :
                                      Dim arr(7) As String
                                      Dim itm(3) As String
                                      Dim i As Integer
Enter the following code in command1_Click() :
                        Private Sub Command1_Click()
                                     itmdet.Row = Combo1.ListIndex + 1
                                     itmdet.Col = Combo2.ListIndex + 1
                                     itmdet.Text = Str(Val(itmdet.Text) + Val(Text1.Text))
                         End Sub
Enter the following code in Form_Load event :
                        Private Sub Form_Load()
                               itm(0) = "pens"
                               itm(1) = "pencils"
                               itm(2) = "erasers"
                               arr(0) = "sunday"
                               arr(1) = "monday"
                               arr(2) = "tuesday"
                               arr(3) = "wednesday"
                               arr(4) = "thursday"
                               arr(5) = "friday"
                               arr(6) = "saturday"
                           

                              itmdet.Row = 0
                              For i = 0 To 2
                                      itmdet.Col = i + 1
                                      itmdet.Text = arr(i)
                                     Combo2.AddItem arr(i)
                              Next
                           
                              itmdet.Col = 0
                              For i = 0 To 2
                                      itmdet.Row = i + 1
                                      itmdet.Text = arr(i)
                                     Combo1.AddItem arr(i)
                               Next
                        End Sub      
                         

No comments: