A Little Help On A Paint Program PLZ
Author |
Message |
Weapon X
![](http://www.murauchi.com/MCJ/front/images/commodity/450/00000542450L.jpg)
|
Posted: Thu Nov 17, 2005 7:13 pm Post subject: A Little Help On A Paint Program PLZ |
|
|
hi, i m learning VB in Comp Sci. gr 11 and for an ISU i am making a paint like program. i've got most of it down except for how to make a square. like in paint, i have a square button ( the blank box with the black outline ) but it doesnt make a real box. if someone cud help me that wud be great
thanx
ps: if u change the code can u plz tell me wht was wrong.
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
frmDraw.frm |
Filesize: |
6.67 KB |
Downloaded: |
126 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Thu Nov 17, 2005 8:16 pm Post subject: (No subject) |
|
|
The main problem with your box drawing was that you had
code: | CurrentX = x2
CurrentY = y2 |
instead of
code: | x2 = CurrentX
y2 = CurrentY |
This doesn't really address the issue of the boxes being drawn weird. They are still drawn on top of each other, and I'm not entirely sure how to so it like it's done in paint.
To do a square is similar to a rectangle. Except you are going to have to use some kind of function like this:
code: | Private Function Max(Num1 As Single, Num2 As Single) As Single
If Num1 > Num2 Then
Max = Num1
Else
Max = Num2
End If
End Function |
|
|
|
|
|
![](images/spacer.gif) |
Brightguy
![](http://compsci.ca/v3/uploads/user_avatars/527435178485ad4c287538.gif)
|
Posted: Fri Nov 18, 2005 3:13 pm Post subject: Re: A Little Help On A Paint Program PLZ |
|
|
Here's a bit of code to show you how to draw rectangles on a form like in paint. I'm sure you can figure out how to work it into your program and extend it for lines, circles, etc...
VisualBASIC: | Private StartX As Single, StartY As Single 'Starting Coordinates
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
StartX = X
StartY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button <> 0 Then 'Button being pressed
Me.Cls 'Clear last box
Me.Line (StartX, StartY)-(X, Y), vbBlack, B 'Draw box
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Picture = Me.Image 'Save the new image
End Sub |
Before using this code you must set the form's AutoRedraw to True and BorderStyle to 'Fixed Single' or similar. AutoRedraw must be true so that you can use the form's persistent graphic, and if you leave the form as Sizeable your program will likely redraw far too slow, as the graphic being saved is huge (the maximum possible size of a form, I would guess).
|
|
|
|
|
![](images/spacer.gif) |
Weapon X
![](http://www.murauchi.com/MCJ/front/images/commodity/450/00000542450L.jpg)
|
Posted: Fri Nov 18, 2005 8:45 pm Post subject: a little explanation plz |
|
|
thanx alot for the help brightguy and globe totter, ur advice was perfect but brightguy the only thing i didnt get in your code was "Me.Picture = Me.Image", can u just gimme a brief description abt it plz?
thanx
|
|
|
|
|
![](images/spacer.gif) |
Brightguy
![](http://compsci.ca/v3/uploads/user_avatars/527435178485ad4c287538.gif)
|
Posted: Fri Nov 18, 2005 10:37 pm Post subject: Re: a little explanation plz |
|
|
No problem, I'll give you the whole story...
As you know, a form has certain paint methods that let you draw circles, lines, boxes, etc... Now, by default, whenever anything is painted on the form, it isn't saved. So, if the form has to be redrawn (e.g. if you move it off the screen and then back again) everything you painted will have disappeared. If it's important that it doesn't erase, you can manually redraw it in the Form_Paint event, or you can just set AutoRedraw to True and VB will keep track of everything painted on the form in a "persistent graphic" and automatically repaint everything whenever it needs to be repainted. (The disadvantage is that this will slow down your program.)
The form's Image property allows you to access the persistent graphic, and thus everything that's been drawn on the form. A form also has a Picture property that lets you display a background graphic on the form. So, "Me.Picture = Me.Image" sets the form's background picture to be the form's Image, and thus saves everything that's been drawn so far. If you didn't include that line, then everything would be cleared after Me.Cls, which clears all painting you've done (but not the form's background picture).
|
|
|
|
|
![](images/spacer.gif) |
Weapon X
![](http://www.murauchi.com/MCJ/front/images/commodity/450/00000542450L.jpg)
|
Posted: Fri Dec 09, 2005 11:32 pm Post subject: 2 more prbs :) |
|
|
alright, so i implememted the code u guys gave me and i got most of the program working and now there are only 3 errors with the program:
1.) the eraser only works (after u select it) if u are drawing and u move ur mouse off the form then bak on. i tried to make draw false but tht didnt work for me. so how wud i fox that?
2.) whn u r trying to draw as soon as your program starts the line u make dissappears but only works if u make a box first. i think it might be a me.picture = me.image problem but i cant pinpoint the exact problem.
3.) finally, whn u right klik and select Clear Screen, it doesnt clear screen. i tried me.image = 0 but tht didnt work.
any help wud be greatly apprecited with any of those probs, thanx
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
Paint.zip |
Filesize: |
7.63 KB |
Downloaded: |
116 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Brightguy
![](http://compsci.ca/v3/uploads/user_avatars/527435178485ad4c287538.gif)
|
Posted: Sat Dec 10, 2005 6:28 pm Post subject: Re: 2 more prbs :) |
|
|
Hmmm, well you probably want to rewrite and condense a lot of the code, but:
1.) You have draw being set to True as soon as you click on the form... this doesn't help you if the eraser was selected.
2.) You only have AutoRedraw being set to True when you click on the box. Either set it at design time or in the Form_Load.
3.) If you want to clear the Background picture, do something like: Me.Picture = Nothing
|
|
|
|
|
![](images/spacer.gif) |
|
|