Programming language Visual Basic 6 (Chapter 12 - Using Graphics (Part III))

Graphics Methods

While Graphical Controls like Shape, Line gives us the design drawing at the  Graphics Methods  to draw these things at run-time. We can also put individual spots (pixels) or copy a Picture from one place to another.

Just a bit of experience you can do animation (animation) or create amazing visual effects without having to touch the  Windows API (Application Programming Interface)  to use the  BitBlt function .

Method PaintPicture

PaintPicture method  allows you to quickly copy a block of data graphics, literally, a region in a graphic image on a form, PictureBox, or Printer to another place. For example, you copy an image from one place to another in the form, or from form / PictureBox to the Printer Object for a while after you print it out.

Please start a new VB6 project and DoubleClick on a PictureBox Icon in the Toolbox to place a PictureBox on a form. PictureBox named was  picGraphic  and set its Visible property to False so that we do not see it at run-time.

Now load a picture into the Picture property of a Bitmap picGraphic Browse by file from the Properties window. Here we choose  INTL_NO.BMP  from the folder  \ Program Files \ Microsoft Visual Studio \ Common \ Graphics \ Bitmaps \ Assorted

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (661x454)

In this program we want everyone when pressing the left button of mouse down and move the mouse cursor when the cursor is where they go, the INTL_NO are drawn to it.

We will use a  Flag  to mark the button-left-of-Mouse-Down, named  flgMouseDown . Upon receiving themouseDown event  is set flgMouseDown the  True , and when receiving  Event MouseUp flgMouseDown I reset to  False . Every time you receive MouseMove Event is True, then if we will PaintPicture flgMouseDown INTL_NO.

To clear the form's background, we add a button to run the graphic method nameCmdClearForm Cls. Below is a sample code listing:

' Flag that indicates that the Mouse's left button is depressed
Dim flgMouseDown As Boolean 

Private Sub Form_Load() 
   ' Initialise flgMouseDown to False
   flgMouseDown = False 
End Sub 

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 
   ' Set Flag flgMouseDown 
   flgMouseDown = True 
End Sub 

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 
   ' Paint picGraphic if flgMouseDown is True
   If flgMouseDown Then 
      ' Paint full-size picGraphic at Mouse cursor location
      PaintPicture picGraphic.Picture, X, Y, picGraphic.Width, picGraphic.Height 
   End If 
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 
   ' Reset Flag flgMouseDown 
   flgMouseDown = False 
End Sub 

Private Sub CmdClearForm_Click() 
   ' Clear the form
End Sub 
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (517x352)

Note that you must declare variable  flgMouseDown  outside to any Sub Subs are found and can use it.For more details on how to use the  method PaintPicture , the VB6 IDE DoubleClick up words in the code editor PaintPicture to highlight text and press the button that  F1 .

You can  download the program  here.

Method PSet

We use the  method PSet  (from text  Point Set ) to draw a pixel on the form. PSet we need to know where and with what color, that is we give it coordinates X, Y of pixels and a color from the  RGB function.

Here is the code to draw a full color pixels to form a real long way (randomly) on the location and color when the user clicks on the form:

Private Sub Form_Click() 
   Dim i As Integer 
   ' Variables for pixel coordinates
   Dim iXCoord As Integer 
   Dim iYCoord As Integer 
   ' Variable for primary colours
   Dim iRed As Integer 
   Dim iGreen As Integer 
   Dim iBlue As Integer 
   ' Start the Random number generation
   ' Plot 2000 dots randomly
   For i = 1 To 2000 
      ' get a random XCoord.
      ' Note that Rnd(1) returns a real number between 0 and 1, eg: 0.384
      iXCoord = Int(Rnd(1) * ScaleWidth) 
      ' get a random YCoord.
      iYCoord = Int(Rnd(1) * ScaleHeight) 
      ' Get a random number between 0 and 254 for each primary colour
      iRed = Int(Rnd(1) * 255) 
      iGreen = Int(Rnd(1) * 255) 
      iBlue = Int(Rnd(1) * 255) 
      ' Plot the pixel at iXCoord,iYCoord
      PSet (iXCoord, iYCoord), RGB(iRed, iGreen, iBlue) 
   MsgBox ("All done!") 
End Sub 
In the above example we use the  Randomize method  to generate the memory available in any real numbers from 0 to about 0999. Then, every time you call  Rnd Function (1)  is that it will return a real number taken from the number by any method as long as Randomize generated. Therefore,  Rnd (1) * ScaleWidth  will give us a real number has values from 0 to ScaleWidth. Want to change it to Integer real numbers, we use the  function Int .

When you start the program and click on the form we will have pictures like this:
Tip : To clear a spot back in place you Pset Dom was one of the same color as the BackColor of the form.

You can  download the program  here.

Method Line

Line method to  draw a straight line from these coordinates to coordinates in a color specified by us. Line with two methods PSet and we can do many things. Want for example a mobile object, he would have deleted it by drawing with the same color of the BackColor of the form, then he would draw in the new location. Want to draw a polygon, such as triangles or rectangles we assemble together multiple lines, each line is the beginning of the end of the line is drawn just before. Shade want to paint inside a rectangle we use PSet etc. ..

There are three ways to specify the coordinates of two ends of a line we want to draw:
  1. Indicate the beginning and end coordinates of the line:
    home du:  Line (50, 100) - (3000, 4000)
    When this road is finished, the location of the graphic cursor position coordinates of the end of the road, ie CurrentX CurrentY = 4000 = 3000 and in this case.
  2. Only know the coordinates for the last line:
    thí dụ: Line -(3600, 4500), vbMagenta
    In this case the location of the graphic cursor (CurrentX, CurrentY) is taken as the coordinates of a line drawing. Ie if the line before execute this code and CurrentY CurrentX = 3000 = 4000 lines of code are equivalent:
    Line (3000.4000) - (3600.4500) vbMagenta
  3. Use the word  Step  to tell the difference between CurrentX and CurrentY:
    thí dụ: Line Step(400, 600)-Step(800, -500), vbGreen
    If this line of code before you execute CurrentX CurrentY = 4500 = 3600 and the equivalent lines of code:
    Line (4000,5100)-(4800,4600), vbGreen
In the example below, a triangle drawn with two different coding. When trying to run the program, please click turn  mode CHANGES Triangle I  and  Triangle II mode CHANGES  to see how to draw both are exactly the same, just different colors only.

Private Sub CmdTrianI_Click() 
   ' Drawing a black triangle: METHOD I
   Line (700, 500)-(2800, 2400) 
   Line (2800, 2400)-(1800, 900) 
   Line (1800, 900)-(700, 500) 
End Sub 

Private Sub CmdTrianII_Click() 
   ' Drawing a red triangle: METHOD II
   ' Draw a red line from Location(700, 500) to Location (2800, 24000)
   Line (700, 500)-(2800, 2400), vbRed 
   ' Draw a red line from Location(2800,2400) to Location (1800,900)
   Line -(1800, 900), vbRed 
   ' Draw a red line from Location(1800,900) to Location (700,500)
   Line -(700, 500), vbRed 
End Sub
To draw a rectangle, the most convenient way is to use the step as below:

Private Sub Rectangle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) 
   ' Draw a rectangle
   Line (X1, Y1)-(X2, Y1) 
   Line -(X2, Y2) 
   Line -(X1, Y2) 
   Line -(X1, Y1) 
End Sub 
We can draw a rectangle with rounded corners as follows:

Private Sub RoundCornerRectangle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) 
   Const Delta = 50 
   ' Draw a rectangle with round corner
   Line (X1 + Delta, Y1)-(X2 - Delta, Y1) 
   Line -Step(Delta, Delta) 
   Line -(X2, Y2 - Delta) 
   Line -Step(-Delta, Delta) 
   Line -(X1 + Delta, Y2) 
   Line -Step(-Delta, -Delta) 
   Line -(X1, Y1 + Delta) 
   Line -Step(Delta, -Delta) 
End Sub 
Shade can also be painted inside the rectangle by using the method PSet to mark the spots separated by about 50 pixels as follows:

Private Sub Shade(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) 
   ' Shade a roundcorner rectangle by plotting dots using method Pset
   Const Delta = 50 
   Dim i As Integer 
   Dim j As Integer 
   ' Make sure that X1 is less than X2
   ' Swap values of X1, X2 if X1 > X2
   If X2 < X1 Then 
      Temp = X1 
      X1 = X2 
      X2 = Temp 
   End If 
   ' Make sure that Y1 is less than Y2
   ' Swap values of Y1, Y2 if Y1 > Y2
   If Y2 < Y1 Then 
      Temp = Y1 
      Y1 = Y2 
      Y2 = Temp 
   End If 
   ' Plotting dots inside the rectangle at 50 pixels apart
   For i = X1 + Delta To X2 - Delta Step 50 
      For j = Y1 + Delta To Y2 - Delta Step 50 
         PSet (i, j) 
End Sub 
Shade darker want, you can put the spots are closer together, for example separated by 30 pixels instead of 50 pixels. Another way is to increase the value of  DrawWidth , the thickness of the drawn line or spots.

Now coordinated by drawing a rectangle with the above method and Shade  Print method  we can write text inside a frame pale as follows:

Private Sub CmdDrawFrame_Click() 
   Dim X1 As Integer 
   Dim Y1 As Integer 
   Dim X2 As Integer 
   Dim Y2 As Integer 
   ' Initialise Coordinates of rectangle
   X1 = 4200: Y1 = 1000 
   X2 = 6200: Y2 = 2000 
   ' Draw a roundcorner rectangle
   RoundCornerRectangle X1, Y1, X2, Y2 
   ' Shade the rectangle
   Shade X1, Y1, X2, Y2 
   ' Position cursor to Print some text
   CurrentX = X1 + 50 
   CurrentY = Y1 + 50 
   ' Define Font Size
   Font.Size = 18 
   ' Print the text at cursor location
   Print "Hello there!" 
End Sub 
When you run this program and click all buttons on the form, you will be shown below:
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (585x325)
Remember to set property to True to form AutoDraw of the graphic drawing program is not lost when the user minimises form.

You can also use the above techniques with the Printer Object to print the paper form filling details.

You can  download the program  here.

Method Circle

We use the  Circle method  to draw circles, ovals and road provision, with the inside painted empty or filled with a color you specify. We have to know the coordinates of the center circle and its radius.

Please start a new VB6 project, set to form a button with the name  frmCircle  and caption  & Circle Lines . DoubleClick on that button and write the following code:

Private Sub CmdCircleLine_Click() 
   ' Draw a circle centered at 2000,1500 with radius equal 800
   Circle (2000, 1500), 800 
   ' Draw a vertical line from center
   Line (2000, 1500)-Step(0, 800) 
   ' Draw a horizontal line from center
   Line (2000, 1500)-Step(800, 0) 
End Sub 
Now place a button on another form called  CmdArc  and caption  Draw Arc . Instead of drawing a single circle, we'll just draw a red arc.

To specify that we will draw from any location on the circle to another location, for example from 45do to 230do, we need to change the unit of Radian degree by using  Function Rads  as follows:

Private Function Rads(ByVal Degree As Single) As Single 
   ' Convert Degrees to Radian
   Const PI = 22 / 7 
   Rads = Degree / 180 * PI 
End Function 
Arcs are always drawn counter clockwise. Here is the code to draw a red arc radius 800, the focus in (4000, 2000), from 45do to 230do:

Private Sub CmdArc_Click() 
   Circle (4000, 2000), 800, vbRed, Rads(45), Rads(230) 
End Sub 
We can paint inside the circle, or Pie Slices (part of the circle) set by FillStyle  of 0 and specify color FillColor . A Slice Pie is an arc closed by two lines of glass at both ends of the radius. I want to draw a Pie Slice fighting for the minus (" - ") before the two values Radian, ie, with  -Rads (45),-Rads (230)  instead of  Rads (45), Rads (230) .

Here is the code draw two Pie Slices, which offset each other a little focus, and annotate 87.5% and 12.5%.

Private Sub CmdPie_Click() 
   FillStyle = 0  ' Fill inside any closed shaped
   FillColor = vbYellow 
   ' Draw a Pie Slice from 90deg to 45deg in Yellow
   Circle (3000, 4000), 800, , -Rads(90), -Rads(45) 
   ' Position the graphic cursor to Print some text
   CurrentX = 2800: CurrentY = 4400 
   Print "87.5%" 
   FillColor = vbBlue 
   ' Draw a Pie Slice from 45deg to 90deg in Blue
   Circle (3050, 3900), 800, , -Rads(45), -Rads(90) 
   ' Position the graphic cursor to Print some text
   CurrentX = 3400: CurrentY = 3000 
   Print "12.5%" 
   FillStyle = 1  ' No fill
End Sub 
Using the last of the Circle method to draw an  oval (Elllipse) . Draw an oval like drawing a circle, but we need to add a parameter called  Aspect . Aspect is the relation between the radius of vertical and horizontal radius. Aspect = 2 For example, if the height of the double oval horizontally, in contrast, if Aspect = 0.5, the width will double the height.

Here is the code I used to draw two oval the same size, a vertical purple and a green landscape.

Private Sub CmdEllipse_Click () Circle (1400, 3000), 800, vbMagenta,,, 2 Circle (1400, 3000), 800, vbBlue,,, 0.5 End Sub 
If you start the program and click the four buttons you'll see the following:

You can  download the program  here.

Property DrawMode

Usually when I draw, default value of the  property DrawMode  is  13 - Copy Pen . There are a number of DrawMode very suitable for application of animation is  7 - XOR Pen . To delete a picture I just recently finished them in redrawing the DrawMode XOR pen, no matter how the previous background, it will appear again.
(Video Games)


Post a Comment