解决方案 »

  1.   

    参考: C#获取进程的主窗口句柄using System;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Runtime.InteropServices;namespace CloseWindow
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class MainForm : Form
    {
    /// <summary>
    /// Designer variable used to keep track of non-visual components.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Disposes resources used by the form.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing) {
    if (components != null) {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent()
    {
    this.btnClose = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // btnClose
    // 
    this.btnClose.Location = new System.Drawing.Point(13, 13);
    this.btnClose.Name = "btnClose";
    this.btnClose.Size = new System.Drawing.Size(75, 23);
    this.btnClose.TabIndex = 0;
    this.btnClose.Text = "Close";
    this.btnClose.UseVisualStyleBackColor = true;
    this.btnClose.Click += new System.EventHandler(this.BtnCloseClick);
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 261);
    this.Controls.Add(this.btnClose);
    this.Name = "MainForm";
    this.ShowInTaskbar = false;
    this.Text = "CloseWindow";
    this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button btnClose;


    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    } private bool haveMainWindow = false;
    private IntPtr mainWindowHandle = IntPtr.Zero;
    private int processId = 0; public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); public IntPtr GetMainWindowHandle(int processId)
    {
    if (!this.haveMainWindow)
    {
    this.mainWindowHandle = IntPtr.Zero;
    this.processId = processId;
    EnumThreadWindowsCallback callback = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
    EnumWindows(callback, IntPtr.Zero);
    GC.KeepAlive(callback); this.haveMainWindow = true;
    }
    return this.mainWindowHandle;
    } private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
    {
    int num;
    GetWindowThreadProcessId(new HandleRef(this, handle), out num);
    if ((num == this.processId) && this.IsMainWindow(handle))
    {
    this.mainWindowHandle = handle;
    return false;
    }
    return true;
    } private bool IsMainWindow(IntPtr handle)
    {
    return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero)/* && IsWindowVisible(new HandleRef(this, handle))*/);
    } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd); [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool IsWindowVisible(HandleRef hWnd);

    [DllImport("user32.dll")]
    public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
    void BtnCloseClick(object sender, EventArgs e)
    {
    processId = Process.GetCurrentProcess().Id;
    //获取句柄
    IntPtr hWnd = GetMainWindowHandle(processId);
    PostMessage(hWnd, /*WM_CLOSE*/0x0010, 0, 0);
    }
    }
    }