From the pull-down list, select the Paint event as shown below if you are using Visual Studio the events are accessed by clicking on the lightning bolt above the properties list :. Once selected, Visual Studio will display the Paint event subroutine. The next task is to create a paintbrush with which to paint:. In the above code we have created a new SolidBrush drawing object using color blue and assigned it to variable blueBrush.
The next step is to use the brush to draw a shape:. Press ' F5 to build and run the application which should appear as follows:. Visual Basic provides a wide range of brush effects. Using the line and shape controls to draw graphics will only enable you to create a simple design. In order to improve the look of the interface, you need to put in images and pictures of your own.
Fortunately, there are two very powerful graphics tools you can use in Visual Basic 6 which are the image box and the picture box. To load a picture or image into an image box or a picture box, you can click on the picture property in the properties window to launch a dialog box that will prompt you to select a certain picture file.
You can also load a picture at runtime by using the LoadPictrure method. The syntax is. This program uses the Rnd function to generate random integers and then uses the LoadPicture method to load different pictures into the image boxes using the If…Then…Statements based on the random numbers generated. Other than using the line and shape controls to draw graphics on the form, you can also use the Pset, Line and Circle methods to draw graphics on the form.
Graphics 'Set the composite quality and smoothing mode of the surface. Red, , , 80, 80 'Dispose of objects greenBrush. Dispose 'blackPen. Dispose ; redPen. Dispose g. NET Framework, the garbage collector is responsible for managing resources associated with an object. When you dispose of an object, the garbage collector collects the object right away and frees all the resources associated with that object.
If you don't dispose of an object, the garbage collector will keep track of the objects, and if an object is not used for a certain amount of time, it will dispose of it automatically. It is always best programming practice to dispose of any object that you create explicitly using the new operator. Building and Running the application The final step in creating an application is to build and run it. To do this, in Visual Studio. The output of the application looks like Figure 2.
The application draw a line, a rectangle, and some ellipses with different with different colors. The first four lines here are concerned with restricting the xUp and yUp coordinates to a point within the bounding rectangle of the drawing surface.
The next two lines call the Math. Abs function to set absolute values of intW and intH respectively saving us the bother of having to work out whether to subtract xUp from xDown , or the other way round, and so on. The next line of code determines how big the square will be when it is drawn. Since it is a square, then by definition the sides will be equal in length. We will somewhat arbitrarily limit the width and height to whichever is the shorter of intW and intH.
The square will be drawn within a bounding rectangle or bounding square, in this case that extends left or right, up or down from the xDown and yDown coordinates, depending on whether the user drags the mouse left or right, up or down, after they have pressed the mouse button. The last two lines of code determine where the top left hand corner of the bounding rectangle should be this will have the x and y coordinates intL and intT respectively. The first six lines of code are identical to the previous procedure.
The only real differences are that the line of code that reduces the value of intW to that of intH if the latter is smaller in the dimSquare procedure has been removed, and that the final line of code uses intH instead of intW to determine the y coordinate for the top of the bounding rectangle.
The first six lines of the subroutine are identical to those used in the subroutines used for the square and the rectangle advocates of re-usable code will be straining at the leash to remove this code to a separate subroutine - feel free to optimise the code! We are only going to use one of the dimensions intW to draw the circle, so the next line of code checks to see if intW is smaller than intH.
If so the value of intW is increased to match the value of intH. This dimension represents the circle's radius, so the coordinates for the top left-hand corner of the bounding rectangle will be intW pixels to the left of the point defined by the xDown and yDown coordinates, and the same distance above it. The penultimate two lines of code subtract intW from xDown and yDown to find intL and intY respectively. The last line of code doubles the value of intW , so that the bounding rectangle again, read bounding square for the circle extends for the same distance on either side, and above and below, the point defined as the centre of the circle xDown , yDown.
The only real difference between dimCircle and dimEllipse are that dimEllipse uses the values of both the intW and intH variables to determine the position of the top left-hand corner of the bounding rectangle and its size. Note that the dimensioning of shapes works the same regardless of whether the shapes are outlined or filled. The real difference between the two types of shape is in the methods used to draw them by the Graphics object.
The next piece of code we need to write is the MouseUp event handler for the picCanvas control, which will enable us to demonstrate the various shape tool methods. Text G. DrawString strText, New System. Here we see the tools used to draw shapes - the SolidBrush and the Pen.
The SolidBrush is used to draw the filled shapes, while the Pen is used to draw lines and outline shapes. Both accept a Color argument, which is set to clrSelected at the beginning of the procedure. The Pen method also accepts a Width argument that determines the thickness of lines or the borders of any outline shapes drawn.
In our MouseUp procedure, we use the value of intPenWidth. Although they have default values, both clrSelected and intPenWidth can be set by the user at run time. The next three lines of code set the drawFlag variable to False signalling that no drawing tools are currently active , and set the xUp and yUp variables to the x and y coordinates relative to the drawing surface of the point at which the mouse button was released.
The remaining code mostly consists of a Select Case statement that checks to see if a drawing tool is currently selected, and if so takes the appropriate action. If the Line tool has been selected, the Drawline method of the Graphics object draws the line with the penLine object which, if you remember, has been set to the currently selected colour and line width between the coordinates defined by xDown , yDown , and xUp , yUp.
If the Text tool is selected, whatever text is currently in the txtInsertText control's Text property if any is drawn using the font face, font style, and font size selected by the user, starting at the position defined by xUp , yUp.
If a shape tool is selected then - depending on which shape tool has been chosen - the appropriate subroutine dimSquare , dimRectangle , dimCircle or dimEllipse will be called to set the bounding rectangle.
For outline squares or rectangles, the Graphic object's DrawRectangle method is used with the penLine object, the only difference being that the width and height dimensions used for drawing a square will be identical, whereas those used for drawing a rectangle will usually be different.
For solid squares or rectangles, the method used will be FillRectangle and the object used to draw will be brushFill. Circles and ellipses follow a similar patter, with the DrawEllipse method used for the outline shapes and FillEllipse used for their solid counterparts. The final line of code calls the Graphic object's Refresh method to make sure the changes made by the program are displayed to the screen immediately. OK Then picCanvas.
Save sfdSavePic. FileName, Drawing. Bmp MsgBox "File saved. This code uses the sfdSavePic SaveFileDialog object that we added to the form earlier, and sets up a filter that will only allow the user to choose the bitmap file format.
It will also add the ". What is actually being saved here is the picCanvas PictureBox control's Image , which is a bitmap object, and to which all our graphics commands draw. To complete the application, there is one more very trivial task:. Net A VB. Net Paint Program Visual Basic. Open a new project called "EasyPaint". In the Solution Explorer window, rename the file "Form1. In the control's Properties window, rename the menu strip control from " MenuStrip1 " to " mnuEasyPaint ".
Click on the menu strip where it says "Type Here" and type the word " File " to create the application's File menu. As you type, you will see a second text box appear below and slightly to the right of the first one. In this box, type the word " New " to add this item to the application's File menu.
Repeat the previous step two more times to add the items " Save " and " Exit " to the File menu don't worry about the code for these menu items just yet - we will be adding it later. In the toolbox, expand the Dialogs pane and double-click the FontDialog item. When when the font dialog control appears below your form, rename it from " FontDialog1 " to " dlgFont ".
0コメント