zoukankan      html  css  js  c++  java
  • c#调用 windows api实现WinForm中嵌入EXE程序

    添加引用:
    
    using System.Diagnostics;
    
    using System.Runtime.InteropServices;
    
     
    
    定义:
    
    Process process = null;
    
    IntPtr appWin;
    
    private string exeName =""; 
    
    [DllImport("user32.dll", EntryPoint ="GetWindowThreadProcessId", SetLastError = true,
    
    CharSet = CharSet.Unicode, ExactSpelling =true,
    
    CallingConvention = CallingConvention.StdCall)]
    
    private staticextern long GetWindowThreadProcessId(long hWnd,long lpdwProcessId); 
    
    [DllImport("user32.dll", SetLastError =true)]
    
    private staticextern IntPtr FindWindow(string lpClassName,string lpWindowName); 
    
    [DllImport("user32.dll", SetLastError =true)]
    
    private staticextern long SetParent(IntPtr hWndChild,IntPtr hWndNewParent); 
    
    [DllImport("user32.dll", EntryPoint ="GetWindowLongA", SetLastError = true)]
    
    private staticextern long GetWindowLong(IntPtr hwnd,int nIndex); 
    
    [DllImport("user32.dll", EntryPoint ="SetWindowLongA", SetLastError = true)]
    
    private staticextern long SetWindowLong(IntPtr hwnd,int nIndex, long dwNewLong);
    
    //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); 
    
    [DllImport("user32.dll", SetLastError =true)]
    
    private staticextern long SetWindowPos(IntPtr hwnd,long hWndInsertAfter, long x,long y, long cx, long cy, long wFlags); 
    
    [DllImport("user32.dll", SetLastError =true)]
    
    private staticextern bool MoveWindow(IntPtr hwnd,int x, int y, int cx, int cy, bool repaint); 
    
    [DllImport("user32.dll", EntryPoint ="PostMessageA", SetLastError = true)]
    
    private staticextern bool PostMessage(IntPtr hwnd,uint Msg, long wParam,long lParam); 
    
    private constint SWP_NOOWNERZORDER = 0x200;
    
     private const int SWP_NOREDRAW = 0x8;
    
    private constint SWP_NOZORDER = 0x4;
    
    private constint SWP_SHOWWINDOW = 0x0040;
    
    private constint WS_EX_MDICHILD = 0x40;
    
    private constint SWP_FRAMECHANGED = 0x20;
    
    private constint SWP_NOACTIVATE = 0x10;
    
    private constint SWP_ASYNCWINDOWPOS = 0x4000;
    
    private constint SWP_NOMOVE = 0x2;
    
    private constint SWP_NOSIZE = 0x1;
    
    private constint GWL_STYLE = (-16);
    
    private constint WS_VISIBLE = 0x10000000;
    
    private constint WM_CLOSE = 0x10;
    
    private constint WS_CHILD = 0x40000000;
    
     
    
    public string ExeName
    
    {
    
    get
    
    {
    
    return exeName;
    
    }
    
    set
    
    {
    
    exeName = value;
    
    }
    
    } 
    
    
    
    调用:
    
     this.exeName = @"C:/Program Files/Kingview/Touchvew.exe";
    
    try
    
    {
    
    // Start the process 
    
    process = System.Diagnostics.Process.Start(this.exeName); 
    
    // Wait for process to be created and enter idle condition
    
    process.WaitForInputIdle(); 
    
    // Get the main handle
    
    appWin = process.MainWindowHandle;
    
    }
    
    catch (Exception ex)
    
    {
    
    MessageBox.Show(this, ex.Message,"Error");
    
    } 
    
    // Put it into this form
    
    SetParent(appWin, this.splitContainer1.Panel2.Handle); 
    
    // Remove border and whatnot
    
    // SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE); 
    
    // Move the window to overlay it on this window
    
    MoveWindow(appWin, 0, 0, this.Width, this.Height, true); 
    
    在控件改变大小的时候,调用:
    
    private void splitContainer1_Panel2_Resize(object sender,EventArgs e)
    
    {
    
    if (this.appWin !=IntPtr.Zero)
    
    {
    
    MoveWindow(appWin, 0, 0, this.splitContainer1.Panel2.Width,this.splitContainer1.Panel2.Height, true);
    
    }
    
    base.OnResize(e);
    
    }
    
     
    
    窗口退出时:
    
    private void Form1_FormClosed(object sender,FormClosedEventArgs e)
    
    {
    
    process.Kill();
    
    // Stop the application
    
    //if (appWin != IntPtr.Zero)
    
    //{ 
    
    //// Post a colse message
    
    ////PostMessage(appWin, WM_CLOSE, 0, 0); 
    
    //// Delay for it to get the message
    
    //// System.Threading.Thread.Sleep(1000); 
    
    //// Clear internal handle
    
    //appWin = IntPtr.Zero; 
    
    //} 
    
    //base.FormClosed(e); 
    
    } 
    待人以诚,做事用心,对事不对人.
  • 相关阅读:
    一个bug案例分析
    《需求工程》阅读随笔-1.做什么和怎么做
    连贯接口(fluent interface)的Java实现及应用。
    代码覆盖率检测工具大全
    腾讯的一个移动端测试小工具GT
    用复制mysql/data 文件夹 下面的数据库的形式来复制数据库出现的问题
    淘客API升级后的解决方案,怎么采集淘宝的商品数据
    方维团购系统,给供货商添加省市地址
    支付宝担保交易收款接口使用
    方维分享系统首页,插入新品,用来做优化
  • 原文地址:https://www.cnblogs.com/jiangguang/p/2761132.html
Copyright © 2011-2022 走看看