表單控制代碼強轉成control

來源:互聯網
上載者:User
 
摘自:http://zhidao.baidu.com/question/96926630.html?si=1
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);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.