Results 1 to 13 of 13

Thread: Trapezoid and Click?

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Trapezoid and Click?

    Hello, I am trying to draw a couple of trapezoids on an OCX control... I am using the following code:

    VB Code:
    1. Private Type COORD
    2.  x As Long
    3.  y As Long
    4. End Type
    5.  
    6. Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
    7. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
    8. Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
    9. Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
    10. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    11.  
    12. Const ALTERNATE = 1 ' ALTERNATE and WINDING are
    13. Const WINDING = 2 ' constants for FillMode.
    14. Const BLACKBRUSH = 4 ' Constant for brush type.
    15. Const WHITEBRUSH = 1
    16.  
    17. Private Sub dibujaTrapecio(x As Long, y As Long, width As Long, height As Long, color As Long)
    18.  Dim iXBorder As Long
    19.  Dim iYBorder As Long
    20.  
    21.  iXBorder = 10
    22.  iYBorder = 1
    23.  
    24.  x = x - iXBorder
    25.  width = width + iXBorder
    26.  
    27.  y = y - iYBorder
    28.  height = height + iYBorder
    29.  
    30.  Dim poly(1 To 4) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
    31.  UserControl.Cls
    32.  ' Number of vertices in polygon.
    33.  NumCoords = 4
    34.  ' Set scalemode to pixels to set up points of trapezoid.
    35.  UserControl.ScaleMode = vbPixels
    36.  ' Assign values to points.
    37.  poly(1).x = x + iXBorder
    38.  poly(1).y = y
    39.  poly(2).x = x
    40.  poly(2).y = y + height
    41.  poly(3).x = x + iXBorder + width
    42.  poly(3).y = y + height
    43.  poly(4).x = x + width
    44.  poly(4).y = y
    45.  ' Polygon function creates unfilled polygon on screen.
    46.  ' Remark FillRgn statement to see results.
    47.  Polygon UserControl.hdc, poly(1), NumCoords
    48.  ' Gets stock black brush.
    49.  hBrush = GetStockObject(color)
    50.  ' Creates region to fill with color.
    51.  hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    52.  ' If the creation of the region was successful then color.
    53.  If hRgn Then FillRgn UserControl.hdc, hRgn, hBrush
    54.  DeleteObject hRgn
    55. End Sub

    (I took the code from si_geek and changed it a little for my own needs, thank you, si_geek)

    As you can imagine, I am trying to emulate the behavior Frontpage has with its different files.

    I wonder if there is a way to get the WindowBackground color for the brush... And also how could I redraw an array... Meaning I have to have something like a ZOrder... I am just starting and I would like some help.

    Thanks in advance.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Trapezoid and Click?

    Originally posted by Tec-Nico
    ...
    hBrush = GetStockObject(color)
    ...
    Hola Tec-Nico,

    I'm using code similar to yours to draw a filled polygon. Do you happen to know how it could be filled with a custom colour like, for example, black border and blue inside?

    Saludos desde el otro lado del Atlantico...
    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)

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Trapezoid and Click?

    Originally posted by Tec-Nico
    (I took the code from si_geek and changed it a little for my own needs, thank you, si_geek)
    thanks, I'm glad it helped!


    Anyway, time to see if I can help some more!

    From the title of this thread I guessed you wanted to know how to detect a click in a polygon, if that is the case try searching this site for PtInRegion, hopefully there will be some good examples/explantions.


    No ideas about the Brush questions I'm afraid - I've neve needed to use them.

    Originally posted by Tec-Nico
    And also how could I redraw an array... Meaning I have to have something like a ZOrder... .
    If you are drawing directly onto a form you can just do formname.cls, then re-draw everything (in the new order, the shapes at the "back" first).

    I think usercontrols might have the cls method too, in which case you can use the same method.
    Last edited by si_the_geek; Aug 27th, 2004 at 06:08 PM.

  4. #4

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    ¡Hola krtxmrtz!

    I am so ashamed I can't help you with the brushes either... Yet I solved it in another way... I will show you how I did it.

    si_the_geek, thanks for your suggestions... But I ended up making a control that would be like the shape control but that would know when you click on it... I haven't finished it yet, but so far I have done what I need so far.

    I will add that control here (the code and the OCX, of course), it is still in testing mode.. For example, when it is disabled in Windows XP it won't get the colors it should... Also when you try changing the BackColor (I commented this code) it will make all of it go black.

    Why? I don't know...

    Anyway, here is the code...

    Oh, saludos desde este lado del charco, krtxmrtz
    Attached Files Attached Files
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Tec-Nico
    Hi,

    I'll take a look at the control... For now I've solved it differently: I draw my polygon on a picturebox with black brush and then I use an API function to change black for another colour.

    Still I'd like to know if this step could be avoided by using the SetDCBrushColor or SetDCPenColor function. The explanation in the Microsoft pages was not too informative -I couldn't set the functions to work properly- and I haven't found any references to them neither searching the forum nor in Allapi.
    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
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    What I haven't figured out yet is how to have a border colour different from the fill colour...
    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)

  7. #7

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    What I did was to draw a line per edge of the polygon of the color I wanted. That's how I personally solved it.

    I am afraid I know how to work with Brushes but with .NET which seems to be a little different (in the management of them) than VB 6.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I don't have VB.NET so I'll have to make do with VB6.

    I have been experimenting with various values of the parameter in GetStockObject but can't get it to plot a black border and the interior area of a different color, for example unfilled.

    Further, I can't erase the polygon -which I draw in a picturebox- after I'm done with it, the picturebox.cls instruction is ignored altogether!!! Thereafter, if I draw a polygon of a different shape it overlaps with the previous image.

    Would someone have pity on me and fish me out of this deadlock? Please...
    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

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Just a couple of ideas... I haven't tried them (Well, I tried the first one) but I hope they can help you.

    The first one is to draw two polygons... One will be the border and the other one will be the polygon inside.

    So you first draw the black polygon and then the blue one to give the sensation there is a blue polygon with a black border.

    The trick here would be to add a quantity in the left and on the top and substract it from the right and the bottom.

    And for erasing it, you could try setting the autoRedraw of the PictureBox to be False, then try .cls and then setting it to True again.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  10. #10
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Tec-Nico
    The trick here would be to add a quantity in the left and on the top and substract it from the right and the bottom.

    And for erasing it, you could try setting the autoRedraw of the PictureBox to be False, then try .cls and then setting it to True again.
    The first idea may work, probably I should add/substract one or two pixels but I thought the CreatePolygonRgn, GetStockObject and FillRgn functions would be more flexible and allow the trick directly.

    As for erasing the picturebox, it didn't work. I don't know what else to try, I'm stuck.
    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 Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    if you look at the tootip text link it my sig then you will see a class called clsDraw. This class is used to simplify API drawing.
    You can fill with custom cothers, you can even fill with a bitmap.
    It allows you to draw text in any font or style.

    All you would ned to do is add the class into your project, and away you go

    Hope that helps.

    Woof

  12. #12
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Wokawidget
    if you look at the tootip text link it my sig...
    You mean the Graph control, I assume... I'll take a look. What kind of bugs are those you mention in the page linked to?
    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
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Tec-Nico
    ¡Hola krtxmrtz!

    I am so ashamed I can't help you with the brushes either...
    I finally found the answer:
    VB Code:
    1. '(In a sub...)
    2.         Dim hBrush As Long
    3.         Dim hPen As Long
    4.  
    5.         hBrush = CreateSolidBrush(MyColour)
    6.         SelectObject myBuffer, hBrush
    7.         hPen = CreatePen(PS_SOLID, 1, vbBlack)
    8.         SelectObject myBuffer, hRPen
    9.  
    10.         'Poly2Buffer is a sub which does exactly
    11.         'what its name suggests by means of the
    12.         'CreatePolygonRgn API function. After the
    13.         'polygon region has been created to a buffer
    14.         'this buffer is BitBlt'd to a picturebox
    15.         Poly2Buffer myBuffer, xIon, yIon, nIon
    16.  
    17.         DeleteObject hBrush
    18.         DeleteObject hPen
    So this plots a polygon with black border and fills it with any colour you like. I've tried this out with back buffering, don't know if it would work when plotting directly to a PictureBox (no time to check it now)
    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