知道一个窗体的句柄,如何获取这个窗体
当我知道这个窗体的句柄时,如何获取得到该窗体Form的实例呢?现有两个.net写的可执行程序A.exe和B.exe,其中在B中有这样的语句.根据gihelo的回答:Int...
当我知道这个窗体的句柄时,如何获取得到该窗体Form的实例呢?
现有两个.net写的可执行程序A.exe和B.exe,其中在B中有这样的语句. 根据gihelo的回答:
IntPtr hWnd = ***;
frm = (Control)Form.FromHandle(hWnd);
如果hWnd的值为B中的窗口句柄,则frm能返回正确的实例.(即当前例程里的窗体)
但是如果hWnd的值为A(或B以外的例程)中的窗口句柄,则frm返回null.
请问:如果hWnd的值为A(或B以外的例程)中的窗口句柄,怎样才能得到正确的frm实例 展开
现有两个.net写的可执行程序A.exe和B.exe,其中在B中有这样的语句. 根据gihelo的回答:
IntPtr hWnd = ***;
frm = (Control)Form.FromHandle(hWnd);
如果hWnd的值为B中的窗口句柄,则frm能返回正确的实例.(即当前例程里的窗体)
但是如果hWnd的值为A(或B以外的例程)中的窗口句柄,则frm返回null.
请问:如果hWnd的值为A(或B以外的例程)中的窗口句柄,怎样才能得到正确的frm实例 展开
展开全部
var frm = (Control)Form.FromHandle(h);
h为句柄,我这里强转成control了,你可以自己看情况改动
我原来写过一个例子,调用cmd.exe窗口,并嵌入到自己的窗体内,你可以参考一下
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
[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);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//Process p = null;
// p = System.Diagnostics.Process.Start("c:\\windows\\system32\\cmd.exe");
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"c:\\windows\\system32\\cmd.exe", null, ".", SW_HIDE); // 让CtrlDemo.exe运行在PANEL里
IntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe");
//关键在这里
var frm = (Control)Form.FromHandle(h);
//使你的Form可以嵌入别的容器
//frm.Visible = true;
SetParent(h, this.panel1.Handle); //嵌套到panel1内
SendMessage(h.ToInt32(), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
ShowWindow(h.ToInt32(), SW_SHOW);
}
}
}
h为句柄,我这里强转成control了,你可以自己看情况改动
我原来写过一个例子,调用cmd.exe窗口,并嵌入到自己的窗体内,你可以参考一下
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
[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);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//Process p = null;
// p = System.Diagnostics.Process.Start("c:\\windows\\system32\\cmd.exe");
ShellExecute(this.panel1.Handle.ToInt32(), "open", @"c:\\windows\\system32\\cmd.exe", null, ".", SW_HIDE); // 让CtrlDemo.exe运行在PANEL里
IntPtr h = FindWindow(null, "c:\\windows\\system32\\cmd.exe");
//关键在这里
var frm = (Control)Form.FromHandle(h);
//使你的Form可以嵌入别的容器
//frm.Visible = true;
SetParent(h, this.panel1.Handle); //嵌套到panel1内
SendMessage(h.ToInt32(), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
ShowWindow(h.ToInt32(), SW_SHOW);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
form a =new form ();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询