Results 1 to 3 of 3

Thread: Center picture in picture box (code needs polishing)

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Center picture in picture box (code needs polishing)

    Hi,
    TO TRY THE CODE: place a picturebox on a form and name it DOWN

    I want to use picture box as a command button in order to avoid the focus rectangle when the regular command button is down.
    To do that I draw edge at runtime around my picture box (see code below)

    The button part works well (picture box looks like the button)

    The only part I have trouble with is centering the picture in the picture box. I've got the code from Arron Young and it works on a picture box but when I combine it with my code to draw the edges around the picture box it doesn't center.

    Anyway here's the code (the last sub - CenterPicture() is the one I want to use with the rest of my code.... so far unsuccessfully.

    VB Code:
    1. Private Declare Function DrawEdge Lib "user32" (ByVal hDC As Long, qrc As RECT, ByVal Edge As Long, ByVal grfFlags As Long) As Long
    2. Private Type RECT
    3.     Left As Long
    4.     Top As Long
    5.     Right As Long
    6.     Bottom As Long
    7. End Type
    8. Private Const BDR_RAISEDOUTER = &H1
    9. Private Const BDR_SUNKENOUTER = &H2
    10. Private Const BDR_RAISEDINNER = &H4
    11. Private Const BDR_SUNKENINNER = &H8
    12. Private Const EDGE_RAISED = (BDR_RAISEDOUTER Or BDR_RAISEDINNER)
    13. Private Const EDGE_SUNKEN = (BDR_SUNKENOUTER Or BDR_SUNKENINNER)
    14. Private Const EDGE_ETCHED = (BDR_SUNKENOUTER Or BDR_RAISEDINNER)
    15. Private Const EDGE_BUMP = (BDR_RAISEDOUTER Or BDR_SUNKENINNER)
    16. Private Const BF_LEFT = &H1
    17. Private Const BF_TOP = &H2
    18. Private Const BF_RIGHT = &H4
    19. Private Const BF_BOTTOM = &H8
    20. Private Const BF_RECT = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
    21.  
    22. Dim ButtonBorder As Long     ' picture variable to store Edge type
    23.  
    24. ' Draw picture with edges at startup
    25. Private Sub Form_Load()
    26.   DOWN.AutoRedraw = False
    27.   ButtonBorder = EDGE_RAISED       ' draw "button edge" around picture
    28. End Sub
    29.  
    30. ' This will make our picturebox look like a PRESSED button
    31. Private Sub DnPressed()
    32.   ButtonBorder = EDGE_SUNKEN
    33.   DOWN.Refresh
    34. End Sub
    35. ' This will make our picturebox look like a RELEASSED button
    36. Private Sub DnReleased()
    37.   ButtonBorder = EDGE_RAISED
    38.   DOWN.Refresh
    39. End Sub
    40.  
    41. ' Simulate Command_MouseDown() & command_MouseUp()
    42. Private Sub DOWN_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    43.   Call DnPressed
    44. End Sub
    45. Private Sub DOWN_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    46.   Call DnReleased
    47. End Sub
    48. Private Sub DOWN_DblClick()
    49.   Call DnPressed
    50. End Sub
    51.  
    52. ' Repaint drawn edges around our picture
    53. Private Sub DOWN_Paint()
    54.   Dim r As RECT
    55.   DOWN.Cls
    56.   r.Bottom = DOWN.ScaleHeight
    57.   r.Right = DOWN.ScaleWidth
    58.   DrawEdge DOWN.hDC, r, ButtonBorder, BF_RECT
    59. End Sub
    60.  
    61. '**********************************************************************************
    62. '**********************************************************************************
    63. '**********************************************************************************
    64. ' Arron Young's code to center picture inside a picturebox THAT NEED'S TO BE INTEGRATED WITH THE ABOVE CODE
    65. Private Sub CenterPicture()
    66.   With DOWN
    67.     DOWN.Line (0, 0)-(.ScaleWidth, .ScaleHeight), .BackColor, BF
    68.     .PaintPicture .Picture, (.ScaleWidth - .ScaleX(.Picture.Width, vbHimetric, .ScaleMode)) / 2, _
    69.                             (.ScaleHeight - .ScaleY(.Picture.Height, vbHimetric, .ScaleMode)) / 2
    70.   End With
    71. End Sub
    72. '**********************************************************************************
    73. '**********************************************************************************
    74. '**********************************************************************************
    Thanks

    Tomexx.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Change Scalemode of the picture box to 3-Pixel, appearence to flat, and BorderStyle to none
    Thanks

    Tomexx.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Ok, here's the project
    Attached Files Attached Files
    Thanks

    Tomexx.

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