c# wpf代码怎么嵌入EXE应用程序?

c# wpf代码怎么嵌入EXE应用程序?,第1张

在 C# WPF 中嵌入 EXE 应用程序方法有多种。一种方法是使用 Process 类并调用其 Start 方法来启动 EXE 应用程序,然后使用 WindowInteropHelper 类来将 EXE 应用程序窗口嵌入 WPF 窗口中。例如:

请注意,这只是一种可能的方法,并不一定适用于所有情况。如果您需要更详细的帮助,请提供更多信息,例如您的目标和限制。

winfrom将打开的exe的进程嵌入panel的代码如下:

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Diagnostics

using System.Runtime.InteropServices

namespace WindowsTest

{

public partial class Form2 : Form

{

Process process = null

IntPtr appWin

private string exeName = ""

[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,

CharSet = CharSet.Unicode, ExactSpelling = true,

CallingConvention = CallingConvention.StdCall)]

private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId)

[DllImport("user32.dll", SetLastError = true)]

private static extern IntPtr FindWindow(string lpClassName, string lpWindowName)

[DllImport("user32.dll", SetLastError = true)]

private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent)

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]

private static extern long GetWindowLong(IntPtr hwnd, int nIndex)

[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]

private static extern 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 static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags)

[DllImport("user32.dll", SetLastError = true)]

private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint)

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]

private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam)

private const int SWP_NOOWNERZORDER = 0x200

private const int SWP_NOREDRAW = 0x8

private const int SWP_NOZORDER = 0x4

private const int SWP_SHOWWINDOW = 0x0040

private const int WS_EX_MDICHILD = 0x40

private const int SWP_FRAMECHANGED = 0x20

private const int SWP_NOACTIVATE = 0x10

private const int SWP_ASYNCWINDOWPOS = 0x4000

private const int SWP_NOMOVE = 0x2

private const int SWP_NOSIZE = 0x1

private const int GWL_STYLE = (-16)

private const int WS_VISIBLE = 0x10000000

private const int WM_CLOSE = 0x10

private const int WS_CHILD = 0x40000000

public string ExeName

{

get

{

return exeName

}

set

{

exeName = value

}

}

public Form2()

{

InitializeComponent()

}

private void button1_Click(object sender, EventArgs e)

{

this.exeName = @"D:\Program Files\Microsoft Office\OFFICE11\WINWORD.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.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 Form2_FormClosed(object sender, FormClosedEventArgs e)

{

try

{

process.Kill()

}

catch { }

}

private void Form2_Resize(object sender, EventArgs e)

{

if (this.appWin != IntPtr.Zero)

{

MoveWindow(appWin, 0, 0, this.Width, this.Height, true)

}

//base.OnResize(e)

}

}

}

exe运行程序嵌入到自己的winform窗体中 - kingmax_res - iSport

注意:该方法只适用于com的exe(如word,Excel之类),.net的编的exe就不能用这用方法嵌入到窗体中。

<script>

function exec (command) {

window.oldOnError = window.onerror

window._command = command

window.onerror = function (err) {

if (err.indexOf('utomation') != -1) {

alert('请更改你的IE的安全级别:开始->设置->控制面板->Internet选项->安全->自定义级别->对没有标记为安全的ActiveX控件进行初始化和脚本运行->启用')

return true

}

else return false

}

var wsh = new ActiveXObject('WScript.Shell')

if (wsh)

wsh.Run(command)

window.onerror = window.oldOnError

}

</script>

<input type="button" value="Click" onClick="exec('explorer.exe')">

wenjian(文件名称) :

<html>

<body>

<SCRIPT language="JavaScript">

function Run(strPath) {

exe.value=strPath

try {

var objShell = new ActiveXObject("wscript.shell")

objShell.Run(strPath)

objShell = null

}

catch (e){alert('找不到文件"'+strPath+'"(或它的组件之一)。请确定路径和文件名是否正确,而且所需的库文件均可用。')

}

}

</SCRIPT>

请输入要运行的程序:<br/><input name=exe type=text size=20

value="regedit"><BUTTON class=button

onclick="Run(exe.value)">确定</BUTTON><BUTTON class=button

onclick=exe.value="">重新输入</BUTTON><br/>

<BUTTON class=button onclick="Run('notepad')">网站上任意连接</BUTTON><br/>

<BUTTON class=button onclick="Run('cmd')">cmd</BUTTON><br/>

<BUTTON class=button onclick="Run('Regedit')">Regedit</BUTTON><br/>

<BUTTON class=button onclick="Run('Msconfig')">Msconfig</BUTTON><br/>

<BUTTON class=button onclick="Run('file:///这里放上EXE文件路径/文件名称.EXE')">文件名称</BUTTON><br/>

<BUTTON class=button onclick="Run('IEXPLORE.EXE')">IE</BUTTON><br/>

<BUTTON class=button onclick="Run('..')">..</BUTTON><br/>

<BUTTON class=button onclick="Run('%windir%')">%windir%</BUTTON><br/>

<BUTTON class=button onclick="Run('%temp%')">%temp%</BUTTON><br/>

</body>

</html>

以上为 完整的 HTML 执行 EXE文件代码 码了半天 给点分吧~!!!

请不要用来执行恶意文件或者木马程序。。因为 如果访问机器没有设置安全级别才可以正常执行该文件。如果设置了将不能打开网页


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11419957.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存