Results 1 to 14 of 14

Thread: Polygons, pboxes & image controls [RESOLVED]

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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)

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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?

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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:
    1. Private Type COORD
    2.     x As Long
    3.     y As Long
    4. End Type
    5. Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
    6. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
    7. Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
    8. Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
    9. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    10. Const ALTERNATE = 1 ' ALTERNATE and WINDING are
    11. Const WINDING = 2 ' constants for FillMode.
    12. Const BLACKBRUSH = 4 ' Constant for brush type.
    13.  
    14. Private Sub Form_Paint()
    15.     'KPD-Team 1999
    16.     'URL: [url]http://www.allapi.net/[/url]
    17.     'E-Mail: [email][email protected][/email]
    18.     Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
    19.     Me.Cls
    20.     ' Number of vertices in polygon.
    21.     NumCoords = 3
    22.     ' Set scalemode to pixels to set up points of triangle.
    23.     Me.ScaleMode = vbPixels
    24.     ' Assign values to points.
    25.     poly(1).x = Form1.ScaleWidth / 2
    26.     poly(1).y = Form1.ScaleHeight / 2
    27.     poly(2).x = Form1.ScaleWidth / 4
    28.     poly(2).y = 3 * Form1.ScaleHeight / 4
    29.     poly(3).x = 3 * Form1.ScaleWidth / 4
    30.     poly(3).y = 3 * Form1.ScaleHeight / 4
    31.     ' Polygon function creates unfilled polygon on screen.
    32.     ' Remark FillRgn statement to see results.
    33.     Polygon Me.hdc, poly(1), NumCoords
    34.     ' Gets stock black brush.
    35.     hBrush = GetStockObject(BLACKBRUSH)
    36.     ' Creates region to fill with color.
    37.     hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    38.     ' If the creation of the region was successful then color.
    39.     If hRgn Then FillRgn Me.hdc, hRgn, hBrush
    40.     DeleteObject hRgn
    41. End Sub
    42. Private Sub Form_Resize()
    43.     Form_Paint
    44. End Sub

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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)

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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).

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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

  12. #12

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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)

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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.

  14. #14

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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
  •  



Click Here to Expand Forum to Full Width