窗体内嵌外部程序的显示,获取控件的图片

本文介绍了一种在C#中将外部程序嵌入到窗体的方法。通过使用SetParent和ShowWindow函数,可以将外部程序如命令行窗口(cmd.exe)嵌入到应用程序的Panel控件中,并展示了具体的实现代码。
#region  窗体内嵌外部程序的显示

[DllImport("User32.dll ", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll ", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

private void button1_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe ";
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//加上这句效果更好    
    p.Start();
    System.Threading.Thread.Sleep(100);//加上,100如果效果没有就继续加大   
    SetParent(p.MainWindowHandle, panel1.Handle); //panel1.Handle为要显示外部程序的容器   
    //ShowWindow(p.MainWindowHandle, 3);
}


#endregion

/// <summary>
/// 获取控件的图片
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
private Bitmap DrawMeToBitmap(Control Obj)
{
    Bitmap bmp = new Bitmap(Obj.Width,Obj.Height);
    Graphics g = Graphics.FromImage(bmp);
    using (Brush backColorBrush = new SolidBrush(Obj.BackColor))
    {
        g.FillRectangle(backColorBrush, new Rectangle(0, 0, bmp.Width, bmp.Height));
        foreach (Control ctl in Obj.Controls)
        {
            using (Bitmap b = new Bitmap(ctl.Width, ctl.Height))
            {
                ctl.DrawToBitmap(b, ctl.ClientRectangle);
                g.DrawImage(b, ctl.Location);
            }
        }
    }
    return bmp;
}

转载于:https://www.cnblogs.com/hdl217/archive/2011/08/28/2156519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值