Help

I could use some help on how to create a square with round corners like the one provided on visual studio using code based on coordinates.
Thank you very much for any help.

Comments

  • : I could use some help on how to create a square with round corners like the one provided on visual studio using code based on coordinates.
    : Thank you very much for any help.
    :

    In VB6, try the Shape control.

    Best Regards,
    Richard

  • Thanks Richard,
    I'm using VB6 and I know the shape control,but unfortunately I have to do it without the shape control,just using coordinates.If you know the code of that shape just by using coordinates you would be very helpful. Thanks again.

    Best Regards,
    George
  • : Thanks Richard,
    : I'm using VB6 and I know the shape control,but unfortunately I have to do it without the shape control,just using coordinates.If you know the code of that shape just by using coordinates you would be very helpful. Thanks again.
    :
    : Best Regards,
    : George
    :

    Why without? :P

    You can dynamically reshape the shape control to your likings at runtime.
    Just place a control on the form, set Visible to False, and then when you want to use it set it's coordinates, shape and size to what you want and then make it Visible again.

    Anyway, if you're sure about not using the Shape control:
    There are some API's (function exported by Windows, available to programmers) to help you with drawing and stuff. For the API approach you'll need to look into using GDI functions.
    API's themselves require a lot of experience to use (especially for VB programmers). If you're really interested in using GDI, then let me know and I'll try to look into it myself (don't really know how to use them, [italic]yet[/italic]).
    Another approach would be to use VB's drawing. In the Form_Paint event you can add some draw code. For this situation, however, it's probably best to draw the shape once, for instance at Load time (don't know if this works though, you might need to wait till Form_Activate), and then use auto-redraw: set the Form's AutoRedraw property to true. That way, all that is painted on the form will be remembered and automatically repainted every time the Form_Paint event is called. If you want this auto-redrawing to stop you can call [italic]Form[/italic].Cls to wipe the sheet clean.
    Now to the actual drawing: A rounded box is a square with partial circels for corners.
    What you want to do is create a function to draw four quarter-circles and four straight line pieces.
    [code]
    *------------*
    | |
    | |
    | |
    *------------*
    [/code]
    The --- and | are straight lines (should be easy), using the [italic]Form[/italic].Line function. The * represents a part circle which can be drawn using [italic]Form[/italic].Circle.

    Example of these two functions:
    [code]
    'Syntax:
    'object.Line [leftbr]Step[rightbr] (x1, y1) [leftbr]Step[rightbr] - (x2, y2), [leftbr]color[rightbr], [leftbr]B[rightbr][leftbr]F[rightbr]

    Me.Line (20, 20) - (500, 20), vbBlack '//Straigt horizontal line
    Me.Line (20, 20) - (500, 500), vbRed '//Diagonal line

    'Syntax
    'object.Circle [leftbr]Step[rightbr] (x, y), radius, [leftbr]color, start, end, aspect[rightbr]

    '//Upper right corner
    Me.Circle (500, 500), 400, vbBlue, 0, 0.5 * 3.14159265
    '//Lower left corner
    Me.Circle (500, 500), 400, vbBlue, 3.14159265, 1.5 * 3.14159265
    [/code]

    Happy drawing :)

    Best Regards,
    Richard

  • First of All I wish You A HAPPY NEW YEAR,

    You were very helpful and I really appreciate it.
    You really covered most parts of my problem.
    And I see I have a lot of work to do.
    Thanks Again!!!
    Best Regards,
    George

  • I almost completed my task. Thanks for helping me. I really appreciate it,but I could use a little more help so that its 100% complete.The first part of my task was to draw round squares in a picture box(one at a time) by just giving the coordinates and the width and of course to fill them with the right color.Now I have to save the details of every shape(round rect) I create, its coordinates its width, and color and be able to open one record for each shape.I used the menu control and a common dialog control as CD1 to open and save the details of each shape,but every time it only saves the last shape.I can give you an example of the code I used to save and open the details of each shape.If you have any ideas where I'm wrong you would be very helpful.
    Private Sub MNUSAVE_Click()
    Dim newPoint As cords, i As Integer

    With CD1
    .DialogTitle = "FILE SAVE"
    .Filter = "ALL FILES *.* (*.*)|*.*| DATA FILES *.dat (*.dat)|*.dat"
    .FileName = "Myfile1.dat"
    .ShowSave
    End With

    MyFile = CD1.FileName

    Open MyFile For Random As #1 Len = Len(newPoint)
    For i = 1 To 50
    Randomize Timer
    newPoint.pointID = i
    newPoint.z = Val(Text3)
    newPoint.x = Val(Text4)
    newPoint.y = Val(Text5)
    newPoint.c = CD1.Color
    Put #1, newPoint.pointID, newPoint
    Next i

    Close #1
    End Sub

    Private Sub MNUOPEN_Click()
    Dim GivePoint As cords, TheCode As Integer

    With CD1
    .DialogTitle = "OPEN RANDOM ACCESS FILE"
    .Filter = "ALL FILES *.* (*.*)|*.*| DATA FILES *.dat (*.dat)|*.dat"
    .FileName = "Myfile1.dat"
    .ShowOpen
    End With

    MyFile = CD1.FileName

    Open MyFile For Random As #1 Len = Len(GivePoint)

    TheCode = InputBox("INSERT RECORD [1...50]", _
    "INSERT CODE")
    Get #1, TheCode, GivePoint

    MsgBox "x-coordinate = " & GivePoint.x & " y-coordinate = " & GivePoint.y & _
    " width = " & GivePoint.z & " color = " & GivePoint.c, vbInformation, "READING " & TheCode & "RECORD"

    Close #1

    End Sub

    Sorry to bother you with all that code. Thanks again.
    Best regards,
    George

  • It's a good code, but it needs one little adjustment:
    [code]
    ...
    newPoint.pointID = i
    newPoint.z = Val(Text3)
    newPoint.x = Val(Text4)
    newPoint.y = Val(Text5)
    newPoint.c = [red]CD1.Color[/red]
    ...
    [/code]

    (PS: The part in red, about the CD1.Color - what should it do? I don't really understand that part. I think it's supposed to be the shape's color, but now you are setting it to the Common Dialog Color, which is only available when you use the ShowColor function of the common dialog, which you are not using here)

    What you do with this part is store 50 times the SAME shape.
    If you want to save all shapes you ever created, you need to store them in a variable until you save them to disk.

    Because you're following an assignment here, I don't know what the prefered way is. I would myself use an array of cords structures.

    Do you know about arrays, and specifically dynamic arrays?
    Easiest is a static array of 50 items (because you seem to have hard-coded 50 items into your code). Every time you create a point, you save it to the right index of the array (eg first shape in ptShapes(1), second in ptShapes(2) and so on)

    [code]
    'Put this in the general section of your form:
    '//Create an array of shapes
    Private ptShapes(1 To 50) As cords
    '//This might be needed to remember at which index you are on
    Private nIndex As Integer


    'Put this in the Form_Load event:
    nIndex = 1


    'Put this where you want to save the shape:
    ptShapes(nIndex).pointID = nIndex
    ptShapes(nIndex).z = Val(Text3)
    ptShapes(nIndex).x = Val(Text4)
    ptShapes(nIndex).y = Val(Text5)
    ptShapes(nIndex).c = [red]CD1.Color[/red]
    '//Proceed to the next item
    nIndex = nIndex + 1
    If (nIndex = 51) Then
    MsgBox "Woops! The maximum amount of shapes is reached!"
    '//PS: You should do something here, because the next time
    '// this code is run after this messagebox appears it will
    '// give an error
    End If
    [/code]

    Now I always prefer to use dynamic arrays, but they require a bit more maintenance etc
    If you're interested, here's a little snippit:
    [code]
    Private ptShapes() As cords
    Private nCount As Integer

    Private Sub Form_Load()
    nCount = 0
    End Sub

    Private Sub AddShape(newShape As cords)
    nCount = nCount + 1
    '//Redimensions ptShapes, preserving the
    '// information already stored in it
    '//Without Preserve, the array would be
    '// emptied
    ReDim Preserve ptShapes(1 To nCount)
    ptShapes(nCount) = newShape
    End Sub

    Private Sub RemoveShape(Index As Integer)
    Dim i As Integer
    Dim nIndex As Integer
    Dim newShapes() As cords

    '//Check if Index is valid
    If (Index < 1 Or Index > nCount) Then
    MsgBox "Error! Invalid Index for Shape"
    Exit Sub
    End If

    '//If there is only one item left in the array then
    '// clear the array
    If nCount = 1 Then
    Erase newShapes
    nCount = 0
    Else
    '//Reserve space for new array with
    '// item at Index removed
    ReDim newShapes(1 To nCount - 1)
    nIndex = 1
    '//Copy all items, except the item at Index
    '// to the new array
    For i = 1 To nCount
    If (i <> Index) Then
    newShapes(nIndex) = ptShapes(i)
    nIndex = nIndex + 1
    End If
    Next i
    End If
    End Sub

    Private Function GetShape(Index As Integer) As cords

    '//Check if Index is valid
    If (Index < 1 Or Index > nCount) Then
    MsgBox "Error! Invalid Index for Shape"
    Exit Sub
    End If

    GetShape = ptShapes(Index)
    End Function
    [/code]
    Do with it what you like ^^

    PS: The code examples I gave are in VB6
    Best Regards,
    Richard

  • Thanks Richard,

    I really appreciate your help. You see I'm new to VB programming so I definitely needed some help to complete my task in time.
    My email is george_petras@hotmail.com. Feel free to ask me whatever(other than VB), when you need any help.

    Best Regards,
    George
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories