Results 1 to 4 of 4

Thread: [RESOLVED] how to make a picture from a MDIparent centered?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    125

    Resolved [RESOLVED] how to make a picture from a MDIparent centered?

    how to make the background picture of a MDIparent centered?

  2. #2
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: how to make a picture from a MDIparent centered?

    I think this will work, but I havn't tested it.
    VB Code:
    1. Picture1.Left = (MDIParent.ScaleWidth / 2) - (Picture1.Width / 2)
    2. Picture1.Top = (MDIParent.ScaleHeight / 2) - (Picture1.Height / 2)
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: how to make a picture from a MDIparent centered?

    Quote Originally Posted by eyeRmonkey
    I think this will work, but I havn't tested it.
    VB Code:
    1. Picture1.Left = (MDIParent.ScaleWidth / 2) - (Picture1.Width / 2)
    2. Picture1.Top = (MDIParent.ScaleHeight / 2) - (Picture1.Height / 2)
    In a MDI form you can't center picture/or any kind of objects... they always align to left, top, bottom, or right

    Here's how I did it, you have to put a PictureBox on the form that is used as a temporary holder for the picture... (make the picture.Visible = False)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const SWP_FRAMECHANGED = &H20
    4. Private Const SWP_NOMOVE = &H2
    5. Private Const SWP_NOSIZE = &H1
    6. Private Const SWP_NOZORDER = &H4
    7.  
    8. Private Const swpFlags As Long = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
    9.  
    10. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    11.  
    12. Private LoadPic As StdPicture
    13.  
    14. Private Sub MDIForm_Load()
    15.     Set LoadPic = LoadPicture("C:\windows\winnt.bmp") ' change the path to your picture
    16.     CenterPicture Picture1
    17. End Sub
    18.  
    19. Private Sub MDIForm_Resize()
    20.     CenterPicture Picture1
    21. End Sub
    22.  
    23. Private Sub CenterPicture(DummyPic As PictureBox)
    24.     DummyPic.Visible = False
    25.    
    26.     On Error Resume Next
    27.     DummyPic.Width = Me.ScaleWidth
    28.     DummyPic.Height = Me.ScaleHeight
    29.     On Error GoTo 0
    30.    
    31.     DummyPic.AutoRedraw = True
    32.     DummyPic.BackColor = Me.BackColor
    33.    
    34.     DummyPic.PaintPicture LoadPic, (DummyPic.ScaleWidth / 2) - (DummyPic.ScaleX(LoadPic.Width, vbHimetric, vbTwips) / 2), _
    35.         (DummyPic.ScaleHeight / 2) - (DummyPic.ScaleY(LoadPic.Height, vbHimetric, vbTwips) / 2)
    36.    
    37.     Set Me.Picture = DummyPic.Image
    38.    
    39.     SetWindowPos Me.hwnd, 0, 0, 0, 0, 0, swpFlags ' repaint the form
    40. End Sub
    Last edited by CVMichael; Jul 24th, 2005 at 02:18 AM. Reason: this code is better

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] how to make a picture from a MDIparent centered?

    Even though this thread have been marked as resolved I would like to add another solution. It's very simular to the one that CVMicheal posted but it doesn't require you to add a picture box on the MDI form during design time. Instead a picture box is created during run-time by adding it to the MDIForm controls collection. For some reason when you do that the control doesn't have to be aligned to one of the borders. It's a surprising little feature (or bug?) but makes it very interesting since you can now wrap it up in an easy to use class.

    I quickly wrote a class that you find attached to this thread. To use it add code simular to this to your MDI Form.
    VB Code:
    1. Private oPic As CMDIPicture
    2.  
    3. Private Sub MDIForm_Load()
    4.     Set oPic = New CMDIPicture
    5.     'The Init method takes the MDI form and a standard picture object
    6.     'as argument. Instead of LoadPicture you can pass the Picture property of
    7.     'any PictureBox or Image control or load an image from a resource file.
    8.     oPic.Init Me, LoadPicture("c:\TheImage.jpg")
    9. End Sub
    That's it the rest is handled by the class in a very simular way CVMichael did.
    Attached Files Attached Files

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