Results 1 to 7 of 7

Thread: How to centre the graphic in the picture box

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    How to centre the graphic in the picture box

    How to centre the graphic draw in the picture box?


    Picture1.Line (Text1.Text, Picture1.ScaleHeight - Text2.Text)-(Text3.Text, Picture1.ScaleHeight - Text4.Text), vbBlue
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text1.Text, Picture1.ScaleHeight - Text2.Text), 100
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text3.Text, Picture1.ScaleHeight - Text4.Text), 100
    Picture1.Line (Text3.Text, Picture1.ScaleHeight - Text4.Text)-(Text5.Text, Picture1.ScaleHeight - Text6.Text), vbBlue
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text5.Text, Picture1.ScaleHeight - Text6.Text), 100
    Picture1.Line (Text5.Text, Picture1.ScaleHeight - Text6.Text)-(Text7.Text, Picture1.ScaleHeight - Text8.Text), vbBlue

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to centre the graphic in the picture box

    I believe that the center of a PictureBox is
    x= Picture1.ScaleWidth / 2 , y = Picture1.ScaleHeight / 2

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

    Re: How to centre the graphic in the picture box

    The algo for centering anything on a background object

    centerX = (bkgObj.Width - tgtObj.Width) \ 2
    centerY = (bkgObj.Height - tgtObj.Height) \ 2
    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
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to centre the graphic in the picture box

    Matrick

    In addition to the foregoing posts, I'll mention that
    I've found the graphic method specs somewhat "obtuse".

    So I create generic subs that make it easier for me to
    establish the various paremeters. For example:

    ... for a Line
    Code:
    Sub ZLINE(nXX1, nXX2, nYY1, nYY2, nDW, nCC)
        '
        With PB1
            .DrawStyle = 0
            .DrawWidth = nDW
            PB1.Line (nXX1, nYY1)-(nXX2, nYY2), nCC
        End With
        '
    End Sub
    ... for a Circle
    Code:
    Sub ZCIRC(nXX1, nYY1, nDW, nRADIUS, nCC)
        '
        With PB1
            odw = .DrawWidth
            .DrawWidth = nDW
            PB1.Circle (nXX1, nYY1), nRADIUS, nCC
            .DrawWidth = odw
        End With
        '
    End Sub
    Then, in the calling sub, to draw your graphics,
    (not centered yet) I would do this..
    Code:
    xx1 = Val(Text1.Text)
    xx2 = Val(Text3.Text)
    yy1 = Val(Text2.Text)
    yy2 = Val(Text4.Text)
    '
    ZLINE xx1, xx2, yy1, yy2, 1, vbBlue
    ZCIRC xx1, yy1, 1, 100, vbRed
    ZCIRC xx2, yy2, 1, 100, vbRed
    '
    ' etc
    '
    To deal with the centering requirements, I would
    modify the code as follows by applying offsets
    Code:
    With PB1
        ' raw coords
        xx1 = Val(Text1.Text)
        xx2 = Val(Text3.Text)
        yy1 = Val(Text2.Text)
        yy2 = Val(Text4.Text)
        ' calc offsets
        lenX = xx2 - xx1
        lenY = yy2 - yy1
        cenX = xx1 + lenX / 2
        cenY = yy1 + lenY / 2
        cenXpb = .Width / 2
        cenYpb = .Height / 2
        oox = cenXpb - cenX 
        ooy = cenYpb - cenY
        ' apply offsets
        xx1o = xx1 + oox
        xx2o = xx2 + oox
        yy1o = yy1 + ooy
        yy2o = yy2 + ooy
        ZLINE xx1o, xx2o, yy1o, yy2o, 1, vbBlue
        ZCIRC xx1o, yy1o, 1, 100, vbRed
        ZCIRC xx2o, yy2o, 1, 100, vbRed
        '
    End With
    I have not tested this.

    It is a admittedly "brute force" approach, but I
    hope that it gives you some ideas.

    Spoo

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to centre the graphic in the picture box

    Quote Originally Posted by LaVolpe View Post
    The algo for centering anything on a background object

    centerX = (bkgObj.Width - tgtObj.Width) \ 2
    centerY = (bkgObj.Height - tgtObj.Height) \ 2
    Yes, I would add however that in some cases you need to use the scalewidth and scaleheight of the background object to get the correct results.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to centre the graphic in the picture box

    I would like to draw the line at the centre of picture box. Mybe I would like to move the line I drew to the center of picturebox

    Picture1.Line (Text1.Text, Picture1.ScaleHeight - Text2.Text)-(Text3.Text, Picture1.ScaleHeight - Text4.Text), vbBlue
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text1.Text, Picture1.ScaleHeight - Text2.Text), 100
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text3.Text, Picture1.ScaleHeight - Text4.Text), 100
    Picture1.Line (Text3.Text, Picture1.ScaleHeight - Text4.Text)-(Text5.Text, Picture1.ScaleHeight - Text6.Text), vbBlue
    Picture1.FillStyle = 0
    Picture1.FillColor = vbRed
    Picture1.Circle (Text5.Text, Picture1.ScaleHeight - Text6.Text), 100
    Picture1.Line (Text5.Text, Picture1.ScaleHeight - Text6.Text)-(Text7.Text, Picture1.ScaleHeight - Text8.Text), vbBlue

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to centre the graphic in the picture box

    Matrik

    Your post #6 appears to be the same as your OP (post #1)

    Did you happen to notice my post #4?

    If you did but felt it was too "obtuse", so be it .. no problem
    But, if you did and have some questions, I'll be glad to expand.

    Spoo

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