我有这个Form5,当我加载它时,应该将chrome加载到该窗口表单中(如果chrome已经在运行或未运行,那么它将开始一个新的过程。)
然后的问题是,当我关闭Form5时,然后未从窗口表单中取消嵌入chrome,而您在 Form5_FormClosed 上看到的是应该将窗口还原回任务栏的代码但什么也没发生,有时在100分之一之后仍然有效。我在做什么错了?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TrinityItemCreator
{
public partial class Form5 : Form
{
private string urlDestination;
private Process myprocess;
public Form5(string urlTarget)
{
InitializeComponent();
urlDestination = urlTarget;
}
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
// HWND Constants
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
// P/Invoke
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private void Form5_Load(object sender, EventArgs e)
{
Process[] pname = Process.GetProcessesByName("chrome");
if (pname.Length != 0) // chrome is already opened
{
//Process.Start("chrome", urlDestination);
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == "chrome")
{
SetParent(theprocess.MainWindowHandle, panel2.Handle);
int left = 1;
int top = 1;
int width = 876;
int height = 656;
SetWindowPos(theprocess.MainWindowHandle, HWND_TOP, left, top, width, height, 0);
myprocess = theprocess;
}
}
}
else
{
myprocess = Process.Start("chrome", urlDestination);
Thread.Sleep(1000);
SetParent(myprocess.MainWindowHandle, panel2.Handle);
int left = 1;
int top = 1;
int width = 876;
int height = 656;
SetWindowPos(myprocess.MainWindowHandle, HWND_TOP, left, top, width, height, 0);
}
}
private void Form5_FormClosed(object sender, FormClosedEventArgs e)
{
SetParent(myprocess.MainWindowHandle, IntPtr.Zero);
ShowWindow(myprocess.MainWindowHandle, 3); // maximize back
ShowWindow(myprocess.MainWindowHandle, 2); // minimize to taskbar
Thread.Sleep(1500);
}
private void ButtonMyTemplates_Click(object sender, EventArgs e)
{
Close();
}
}
}