This tutorial will help you create a paint program with basic tools like lines, circles, brushes and color choices. These tools are quite similar so after getting a lot of help with the first one, you will be able to do the others with less and less help - a nice way to ease into Delphi programming. Create a folder named "Paint" for this project, possibly in the "My Documents" folder on your computer. Of course you can name it anything you like, but I will use the name "Paint" in this article.
This should create two windows named "Form1" and "Unit1". You can switch between them quickly by pressing the F12 key. Click on the unit so it is in front of everything else. Choose File - Save As , name it "main" and direct it into your paint folder. Then choose File - Save Project As and name it "paint".
Click on the form. Press the F11 key to select the object inspector. Right under its title it should say "Form1" to indicate it is presently inspecting the details of the form. In the list below, find the item "name" and beside it change "Form1" to "MainForm".
This will help keep things organized when you have lots of forms windows and dialogs in your project. Exit from Delphi, saving if it asks. Look in your Paint folder. You should find 5 files related to this project: main. You will normally begin work on your project by doubleclicking on Paint. Click on "standard". Just below the tabs are icons of various standard components. Hold the mouse pointer over one for a second or two to see a brief description.
The first one is described as "mainmenu". Click on the "mainmenu" component, then click anywhere on your form. The mainmenu icon should appear on your form. It doesn't matter where on the form you place this particular component.
Doubleclick on your mainmenu to open it. Nothing much there so far, except a blank entry which should be selected. Press the F11 key to inspect it. Find the "name" in the object inspector's list and enter the name "FileMenu". Find the "Caption" and enter "File". Click the x icon at the upper right corner of the mainmenu window to close it. Your program should run now. Choose "Run" in the "Run" menu. After a moment of compiling, you should see your form appear with its main menu containing only the "File" item.
Of course it doesn't work yet. Click the x button to close it. Let's add an Exit item to the file menu. Doubleclick on your mainmenu object, select the blank item hanging under the word "File" and press F11 to edit it. Change the "Caption" to "Exit". The name should automatically change to "Exit1", which is as good as any. Again you have to click x to close your program. This automatically creates an exit response procedure in your main unit.
Here is where you write the code that makes your program work:. You just have to type MainForm. Close - Delphi has written the routine stuff for you automatically. The response procedure holds the detailed programming for an action like choosing a menu command.
When the program is running and someone chooses "Exit", Windows gives control to your Exit1Click procedure and follows the instruction you entered. Congratulations on getting one of your program's commands to actually work! Our program needs a set of painting tools pencil, line, brush, etc. Begin by choosing a panel component to hold all the tool buttons. On the component bar at the top or right side of the mainform, click the panel component in the "standard" tab, then click it onto your form.
Drag it wherever you like; you can always move it later. In fact, the purpose of the panel is to allow us to move all the tools as a group more easily. You can get rid of the "Panel1" caption in the middle by using the object inspector to change its caption to a blank. Let's leave the name as. Delphi has a number of different buttons that could be used for the toolbar.
The speedbutton on the "additional" tab has the advantage of being easy to put an icon on and to indicate when it is selected.
The speedbutton looks like a lightning bolt - if you are in doubt hold the cursor over each component until you see its hint. Put a speedbutton on the panel.
We need to draw a picture of a pencil to put on the speedbutton. Delphi has a little icon editor built in. I'm using Delphi 5, so what I'm seeing may not be exactly the same as what you are seeing.
In the Tools menu, choose "Image Editor". Pull down its file menu and choose new bitmap image. Make it 64 dots wide and 16 high in 16 colors. Use the pencil tool to make vertical dividing lines splitting the bitmap into 4 equal parts. Choose the magnifying glass and right click choosing zoom in a couple of times to make the 64x16 bitmap large enough to work with. Count 16 squares for each part. The first part is the normal appearance of your pencil tool.
The third part is the appearance when clicked and the fourth part is what it looks like when held down. Draw them any way you like! You can simply use a different background color for the 3rd part. Erase or cover up the black lines to the right of the 1st and 3rd parts - they are part of the pictures.
Save in your folder, using the name Pencil. Now you need to tell Delphi that you want to use Pencil. Click on the speedbutton. Press F11 to bring up the object inspector. Click on the blank to the right of "Glyph", then on the 3 dots that appear there. In the dialog that opens, choose "Load", then find pencil. Presto, your button should have your picture on it. Run your program and see what happens when you click on the pencil button.
It should change appearance. If you want to change the picture, open it with the image editor or any paint program , edit, save, then choose it again in the object inspector. It is possible to paint on the bare form, but adding an image component makes it easy to load and save the image.
On the component palette, click the "additional" tab and choose the image component. Place it on your form and adjust to the size you want. It should already be named "Image1". We need a variable to keep track of whether or not we have started using the pencil. Add the drawingNow variable near the top of your unit window, as shown below: the first 2 lines are already there; just add the 3rd line. This means it can have only two possible values, either True or False. Can someone please post sample code to do this?
Any help will be greatly appreciated. Jens Gruschel. Probably you wish to save the things you have drawn to. If you want to save the drawings you have to call your new DrawSomething. Of course you could try to get the drawings from the. You can do that, but that results in something similar to TImage with a.
Roger Lascelles. Move all of the code from this handler to a new procedure with following parameters: procedure TForm1. However, in the. Quote You can write on a TPaintBox canvas anytime you like. However, in the OnPaint handler, you must redraw the whole image, because Windows calls OnPaint when it needs to redraw the image - for example when your app has been hidden beneath another window.
Many real apps have to draw on the canvas immediately - for instance when drawing on a TPaintBox in response to a mouse movement, you draw on the canvas within the OnMouseMove handler. You are right. Peter No amount of code can make that happen by magic. The drawing code has to be able to draw to fit the size of the target canvas. The asker has to do the bit. ClipRect to find out what size to draw. So no hard coded coordinates.
All coords scaled according to ClipRect. I'm well aware that magic doesn't exists thanks for that. I just wanted you to explain to me why you think that this answer answers his question. Peter I added an extra intro paragraph. Do you understand now? Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 8. Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
0コメント