我用Process要开Notepad.exe,想让它只开一个,如果打开了,就让它的主窗口在桌面最前面激活??

解决方案 »

  1.   

    在你的项目中引入下面的API函数
    [DllImport("User32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);   
    ...........................
    定义一个全局变量
    Process mNotePad ;
    // 判断notepad是否已经运行了
               Process[] tmpPro = System.Diagnostics.Process.GetProcessesByName("Notepad");
                        if (tmpPro.Length == 0)// 判断是否已经运行了,
                        {
                            mNotePad = Process.Start(“Notepad.exe”);
                            
                        }
                        else
                        {
                            SetForegroundWindow(mNotePad.MainWindowHandle);
                        }
      

  2.   

    行的,文件头加上
    using System.Runtime.InteropServices;
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using StockMain;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;namespace StockMain
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            Process instance = RunningInstance();
                if (instance == null)
                {
                    Application.Run(new fmMain());
                    //Application.Run(new FormTest());
                }
                else 
                {
                    HandleRunningInstance(instance);
                }
            }        public static Process RunningInstance()
            {
                Process current = Process.GetCurrentProcess();
                Process[] processes = Process.GetProcessesByName(current.ProcessName);            //遍历正在有相同名字运行的例程
                foreach (Process process in processes)
                {
                    if (process.Id!=current.Id)
                    {
                        if (Assembly.GetExecutingAssembly().Location.Replace("/","\\")==current.MainModule.FileName)
                        {
                            return process;   
                        }
                    }
                }
                return null;
            }        public static void HandleRunningInstance(Process instance)
            { 
                ////确保窗口没有被最小化或最大化
                //ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
                //设置真实例程为foreground window
                SetForegroundWindow(instance.MainWindowHandle);
            }        [DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
            [DllImport("User32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            private const int WS_SHOWNORMAL = 1;
        }
      

  4.   

             Process[] processes = Process.GetProcessesByName("notepad");            if (processes.Length == 0)
                {
                    mNotePad = Process.Start("notepad");            }
                else
                {
                    mNotePad = processes[0];
                    SetForegroundWindow(mNotePad.MainWindowHandle);
                }
         窗口最小化就不能激活了,要加楼上的这一段
    ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
    就能全部解决了
      

  5.   

    不行呀,,,不能把Notepad显示到最前面,老是在状态栏中呢
      

  6.   

    likegod[DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);这个函数没有用上呢??
    帮我举个打开NotePad的示例吧
      

  7.   


    补充:在外围加上
    DllImport("user32.dll")]
         private static extern
                bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
      private const int SW_SHOWNORMAL = 1;并且ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); 中WS_SHOWNORMAL改为SW_SHOWNORMAL