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

Print screen

VB6 is our method for printing Print directly on the Form, PictureBox, or Printer. Threetypes of control is seen as the canvases that the artist painted.

Please start a new VB6 program. Set up a PictureBox named Picture1 and form a button with the name CmdPrintTenLines Caption  Print Ten Lines . DoubleClick on this button and write code below:

Private Sub CmdPrintTenLines_Click() 
   Dim i As Integer 
   ' String variable used for display
   Dim strLine As String 
   ' Write 10 lines, one under the other
   For i = 1 To 10 
      strLine = "This is line " & CStr(i) 
      Me.Print strLine  ' Print on Form
      Picture1.Print strLine  ' Print on Picture1
End Sub 
You run the test program and then click  Print Ten Lines . In this case we use the default font and color to print 10 rows. After each print, the program automatically relegated.

Next, add a button with the name CmdPrintFontSizes Caption Print Font Sizes.DoubleClick on this button and write code below

Private Sub CmdPrintFontSizes_Click() 
   Dim i As Integer 
   ' Print numbers 1 to 10, one after the other on the same line
   For i = 1 To 10 
      ' Define Font size
      Me.Font.Size = Me.Font.Size + i 
      ' Define Color using Function QBColor
      Me.ForeColor = QBColor(i) 
      ' Print without moving to next line. Note the semicolon ";"
      Me.Print Str(i); 
End Sub 
In Sub CmdPrintFontSizes_Click, we change the font size for the numbers to be printed out and grow up gradually change the color of the numbers by using the  function QBColor . To print the numbers down a continuous row we use the Print method with the semicolon ( ; ). You can run the program again. Click the Print Ten Lines  and then click  Print Font Sizes , the results will look like this:

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (526x304)
Now you try to  minimize  the program window, and then restore it former size. You will see every one at this time is in the form or picture box again.

The reason is that when we print up a form or PictureBox, the picture was painted in the local graphic VB6 not counting as part of the window. Want to avoid this obstacle we have to remember ordered redrawn by VB6 set AutoRedraw property  of Picture1 to form and  True .

Coordinate system

When placing an object or draw something on screen (screen), or form. Etc. we need to specify the object that lies anywhere from (with reference to) the upper left corner (Top Left) of the screen or form.

On the left corner as the center coordinates of the screen or form. Where X and Y coordinates are equal to 0, we write is  0.0 . If I was going through to the width of the screen, the X coordinates increase. If we go along the bottom in the height of the screen, the Y coordinates increase.

Next is our unit of measurement used to represent distance. In the previous article we spoke of fineness of the screen (screen resolution) based on the  pixel . We can use the pixels to say an Object with coordinates X and Y each way how many pixels from the center coordinates.

Thus, even on the same screen when we increase the fineness it up, an object has been set up in units of pixel screen will link to the left and up a little because a pixel size smaller now than before a bit.

The following figure illustrates the size of the screen and form.
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (531x325)
Points to be aware that there are sections such as  title bar  and  border  of a form can not be drawn.Thus the remaining area of the form is called  the Client Area . Width and height of the Client Area is called ScaleWidth  and  ScaleHeight .

If desired distance from one object to the center coordinates, or the size of the object, does not change whether we can increase or decrease in the fineness of the screen or printed with a printer (for example, we want it always 5cm long for example), we used coordinate system in units of  twips  of the form.

The Default Coordinate System Twips of VB6. In this system a point is equal to 1 / 567 centimeter.Therefore, if you draw a long way it displays 567 twips 1 cm long on the screen, and when you print it out, it is 1cm long on paper. Ie the length of the object is not dependent on the type of screen (high or low fineness) or printer. People say it is the  Device independent  coordinate system (coordinate system independent of the instrument). In other words Twips you really  what you see is what you get (WYSIWYG - why has it found) , very suitable for desktop publishing.

You can change the coordinate system of an edit form by  ScaleMode property  through the Properties window as follows:
Note:  Changes to the numbers ScaleMode no immediate effect but only affects what is designed after.

Similar to the Edit Text in Notepad, Text Cursor (audio  |  flash fire) is the current position, where it will display the text reviews coming in have a graphic Cursor Invisible, which will show what we going to  print .I specify the location of the graphic cursor by giving it to the numbers of  CurrentX  and  CurrentY .

Please start a new VB6 project and write code for the  Resize event  of the form are as follows:

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (525x212)
Try running the program and Resize form. Every time you Resize form, the word  X  will be moved into position around the middle of the Client Area of the form.

Dùng Graphics

Had a bit of VB6 basics of graphics, we can now put the graphics on the form. There are two ways to do it:
  • Dùng Graphical Controls: Ta có PictureBox và Image có thể chứa hình ảnh. Trong khi Line vàShape có thể vẽ đường thẳng hay các hình chữ nhật, tròn .v.v.. trên form, lúc thiết kế.
  • Using  Graphics Methods : This is the command to draw directly on the form at run-time. VB6 commands us as  Cls, Pset, Point, Line  and  Circle .

Depending on the circumstances, you can choose any convenient way.

PictureBox và Image

Use a PictureBox or Image is the easiest way to display a graphic in the form. As designed, you can type the name of the graphic directly into the  Picture property  in the Properties window. Form Picture property also. You can also click on the Picture property right word to browse and select a graphic, usually a bitmap or Icon.

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (708x315)
The difference between Image and PictureBox Image as a  Stretch property  , which can be set to true to stretch the graphic to the occupied area of the image. Image is a control grapgic  lightweight (lightweight), ie it does not require much memory and runs faster PictureBox. The reason is that PictureBox is a container, ie it can contain other controls. In addition, PictureBox also allows us to draw on it like on the form.

In the picture below, while we set design and a picture box the same size image on the same form. Then we assign the same picture of  happy.bmp  for both. Particularly with the Image, we set the propertyStretch  it out  True .
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (616x338)

Specify at run-time image

While the program is running, we can change the image contained in a PictureBox or Image using LoadPicture Function . Remember we can not assign directly to the two graphical controls Property Picture this. Property Picture reason is just a friendly way for us to specify a graphic designer while. When an image has been assigned then, VB6 form that contains all the files have the same name but with extension of the form file  . frx . Ie if the form is named  Form1  is contained graphic Picture of Property in conjunction with other graphics file of the form in  Form1.frx .

Therefore, because VB6 program always contains graphic with it, we need not mention the name of the graphic file to use or deploy, ie no need to attach graphic file name in the Setup file to install it. Below is a sample code to load at run-time is a graphic named  sad.bmp  in the subfolder named  images  into the Image control's name App.path image1.

Private Sub CmdLoad_Click() 
   Dim LocalDir As String 
   ' Assign Folder where program resides to LocalDir
   LocalDir = App.Path 
   ' Append right backslash if last character is not "\"
   If Right(LocalDir, 1) <> "\" Then 
      LocalDir = LocalDir & "\" 
   End If 
   ' Load graphic "sad.bmp" from SubFolder "images" into Image1
   Image1.Picture = LoadPicture(LocalDir & "images\sad.bmp") 
End Sub 
Of course, if we want to load at run-time graphic must supply their own graphic files.

Control Shape

Shape Control  allows you to draw simple shapes such as lines, boxes, circles on the form, at design time. After DoubleClick Shape control in the Toolbox to add a control to shape the form, select the Shape of its kind from the Properties window and then held in a corner of a large drag on the form Shape arbitrarily small.

Want to paint inside a shape, you select a color from the FillColor property. FillColorproperty like BackColor of the other controls, but it only takes effect when the propertyFillStyle a value other than 1 - Transparent (transparent), such as 0 - Solid (thick).

Control Line

Similar to the properties of the Shape Fill, for  Line  you have the  properties BorderColor, BorderStyle, and BorderWidth . Border color of the specified color line, BorderStyle to let you select or continuous brick road marking, and BorderWidth to make bigger lines. Of these properties also apply to the perimeter (surrounding lines) of the rectangular, circular. Etc.

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

You can  download the program  here.



Post a Comment