Winform嵌入exe程序(Win API练习)
1
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8
//先添加如下引用:9
using System.Diagnostics;10
using System.Runtime.InteropServices;11

12
namespace WindowsApplication113


{14
public partial class Form1 : Form15

{16
public Form1()17

{18
InitializeComponent();19
}20

21

WIN API---引入系统API,用于把程序打开的窗口整合到SHOW页面中#region WIN API---引入系统API,用于把程序打开的窗口整合到SHOW页面中22

宏定义#region 宏定义23
private const int SWP_NOOWNERZORDER = 0x200;24
private const int SWP_NOREDRAW = 0x8;25
private const int SWP_NOZORDER = 0x4;26
private const int SWP_SHOWWINDOW = 0x0040;27
private const int WS_EX_MDICHILD = 0x40;28
private const int SWP_FRAMECHANGED = 0x20;29
private const int SWP_NOACTIVATE = 0x10;30
private const int SWP_ASYNCWINDOWPOS = 0x4000;31
private const int SWP_NOMOVE = 0x2;32
private const int SWP_NOSIZE = 0x1;33
private const int GWL_STYLE = (-16);34
private const int WS_VISIBLE = 0x10000000;35
private const int WM_CLOSE = 0x10;36
private const int WS_CHILD = 0x40000000;37
#endregion38
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,39
CharSet = CharSet.Unicode, ExactSpelling = true,40
CallingConvention = CallingConvention.StdCall)]41
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);42

43
[DllImport("user32.dll", SetLastError = true)]44
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);45

46
[DllImport("user32.dll", SetLastError = true)]47
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);48

49
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]50
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);51

52
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]53
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);54
//private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);55

56

57
[DllImport("user32.dll", SetLastError = true)]58
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);59

60
[DllImport("user32.dll", SetLastError = true)]61
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);62

63
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]64
private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);65

66

67
#endregion68

69
Process process = null;70
IntPtr appWin;71
private string exeName = "";72
//在控件改变大小的时候,调用:73
private void splitContainer1_Panel2_Resize(object sender, EventArgs e)74

{75
if (this.appWin != IntPtr.Zero)76

{77
MoveWindow(appWin, 0, 0, this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height, true);78
}79
base.OnResize(e);80
}81

82
// 3 用Process执行其它程序,传入打开窗口命令,运行进程并放入C#窗口中83
private void Form1_Load(object sender, EventArgs e)84

{85
exeName = @"C:\WINDOWS\system32\notepad.exe";86
try87

{88
// Start the process 89
process = System.Diagnostics.Process.Start(this.exeName);90

91
// Wait for process to be created and enter idle condition 92
process.WaitForInputIdle();93

94
// Get the main handle95
appWin = process.MainWindowHandle;96
}97
catch (Exception ex)98

{99
MessageBox.Show(this, ex.Message, "Error");100
}101

102
// Put it into this form103
SetParent(appWin, this.splitContainer1.Panel2.Handle);104

105
// Remove border and whatnot106
// SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);107
// Move the window to overlay it on this window108
MoveWindow(appWin, 0, 0, this.Width, this.Height, true);109
}110

111
//窗口退出时:112
private void Form1_FormClosed(object sender, FormClosedEventArgs e)113

{114
if (process != null && !process.HasExited)115
process.Kill();116

117
118
}119

120

121
}122
}
浙公网安备 33010602011771号