how to make the background picture of a MDIparent centered?
Printable View
how to make the background picture of a MDIparent centered?
I think this will work, but I havn't tested it.
VB Code:
Picture1.Left = (MDIParent.ScaleWidth / 2) - (Picture1.Width / 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 rightQuote:
Originally Posted by eyeRmonkey
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:
Option Explicit Private Const SWP_FRAMECHANGED = &H20 Private Const SWP_NOMOVE = &H2 Private Const SWP_NOSIZE = &H1 Private Const SWP_NOZORDER = &H4 Private Const swpFlags As Long = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE 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 Private LoadPic As StdPicture Private Sub MDIForm_Load() Set LoadPic = LoadPicture("C:\windows\winnt.bmp") ' change the path to your picture CenterPicture Picture1 End Sub Private Sub MDIForm_Resize() CenterPicture Picture1 End Sub Private Sub CenterPicture(DummyPic As PictureBox) DummyPic.Visible = False On Error Resume Next DummyPic.Width = Me.ScaleWidth DummyPic.Height = Me.ScaleHeight On Error GoTo 0 DummyPic.AutoRedraw = True DummyPic.BackColor = Me.BackColor DummyPic.PaintPicture LoadPic, (DummyPic.ScaleWidth / 2) - (DummyPic.ScaleX(LoadPic.Width, vbHimetric, vbTwips) / 2), _ (DummyPic.ScaleHeight / 2) - (DummyPic.ScaleY(LoadPic.Height, vbHimetric, vbTwips) / 2) Set Me.Picture = DummyPic.Image SetWindowPos Me.hwnd, 0, 0, 0, 0, 0, swpFlags ' repaint the form End Sub
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.That's it the rest is handled by the class in a very simular way CVMichael did.VB Code:
Private oPic As CMDIPicture Private Sub MDIForm_Load() Set oPic = New CMDIPicture 'The Init method takes the MDI form and a standard picture object 'as argument. Instead of LoadPicture you can pass the Picture property of 'any PictureBox or Image control or load an image from a resource file. oPic.Init Me, LoadPicture("c:\TheImage.jpg") End Sub