项目需要判断程序只能运行一个!在打开另外一个程序的时候,显示并前置已打开的程序! 目前可以使程序只能运行一个,但不知道怎么设置前置!本人新手,求高手指点一二....

解决方案 »

  1.   

    单实例:
    http://topic.csdn.net/t/20050516/14/4010565.html窗口前置:用SetForegroundWindow函数[DllImport("user32.dll ")] 
          public static extern IntPtr  FindWindow(string 类名, string 程序标题);     [DllImport( "user32.dll ")]  
          private  static  extern  bool  SetForegroundWindow(IntPtr hWnd); 
    private void button3_Click(object sender, EventArgs e) 
            { 
                IntPtr hwnd = FindWindow(null, "新建 文本文档 (6).txt - 记事本"); //放回句柄 
                  int WM_PASTE = // 具体的消息号你可以网上查下
                  if (hwnd.ToInt32 () != 0) 
                { 
                SetForegroundWindow(hwnd); 
               SendMessage(hwnd,WM_PASTE,0,0); // 具体的消息原型你可以网上查下
                } 
                else { MessageBox.Show("记事本没有运行 "); } 
              
            }
      

  2.   

    代码都给你了,其实自己组合一下就行了。。[DllImport("user32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
            [DllImport("user32.dll")]
            private static extern bool IsIconic(IntPtr hWnd);
            private const int SW_RESTORE = 9;        public void RaiseOtherProcess()
            {
                Process proc = Process.GetCurrentProcess();
                Process.GetProcesses();
                foreach (Process otherProc in
                    Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
                {
                    //ignore "this" process
                    if (proc.Id != otherProc.Id)
                    {
                        // Found a "same named process".
                        // Assume it is the one we want brought to the foreground.
                        // Use the Win32 API to bring it to the foreground.
                        IntPtr hWnd = otherProc.MainWindowHandle;
                        if (IsIconic(hWnd))
                        {
                            ShowWindowAsync(hWnd, 9);
                        }
                        SetForegroundWindow(hWnd);
                        break;
                    }
                }
            }
    用mutex实现互斥。
    当互斥发生时调用以上代码,实现:
    2.如果程序已经存在,且最小化,则还原那个程序。
    3.如果程序已经存在,且不是最小化(最大化或正常状态),则显示(注意:不是还原!)那个程序。
      

  3.   

    [DllImport("user32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
            [DllImport("user32.dll")]
            private static extern bool IsIconic(IntPtr hWnd);
            private const int SW_RESTORE = 9;
    有错误,系统找不到!
    这些需要引用什么名称空间吗?DLL文件时系统的吗?
      

  4.   

    知道了,谢谢了yfqvip(满衣兄), 祝你新婚快乐! 幸福永远!!!