Link to home
Start Free TrialLog in
Avatar of hexux
hexux

asked on

how to fill a shape (triangle, hexagon, rectangle, etc) with colors

I created some figures or shapes (like triangles, squares, rectangles) inside a picture object with picture.line(x,y), that's very easy, now I want to change the internal color of the figure only clicking one time at any internal point of the figure. I'm working with VB6.
ASKER CERTIFIED SOLUTION
Avatar of NBrownoh
NBrownoh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qwertykeyboard
qwertykeyboard

Is it actually possible if you are using line. Woulnd't is be simpler to just draw pictures and change the infill there? If not u could use lots of thick lines to fill them up!!! Bad ideas perhaps, but never the less, still possibilities
Avatar of hexux

ASKER

example:

Private Sub Command1_Click()
Picture1.Cls
Picture1_Paint
Picture1.DrawWidth = 2
x1 = Val(Text1)
y1 = Val(Text2)

x2 = Val(Text3)
y2 = Val(Text4)

x3 = Val(Text5)
y3 = Val(Text6)

Picture1.Line (x1, y1)-(x2, y2), vbRed
Picture1.Line (x2, y2)-(x3, y3), vbRed
Picture1.Line (x3, y3)-(x1, y1), vbRed

End Sub

I want to fill the triangle with a color just clicking once over its internal surface
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
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long

Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Dim poly(1 To 3) As COORD, NumCoords As Long

Private Sub Command1_Click()
    On Error Resume Next
    Dim hBrush As Long, hRgn As Long
    Picture1.Cls
    NumCoords = 3
    Picture1.ScaleMode = vbPixels
    poly(1).x = Text1
    poly(1).y = Text2
    poly(2).x = Text3
    poly(2).y = Text4
    poly(3).x = Text5
    poly(3).y = Text6
    Polygon Picture1.hdc, poly(1), NumCoords
    hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    DeleteObject hRgn
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    On Error Resume Next
    hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    If PtInRegion(hRgn, x, y) <> 0 Then
        hBrush = GetStockObject(BLACKBRUSH)
        If hRgn Then FillRgn Picture1.hdc, hRgn, hBrush
    End If
    DeleteObject hRgn
End Sub


This code will turn the triangle you click black
oops, you can remove the hBrush from the Dim statement in the Command1_Click event, its not needed there, also add this line to the top of the Picture1_MouseDown event.
Dim hBrush As Long, hRgn As Long

and there ya go, that works perfectly.
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you try my generic solution yet?
hexux,

Have you tried NBrownoh's code or my post?
I would recommend splitting points between NBrownoh and myself.

Idle_Mind
Moderator, my recommended disposition is:

    Split points between: NBrownoh & Idle_Mind

Dan Rollins -- EE database cleanup volunteer