Results 1 to 14 of 14

Thread: [RESOLVED] Shape controls not drawn very precisely.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] Shape controls not drawn very precisely.

    Can anyone explain why the shape controls don't draw very precisely?

    I draw a square shape with an invisible background then diagonal line controls from corner to corners but the drawn points don't quite line up. Hoping it's visalble on the attached picture.

    Top Left corner is ok but the rest of the square is offset.
    Attached Images Attached Images  

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Shape controls not drawn very precisely.

    What are the X1, Y1, X2, Y2 coordinates of each line?

    Set them to the Shape's Width/Height properties minus 15 (using twips). Obviously adjusting the coords with the shape's left and top properties.

    This seems to better align the Line controls to the Shape's corners (at least to my old eyes).

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Shape controls not drawn very precisely.

    Also keep in mind that if you tell Vb the shape is a square, it will draw a square centered in the shape. So, if the shape's size is not exactly a square (i.e., width=height), then the drawn square will be offset/centered within the shape's bounding rectangle.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    I was thinking about manually adjusting the shape but I didn't know if the problem was based on the picture scale so forever being a problem.

    Bit of a pain because I need to adjust the code so that the top right corner is the one that doesn't get adjusted in each case.

    I was hoping there was a property value of the shape I could ammend. Seems just to be a strange bug.

    Thanks for the suggestions!!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    Just added 15 "twips" to the width and height.
    Thanks brucevde, your "old eyes" serve you well.

  6. #6
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: Shape controls not drawn very precisely.

    To elaborate on what La Volpe said - it is not a bug:

    Switch back and forth between rectangle and square if the shapes are not the same size then the shape control is not a square and the left coordinate is the upper left when the shape control is a rectangle. The shape is not the control it is within the control and the properties don't necessarily reflect the shapes dimensions.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    technorobbo,
    I don't understand how it's not a bug. The defined shape was actually a rectangle, but even then shouldn't make any difference from a square.

    I define the shape .left , .top , .width, & .height . BorderWidth = 1 so should draw it as specified without be needing to make any manual tweaks.

    I've worked out that a 10 "twip" adjustment seem to do the job though.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Shape controls not drawn very precisely.

    What technorobbo and I were trying to highlight is that the geometric shape drawn in the control is not necessarily the same size of the control.

    If you said the shape is a square, and the control's width > height or height > width, then a square would be drawn, but centered within the shape. A shape set to rectangle, of course would draw a rectangle from edge to edge.

    So, drawing a line from the bottom left corner to the top right corner of a "square" shape control that has unequal width & height is not guaranteed to line up correctly from corner to corner. If you define the shape with .Left, .Top, .Width, .Width then I would think the diagonal lines would line up correctly. Not on a VB machine at the moment, so I can't back that statement up.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    Thanks,
    It was a "rectangle" that I was drawing, I should have specified that even though the example happened to be a square. (width = height)
    Still seems like a bug. Why wouldn't they make the shape the same size as the control?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Shape controls not drawn very precisely.

    Well, generally, unless a control has some sort of "AutoSize" property, controls will not size themselves to what is drawn inside of them. The Shape control doesn't have an AutoSize property so it resizes the drawn figure to the control's dimensions instead of the other way around. Why does it center the drawn figure? The control author had to make a decision: center, left justify or right justify or don't draw it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    Still don't follow..

    I have a .left , .top, .height, and .width. And I want it to draw a rectangle to those specified dimensions. The lines I drew inside make no difference to the size of the shape, they only highlighted the problem.

    My problem is that it doesn't draw the correct size and I can't imagine the situation where someone didn't want vb to follow their requirements

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Shape controls not drawn very precisely.

    I think I now see what you are describing. It isn't a bug, but rather a function.

    Without decompiling VB, I am pretty confident that the Rectangle drawn in the shape control is done via the API Rectangle. That API's description follows, notice the last line
    Quote Originally Posted by MSDN
    ...The extreme bottom and right edges of rectangle are not drawn because the bottom and right coordinates are treated as the width and height respectively of the rectangle instead of its edges
    The above simply states that the far right and bottom edges are not included in the rectangle.

    To highlight this, add the following code to a new form and run that form. You will see that the red rectangle is a shape control and only has its top/left corner centered in the little blue handles when all corners are really expected to be centered also. The black rectangle is done with the API Rectangle -- it is identical to the red rectangle and using the same dimensions.
    Add a Shape control to the form first
    Code:
    Option Explicit
    Private Declare Function Rectangle Lib "gdi32.dll" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    
    Private Sub Form_Load()
        Me.ScaleMode = vbPixels
        Me.AutoRedraw = True
        Shape1.BorderColor = vbRed
        Show
        Shape1.Move 10, 10, 50, 50
        Me.Line (8, 8)-(12, 12), vbBlue, B
        Me.Line (58, 8)-(62, 12), vbBlue, B
        Me.Line (58, 58)-(62, 62), vbBlue, B
        Me.Line (8, 58)-(12, 62), vbBlue, B
    
        Dim offset As Long
        offset = 70
        Me.Line (8 + offset, 8)-(12 + offset, 12), vbBlue, B
        Me.Line (58 + offset, 8)-(62 + offset, 12), vbBlue, B
        Me.Line (58 + offset, 58)-(62 + offset, 62), vbBlue, B
        Me.Line (8 + offset, 58)-(12 + offset, 62), vbBlue, B
        Rectangle Me.hdc, 10 + offset, 10, 60 + offset, 60
    End Sub
    Now change this line: Shape1.Move 10, 10, 50, 50
    To this & see the difference: Shape1.Move 10, 10, 50+1, 50+1
    You will notice it is exactly what we wanted in the first place. So, bottom line, it is simply the result of a very common API function. BTW, many APIs/functions have the same restrictions, including regions and other shape drawing APIs. They will not include the far right/bottom edges. Just the way it is.
    Last edited by LaVolpe; Jul 17th, 2009 at 06:17 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: Shape controls not drawn very precisely.

    Actually if it's a rectangle and you draw the lines correctly it should align.
    The code should look something like this:
    Code:
    Me.Line (Shape1.Left, Shape1.Top)-(Shape1.Width + Shape1.Left - 1, Shape1.Top + Shape1.Height - 1)
    Me.Line (Shape1.Left, Shape1.Top + Shape1.Height - 1)-(Shape1.Width + Shape1.Left - 1, Shape1.Top)
    You have to subtract 1 because "width" plus "start" is always 1 more than "end" since "start" is always zero mathematically.

    This is dependent on the draw width being set to 1 on the container.

    Post Edited:


    Note in diagram below a width of 4 but address of 3



    Maybe you should post your code.
    Attached Images Attached Images  
    Last edited by technorobbo; Jul 18th, 2009 at 09:25 AM.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Shape controls not drawn very precisely.

    Thankyou both!

    It clears everything up and you've happened to show me how to draw the controls in a slightly simpler way than I am currently doing.

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