How do I stop the childform overlapping the menubar and statusbar when i use Setparent?

Posted on 2010-01-11
Medium Priority
Last Modified: 2013-11-25
As a test app, I have a .net MDI parent form, with a standard menubar and statusbar.  On the 'new' button click, I start a new process, get it's window handle using .MainWindowHandle, and pass it to SetParent, to make it a child of the main form.  This works, however if I maximize the child window it overlaps the menubar and the statusbar.  It also overlaps the menu/status bar when the form is moved around inside the client area.

Any ideas how i stop this?  I only want it to expand into the main client area.

Many Thanks in advance
Dim p As New Process
        p.StartInfo.FileName = "notepad.exe"

        SetParent(p.MainWindowHandle(), Me.Handle)

Open in new window

Question by:ITPOL
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Expert Comment

ID: 26290257
This is what I use in a MDI...
this code will fill inthe MDI area and check to make sure that it is not running already in the background under someother screen.
This code will also kept the Maximized Child Forms from flickering when opening

Public Const WM_SETREDRAW As Integer = &HB
Public Declare Auto Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Boolean

'ADD this to your button or menu click
Dim objForm As Form = New frmMyNewScreen
                    With objForm
                        If Not CheckActive(.Name) Then
                            .Dock = DockStyle.Fill
                            SendMessage(Me.Handle, WM_SETREDRAW, 0, 0)
                            .Text = "My New Screen"
                            .MdiParent = Me
                            .Text = "My New Screen"
                            .MdiParent = Me
                            .TopMost = True
                        End If
                    End With
'' IF you use a StatusBar  then un-comment
 ''       StatusBar1.Height += 1
 ''   StatusBar1.Height -= 1     
    SendMessage(Me.Handle, WM_SETREDRAW, 1, 0)

  Private Function CheckActive(ByVal pChildName As String) As Boolean
        '//  Check to see if the form 
        '//  is already open for viewing
        For Each childForm As Form In Me.MdiChildren
            If TypeName(childForm).ToUpper = pChildName.ToUpper Then
                Return True
            End If
        Return False
    End Function

Open in new window


Accepted Solution

ITPOL earned 0 total points
ID: 26291637
Thanks Planocz,

Unfortunately what I was also looking for was a way to turn the window handle into the form object, then the above code would be fine.

I managed to achieve what I was looking for using the following code and API's;

Thanks for your suggestion
p = New Process
        p.StartInfo.FileName = "notepad.exe"


        ' Create a new instance of the child form.
        Dim ChildForm As New ChildFormvb(p.MainWindowHandle)
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me

        m_ChildFormNumber += 1
        ChildForm.Text = "Window " & m_ChildFormNumber
        'Dim panel1 As New Panel
        'panel1.Dock = DockStyle.Fill
        'panel1.BorderStyle = BorderStyle.Fixed3D

        'SetParent(p.MainWindowHandle(), Me.Handle)
        SetParent(p.MainWindowHandle(), ChildForm.Handle)

        ' Remove border and whatnot
        SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE)

        ' Move the window to overlay it on this window
        MoveWindow(p.MainWindowHandle, 0, 0, ChildForm.Width, ChildForm.Height, True)

Open in new window


Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question