假设有两个程序,分别为“A.exe”、“B.exe”,分别是来自两个不同的解决方案,我在A中用Process.Start("B.exe");   实现了启动同一目录下的B,此时如何保持B始终在A之前,如果B窗口不取消,则无法在A上操作?
ps:可能表达地不清楚,平时在一些程序中也碰到过类似的,就像这样此时再点击“系统属性”窗口无法选中。WinForm前端

解决方案 »

  1.   

    你截图里面的是ShowDialog,模态对话框。
    你说的两个exe是两个独立的进程,不一样的场景。
    你要这个效果的话,可能要换一下思路了,楼下请继续~
      

  2.   

    在A程序的Form1里,获取到B程序的窗口Form2,然后
            private void Form1_Activated(object sender, EventArgs e)
            {
                 Form2.BringToFront(); 
            }
      

  3.   

    找到目标程序主窗口(比如用FindWindow)
    然后用SetParent将它的父窗口设置为第一个窗口,那么它永远在第一个窗口前面。
      

  4.   

    或者,把Process.Start("B.exe")改为反射,然后ShowDialog
      

  5.   

    请问大侠,B里的的窗口在A程序中又要如何获取呢这样可以不,你测试一下,我没有测试过!            Form.FromHandle(process.MainWindowHandle).BringToFront();
      

  6.   

    [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern IntPtr FindWindow(string strclassName, string strWindowName);
    IntPtr hWnd = (IntPtr)FindWindow(null, Title);
                if (hWnd != IntPtr.Zero)
                {
                    bool isIcon = IsIconic(hWnd);
                    if (!isIcon)
                    {
                        SetForegroundWindow(hWnd);
                    }
                    else
                    {
                        OpenIcon(hWnd);
                    }
                }
    title就是要激活的窗口标题
      

  7.   

    你既然是不同解决方案的,用的是进程访问的EXE文件么?
    那用委托吧
    启动前将主画面设为this.Enabled =FALSEp.SynchronizingObject = this;
                        p.Exited += (sender, e) =>
                        {
                                this.Enabled = true;
                            };
                        p.EnableRaisingEvents = true;
      

  8.   

    请问这个OpenIcon方法是从哪里来的?
      

  9.   

    -----------------声明三个API:FindWindow,SetParent,SetForegroundWindow
    调用计算器为例: Process.Start("calc");
                Thread.Sleep(1000);
                IntPtr hwnd = FindWindow(null, "计算器");
                if (hwnd != IntPtr.Zero)
                {
                    SetForegroundWindow(hwnd);
                    //Process.Start(@"C:\Documents and Settings\10989889a\桌面\反汇编工具.exe");
                    //IntPtr hWnd = (IntPtr)FindWindow(null, "打开文件 - 安全警告");
                    SetParent(hwnd, this.Handle);
                    ////DialogBoxParam(hWnd, null, this.Handle, IntPtr.Zero, IntPtr.Zero);
                }
      

  10.   

    -----------------声明三个API:FindWindow,SetParent,SetForegroundWindow
    调用计算器为例: Process.Start("calc");
                Thread.Sleep(1000);
                IntPtr hwnd = FindWindow(null, "计算器");
                if (hwnd != IntPtr.Zero)
                {
                    SetForegroundWindow(hwnd);
                    //Process.Start(@"C:\Documents and Settings\10989889a\桌面\反汇编工具.exe");
                    //IntPtr hWnd = (IntPtr)FindWindow(null, "打开文件 - 安全警告");
                    SetParent(hwnd, this.Handle);
                    ////DialogBoxParam(hWnd, null, this.Handle, IntPtr.Zero, IntPtr.Zero);
                }
    sorry。你理解错误了
      

  11.   


    [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern bool OpenIcon(IntPtr hWnd);        [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern bool IsIconic(IntPtr hWnd);        [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern int SetForegroundWindow(IntPtr hWnd);