Code not working when move it from FORM to MODULE

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all
I have a code which display an exe inside a vb form, then dock it to a container.
I got the code from the internet, copy it to a command_click and modify it and then I run it, and it worked as expected.
Now, I keep the code in a module so I can use it any where in my project, when I put it there, the behaviour is not what expected.
If I called the code from the command_click, the codes work, when I call it from the module the Docking code dont work.
Any one can test it and see if he got the same behaviour?
Code In Form
<pre class="prettyprint lang-vb Public Class Form2
Public Enum SetWindowPosFlags As UInteger
SWP_ASYNCWINDOWPOS = &H4000
SWP_DEFERERASE = &H2000
SWP_DRAWFRAME = SWP_FRAMECHANGED
SWP_FRAMECHANGED = &H20
SWP_HIDEWINDOW = &H80
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSENDCHANGING = &H400
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_SHOWWINDOW = &H40
End Enum


Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2


Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Auto Function SetWindowPos Lib"user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean

Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Private proc As Process = New Process()

Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
proc = Process.Start("notepad.exe")
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
SetWindowPos(proc.MainWindowHandle, HWND_TOP,
Panel1.ClientRectangle.Left,
Panel1.ClientRectangle.Top,
Panel1.ClientRectangle.Width,
Panel1.ClientRectangle.Height, SetWindowPosFlags.SWP_NOACTIVATE + SetWindowPosFlags.SWP_SHOWWINDOW)
End Sub

Private Sub Button4_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
RunEXE_Docked("notepad.exe",Panel1) <--- this function is not working as expected
End Sub
Private Sub Form2_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
<span class="x_Apple-tab-span" style="white-space:pre nothing here
End Sub
End Class [/code]
<br/>

This is my module
<pre class="prettyprint lang-vb Module sam_module

Public Enum SetWindowPosFlags As UInteger
SWP_ASYNCWINDOWPOS = &H4000
SWP_DEFERERASE = &H2000
SWP_DRAWFRAME = SWP_FRAMECHANGED
SWP_FRAMECHANGED = &H20
SWP_HIDEWINDOW = &H80
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSENDCHANGING = &H400
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_SHOWWINDOW = &H40
End Enum


Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2


Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Auto Function SetWindowPos Lib"user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean

Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Private proc As Process = New Process()

Function RunEXE_Docked(ByVal exe_path As String,ByVal display_in As Object)
proc = Process.Start(exe_path)
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, display_in.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
SetWindowPos(proc.MainWindowHandle, HWND_TOP,
display_in.ClientRectangle.Left,
display_in.ClientRectangle.Top,
display_in.ClientRectangle.Width,
display_in.ClientRectangle.Height, SetWindowPosFlags.SWP_NOACTIVATE + SetWindowPosFlags.SWP_SHOWWINDOW)
End Function
End Module[/code]
I have a Panel1 in the form with Dock = Bottom, when I change the form (grow or reduce) the width, the exe (which is notepad.exe in this example) should follow the size of the form. that is not happening when calling the code from the module. why?
Thanks in advance.

<br/>


<hr class="sig Blog: http://samir-ibrahim.blogspot.com/

View the full article
 
Back
Top