WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
Go Back  Xtreme Visual Basic Talk > > > WinForm/Panel1/Embedding EXE


Reply
 
Thread Tools Display Modes
  #1  
Old 04-01-2015, 04:54 PM
isEy78 isEy78 is offline
Freshman
 
Join Date: Nov 2005
Location: Ohio
Posts: 27
Default SOLVED - WinForm/Panel1/Embedding EXE


Hi all, hoping I explain this OK. Sorry if it's in the wrong section.

In my form I have embedded Notepad (for the sake of example)

Here is how

Code:
proc = Process.Start(NOTEPAD_PATH)
SetParent(proc.MainWindowHandle, Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
Ok now I have Notepad running inside Panel1 in my form and I can type in Notepad and save and all that good stuff.

When I click off Notepad, say to click a Button on my form that does something. I want the form to automatically give focus back to the embedded Notepad in the Panel.

Panel1.Focus doesn't work
ActiveControl = Panel1 doesn't work
SetForegroundWindow API doesn't work because FindWindow doesn't work
SetFocus API doesn't work (same as above)

Any advice, or even somewhere to start?

Let me know if I need to produce a working example.

Thanks in advance,
-isey

Last edited by isEy78; 04-02-2015 at 08:57 AM. Reason: Issue resolved
Reply With Quote
  #2  
Old 04-01-2015, 05:11 PM
passel's Avatar
passelWinForm/Panel1/Embedding EXE passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Do you need to use FindWindow?
It seems you would already have the handle, proc.MainWindowHandle.
Don't have time to try to setup a test case to play with.

edit: Went ahead and tested anyway. Worked for me.
p.s. Added a Process component and named it proc.
Two buttons and a panel.
Code:
Imports System.Runtime.InteropServices

Public Class Form1

  <DllImport("user32.dll")> _
  Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
  End Function

  <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
  End Function

  <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
  End Function
  Const WM_SYSCOMMAND = &H112
  Private SC_MAXIMIZE As IntPtr = CType(&HF030, IntPtr)

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Button2.Text = "Host Notepad"
    Button1.Text = "Focus Notepad"
  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    SetForegroundWindow(proc.MainWindowHandle)
  End Sub

  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Proc = Process.Start("notepad.exe")
    Proc.EnableRaisingEvents = True
    Proc.WaitForInputIdle()
    SetParent(proc.MainWindowHandle, Panel1.Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, CType(0, IntPtr))
    Button2.Enabled = False
  End Sub

  Private Sub Proc_Exited(sender As Object, e As System.EventArgs) Handles Proc.Exited
    Button2.Invoke(Sub() Button2.Enabled = True)
  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 04-01-2015 at 05:43 PM.
Reply With Quote
  #3  
Old 04-02-2015, 08:56 AM
isEy78 isEy78 is offline
Freshman
 
Join Date: Nov 2005
Location: Ohio
Posts: 27
Thumbs up

Wow....I seriously cannot thank you enough.

That worked like a champ...I had to change like 3 lines of code in my project. I didn't add Process to my form (mostly cause I didn't know it was an option). Doing that was the key, then I no longer needed FindWindow cause the obj was just there for the taking now. Man, thank you again!
Reply With Quote
  #4  
Old 04-02-2015, 06:01 PM
passel's Avatar
passelWinForm/Panel1/Embedding EXE passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Actually you didn't need to add a Process component to the form.
I didn't originally when I first tested it.
You just needed to declare it at class scope (in the declarations area at the top of the form file) so it would not go out of scope. You could then reference its handle later when you needed.
The main reason I changed to adding the Process component to the form was to make it easier to set up the process Exited event handler by having the IDE generate the sub by selecting the event from the component's properties window.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 04-28-2015, 03:52 PM
isEy78 isEy78 is offline
Freshman
 
Join Date: Nov 2005
Location: Ohio
Posts: 27
Default

After messing with this more....when I try Excel, it isn't working. Just for testing, I tried with "notepad" , "calc" , "mspaint" and they all work fine.

I also tried the code above verbatim, which yields the same result ... works for notepad, but not excel.

I can probably link Excel another way if I have to, but I don't get why this doesn't just work?

I'm using Excel 2013 for what it's worth and VS Ultimate 2013
Reply With Quote
  #6  
Old 04-30-2015, 09:23 PM
passel's Avatar
passelWinForm/Panel1/Embedding EXE passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

I thought I replied, but it looks like it disappeared.
Anyway, I guess Excel is a different animal, based on the number of responses about embedding Excel in a form.
I guess you will have to try a different way of doing it for. I'm not going to be investigating it myself.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #7  
Old 04-30-2015, 10:10 PM
isEy78 isEy78 is offline
Freshman
 
Join Date: Nov 2005
Location: Ohio
Posts: 27
Default

Hey, I'm glad it wasn't just me this time. Ha!

Last edited by isEy78; 04-30-2015 at 10:10 PM. Reason: bad grammar
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
 
WinForm/Panel1/Embedding EXE
WinForm/Panel1/Embedding EXE
 
-->