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;
using System.Threading;
  [DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowName);[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);[DllImport("user32.dll")]
public static extern bool OpenIcon(IntPtr hWnd);
for (int i = 1; i <= number; i++)
                        {
                            textBox1.Text = folderBrowserDialog1.SelectedPath+"\\";
                            string path = textBox1.Text + "nihao(" + i + ").jpg";                            Process.Start("mspaint.exe", path);
                            string cap = "nihao(" + i + ").jpg" + " - 画图";                            IntPtr handle = FindWindow(null, cap);
                            if (handle != IntPtr.Zero)
                            {
                                //MessageBox.Show("nihao");
                                if (IsIconic(handle))
                                {
                                    OpenIcon(handle);
                                }
                                else
                                {
                                    SetForegroundWindow(handle);
                                    Thread.Sleep(2000);
                                    
                                }
                            }
                            //SendKeys.Send("^S");                             SendKeys.Send("%{F4}");                        }
关掉的是主窗口,而不是画图窗口。

解决方案 »

  1.   

    SendKeys.Send("%{F4}"); 并没有send到mspaint的窗口上,而是send到你的主窗口了。
    要想关掉mapaint,需要postmessage。
      

  2.   

    handle 为IntPtr.Zero,所以没能激活画图窗口,为什么handle为intptr.zero呢。
      

  3.   

    程序刚刚创建,窗口可能还没来得及显示。试试用Process.WaitForInputIdle()等它一下。Process process = Process.Start("mspaint.exe", path); 
    process.WaitForInputIdle();                              //<---
    string cap = "nihao(" + i + ").jpg" + " - 画图"; 
    ...
      

  4.   


    不要总是说“还是不行啊”
    它没有增加有用的信息。要学会怎么定位错误。
    比如如果handle还是零,那就试试开一个固定的窗口,检测FindWindow是否工作正常...