How to set the window size of a process to the size of a panel

I'm embedding notepad (it could be any executable) in a panel and I'm trying to set the size of the process to the size of the panel with no luck. Is it possible to maximize the process so it fills in the whole panel?


Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Private Const SWP_NOSIZE As Integer = &H1
 
 
'load wordpad in the panel
        Dim p As Process = Process.Start("wordpad")
        p.WaitForInputIdle()
        SetParent(p.MainWindowHandle, Panel1.Handle)
'try and set wordpad equal to the size of the panel        
 Dim hWnd As Integer
        hWnd = p.Handle.ToInt32 ' however you get your hWnd...
 
       SetWindowPos(hWnd, 0, Panel1.Size.Width.ToString, Panel1.Size.Height.ToString(), 0, 0, SWP_NOSIZE)

Open in new window

cobolinx1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
I've had much better luck using MoveWindow in the past instead of SetWindowPos.

As well, i generally use the SetWindowLong to take off the window border of the embedded app, so the user can't move it around.

For some reason (not sure why), SetWindowLong doesn't seem to work well with the "declare function" import, you need to use the full DllImport, if you even want to take off the window border.
Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal repaint As Boolean) As Boolean
Private Const GWL_STYLE As Integer = (-16)
Private Const WS_VISISBLE As Integer = &H10000000
 
 
<DllImport("user32.dll", EntryPoint:="SetWindowLongA", SetLastError:=True)> _
Private Shared Function SetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
 
 
 
Dim p As Process = Process.Start("wordpad")
p.WaitForInputIdle()
 
'set the parent to the panel
SetParent(p.MainWindowHandle, Panel1.Handle)
 
'take off the window border
SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISISBLE)
 
'move to the dimensions of the panel
MoveWindow(p.MainWindowHandle, 0, 0, Panel1.Width, Panel1.Height, True)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cobolinx1Author Commented:
perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.