这个问题确实很难...希望高手进来帮忙看看...

hylovett 2009-02-26 05:21:28
怎样让另外一个程序,运行在本程序的PANEL里面呢? 我找了一段代码,但是没有达到效果:
[DllImport("user32", EntryPoint = "SetParent", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32", EntryPoint = "FindWindowA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
[DllImport("shell32.dll", EntryPoint = "ShellExecuteA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShellExecute(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

private const int WM_SYSCOMMAND = 0x112;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_MINIMIZE = 0xF020;
private const int SC_RESTORE = 0xF120;
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;

[DllImport("user32.dll ", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShowWindow(int hwnd, int nCmdShow);

private void button1_Click(object sender, EventArgs e)
{
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"D:\MY Program\CtrlDemo\CtrlDemo\bin\Debug\CtrlDemo.exe", null, ".", SW_HIDE); // 让CtrlDemo.exe运行在PANEL里面
IntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe ");
SetParent(h, this.panel1.Handle); //嵌套到panel1内
SendMessage(h.ToInt32(), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
ShowWindow(h.ToInt32(), SW_SHOW);
}


点击按钮后,PANEL里面没有任何东西,只在进程里面多了一个ctrldemo.exe但是也看不到窗体。请有人可以帮忙试一下吗?
...全文
552 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
hylovett 2009-02-27
  • 打赏
  • 举报
回复
谢谢各位,散分。
shalen520 2009-02-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hylovett 的回复:]
还是没找到原因,为什么比如像"扫雷","WORD",之类的程序就可以,但是像QQ,.NET写的程序,几乎都不能实现,有办法可以解决吗?
[/Quote]
因为word一类的程序有OLE接口
hylovett 2009-02-27
  • 打赏
  • 举报
回复
还是没找到原因,为什么比如像"扫雷","WORD",之类的程序就可以,但是像QQ,.NET写的程序,几乎都不能实现,有办法可以解决吗?
homejiji 2009-02-26
  • 打赏
  • 举报
回复
mark
I_am_Z 2009-02-26
  • 打赏
  • 举报
回复
up
hylovett 2009-02-26
  • 打赏
  • 举报
回复
谁帮我仔细看看。有没有办法改进一下?让他在本程序中运行其他任何程序?
fireswallow 2009-02-26
  • 打赏
  • 举报
回复
关注中。。
怎么有两个exe
hylovett 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wanghui0380 的回复:]
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"D:\MY Program\CtrlDemo\CtrlDemo\bin\Debug\CtrlDemo.exe", null, ".", SW_HIDE); ntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe ");

你查询的句柄咋是两个不同滴东东呢?

我把第一个也改成c:\\windows\\system32\\cmd.exe 就可以运行出这个效果了
[/Quote]

我也这样弄过.但确实不行...进程里面倒是有..我还试过QQ.之类的很多程序都不行...

http://www.codeproject.com/KB/miscctrl/AppControl.aspx 这是有时候可以.但大多数时候都不行.
fireswallow 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wanghui0380 的回复:]
http://www.codeproject.com/KB/miscctrl/AppControl.aspx
[/Quote]

有的程序可以 我试了一下 我的那个exe 就不行 不过Uedit32。exe
可以

hylovett 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wanghui0380 的回复:]
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"D:\MY Program\CtrlDemo\CtrlDemo\bin\Debug\CtrlDemo.exe", null, ".", SW_HIDE); ntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe ");

你查询的句柄咋是两个不同滴东东呢?

我把第一个也改成c:\\windows\\system32\\cmd.exe 就可以运行出这个效果了
[/Quote]

也不行.......

另外:3楼的代码也报错: //关键在这里
Form frm = Form.FromHandle(h);
无法将类型“System.Windows.Forms.Control”隐式转换为“System.Windows.Forms.Form”。存在一个显式转换(是否缺少强制转换?)
ls3697264 2009-02-26
  • 打赏
  • 举报
回复
UP
hylovett 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wanghui0380 的回复:]
http://www.codeproject.com/KB/miscctrl/AppControl.aspx
[/Quote]

这个程序不是100%好用,很多时候,IE都不能在PENAL里面运行
wanghui0380 2009-02-26
  • 打赏
  • 举报
回复
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"D:\MY Program\CtrlDemo\CtrlDemo\bin\Debug\CtrlDemo.exe", null, ".", SW_HIDE); ntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe ");

你查询的句柄咋是两个不同滴东东呢?

我把第一个也改成c:\\windows\\system32\\cmd.exe 就可以运行出这个效果了
xianguang321 2009-02-26
  • 打赏
  • 举报
回复
cmd.exe好像不是个Form,这个倒没在意
fireswallow 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xianguang321 的回复:]
C# code[DllImport("user32", EntryPoint="SetParent", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]publicstaticexternintSetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32", EntryPoint="FindWindowA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]publicstaticexternIntPtr FindWindow(stringlpClassName,stringlpWindowName);
[DllImport("us…
[/Quote]
//关键在这里
Form frm = Form.FromHandle(h);
报错
Error 4 Cannot implicitly convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.Form'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form6.cs 40 24 WindowsFormsApplication2
悔说话的哑巴 2009-02-26
  • 打赏
  • 举报
回复
要以OLE的方式嵌入
xianguang321 2009-02-26
  • 打赏
  • 举报
回复
你试试看,这样可以吗?
xianguang321 2009-02-26
  • 打赏
  • 举报
回复
 
[DllImport("user32", EntryPoint = "SetParent", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32", EntryPoint = "FindWindowA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
[DllImport("shell32.dll", EntryPoint = "ShellExecuteA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShellExecute(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
private const int WM_SYSCOMMAND = 0x112;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_MINIMIZE = 0xF020;
private const int SC_RESTORE = 0xF120;
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
[DllImport("user32.dll ", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShowWindow(int hwnd, int nCmdShow);
private void button1_Click(object sender, EventArgs e)
{
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"D:\MY Program\CtrlDemo\CtrlDemo\bin\Debug\CtrlDemo.exe", null, ".", SW_HIDE); // 让CtrlDemo.exe运行在PANEL里面
IntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe ");
//关键在这里
Form frm = Form.FromHandle(h);
//使你的Form可以嵌入别的容器
frm.TopLevel = false;

SetParent(h, this.panel1.Handle); //嵌套到panel1内
SendMessage(h.ToInt32(), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
ShowWindow(h.ToInt32(), SW_SHOW);
}
shalen520 2009-02-26
  • 打赏
  • 举报
回复
要以OLE的方式嵌入
wanghui0380 2009-02-26
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/miscctrl/AppControl.aspx
加载更多回复(1)

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧