|
-
Aug 13th, 2004, 04:34 AM
#1
Polygons, pboxes & image controls [RESOLVED]
I would like to have an irregular polygonal shape sitting on a picturebox, pbox1.
As only straight lines, ellipses and rectangles are allowed for the Shape control I have to draw the polygon on another pbox2 with transparent background and place it on pbox1.
Now I'd like to have the option to hide the polygon. I don't mean setting or clearing the pbox2.visible property. Rather I'd like it to be visible if the first pbox has nothing on it but if, for example, I draw something on pbox1, say a straight line going through pbox2, then I'd like to select whether this line should be in front or behind pbox2.
What's the best approach for this? Is a image control best suited?
Last edited by krtxmrtz; Aug 13th, 2004 at 09:10 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 05:01 AM
#2
The best approach depends on the purpose of doing it.
Is it just one shape, with just one potential "line" over it, or are there multiple shapes/lines?
-
Aug 13th, 2004, 05:51 AM
#3
Originally posted by si_the_geek
The best approach depends on the purpose of doing it.
Is it just one shape, with just one potential "line" over it, or are there multiple shapes/lines?
There is a number of rectangular shapes moving around, but they are not shape controls, instead they are drawn with the line method.
Some of these rectangles can cover the polygon in part or totally and I'd like the polygon to either be covered by the rectangles or the other way round, the rectangles covered by the polygon.
Maybe I should have used an array of shape controls in the beginning, but if I do it now this would implicate a complete rewriting of most of the code.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 06:05 AM
#4
there are several API's to work with polygons, which will work in a similar way to the Line command (once you have created the points).
here's an example that draws a simple one (for a more complex one just add more points to the array):
VB Code:
Private Type COORD
x As Long
y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Private Sub Form_Paint()
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
Me.Cls
' Number of vertices in polygon.
NumCoords = 3
' Set scalemode to pixels to set up points of triangle.
Me.ScaleMode = vbPixels
' Assign values to points.
poly(1).x = Form1.ScaleWidth / 2
poly(1).y = Form1.ScaleHeight / 2
poly(2).x = Form1.ScaleWidth / 4
poly(2).y = 3 * Form1.ScaleHeight / 4
poly(3).x = 3 * Form1.ScaleWidth / 4
poly(3).y = 3 * Form1.ScaleHeight / 4
' Polygon function creates unfilled polygon on screen.
' Remark FillRgn statement to see results.
Polygon Me.hdc, poly(1), NumCoords
' Gets stock black brush.
hBrush = GetStockObject(BLACKBRUSH)
' Creates region to fill with color.
hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
' If the creation of the region was successful then color.
If hRgn Then FillRgn Me.hdc, hRgn, hBrush
DeleteObject hRgn
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
-
Aug 13th, 2004, 06:42 AM
#5
If I use this to draw polygons, whether on a form or on a pbox, then any line method I use thereafter is ignored.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 06:57 AM
#6
That's probably because the example uses the Form_Paint event and Me.CLS at the start.
I removed the Me.CLS, and didnt have the problem 
(but as the polygon is set to a percentage of form size, it creates unwanted effects - if you use pre-detemined co-ordinates this wont be an issue)
-
Aug 13th, 2004, 07:27 AM
#7
What I meant was, if I place a statement such as,
Me.Line (0.2 * Me.Width, 0.5 * Me.Height)-(0.8 * Me.Width, 0.8 * Me.Height), vbRed, BF
before the poly drawing code then a red rectangle is drawn behind the poly. But if the statement is placed following the poly drawing code, THEN it's ignored.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 07:48 AM
#8
I've had an idea but it needs some elaborating. It's based in drawing a polygon both on a picturebox and on a image control. As the latter is not possible, then I've got to (at run time):
1. Use the polygon coordinates to draw and fill the polygon on the picturebox.
2. Save that image to disk.
3. Load the image on the image control. (Is it possible to do this at runtime?)
4. Depending on whether the polygon must be shown at the front or at the back of the image drawn on the container picturebox (referred to above as pbox1), play around with the visible properties of the pbox and img control to show either one at convenience.
The picture in pbox2 will need its background transparent, but I think I can solve this.
Can you see any flaw in this scheme? Do you have a better idea?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 08:02 AM
#9
Originally posted by krtxmrtz
What I meant was ...
I know, I forgot to mention that (assuming your scalemode is not pixels) this line should be removed: "Me.ScaleMode = vbPixels" (and then change the ploy co-ordinates accordingly).
-
Aug 13th, 2004, 08:16 AM
#10
Originally posted by si_the_geek
I know, I forgot to mention that (assuming your scalemode is not pixels) this line should be removed: "Me.ScaleMode = vbPixels" (and then change the ploy co-ordinates accordingly).
Yep, it's working now. So maybe I won't need to work on the idea I've just described above. Still I'd like to know if it looks sound.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 08:20 AM
#11
erm... I dont like it! There is no need to load/save files just to draw a picture, I would stick to just drawing it
-
Aug 13th, 2004, 08:37 AM
#12
OK, I'll buy your code
Further question: can you have the poly border drawn with one color and the interior filled with another? In other words, does the GetStockObject provide the means or is there some other function that could help?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 13th, 2004, 08:49 AM
#13
I'm sure there is at least one way to do it, but I don't know what it is (I don't do much drawing!). As the FillRgn fills the enitre polygon you should be able to just fill first, then draw the lines (setting the colour of the brush/pen or whatever it is first).
I'd recommend searching this forum for other examples, not doubt there will be a few threads that contain Polygon and FillRgn.
-
Aug 13th, 2004, 09:48 AM
#14
I've just realized another reason not to use an image control: it is constantly repainted as the drawing in the container picturebox dynamically changes, in one word, flickering
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|