System.Diagnostics.Process p = new System.Diagnostics.Process();//创建进程
        //       p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
        //        p.Start();//开始进程
有没有办法 把进程SoftBoard 像设置窗体TopMost=true一样 把这个进程设置成处于其他窗体之上

解决方案 »

  1.   

    http://social.msdn.microsoft.com/Forums/zh-HK/vbgeneral/thread/2dbdef52-30cc-4f18-b32d-2907adaa7beb
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

       private void button1_Click(object sender, EventArgs e)
            {
                RECT _Rect =new RECT();            GetWindowRect(窗体句柄, ref _Rect);            SetWindowPos(窗体句柄, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);
            }        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);        [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }这样看看
      

  3.   

    int id;
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
            public static extern int FindWindow(
                string lpClassName,
                string lpWindowName
            );
            [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
            public static extern int GetWindowThreadProcessId(
                int hwnd,
                out  int lpdwProcessId
            );hWnd = FindWindow(null, "SoftBoard");
                if (hWnd == 0)
    {//没启动}
    else
    {
                GetWindowThreadProcessId(hWnd, out  id);
    }
      

  4.   

    public class User32API
    {
        private static Hashtable processWnd = null;    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);    static User32API()
        {
            if (processWnd == null)
           {
                processWnd = new Hashtable();
            }
        }    [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);    [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);    [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);    [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);    [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(uint dwErrCode);    public static IntPtr GetCurrentWindowHandle()
        {
            IntPtr ptrWnd = IntPtr.Zero;
            uint uiPid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID
            object objWnd = processWnd[uiPid];        if (objWnd != null)
            {
                ptrWnd = (IntPtr)objWnd;
                if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄
                {
                    return ptrWnd;
                }
                else
                {
                    ptrWnd = IntPtr.Zero;
                }
            }        bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
            // 枚举窗口返回 false 并且没有错误号时表明获取成功
            if (!bResult && Marshal.GetLastWin32Error() == 0)
            {
                objWnd = processWnd[uiPid];
                if (objWnd != null)
                {
                    ptrWnd = (IntPtr)objWnd;
                }
            }        return ptrWnd;
        }    private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
        {
            uint uiPid = 0;        if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref uiPid);
                if (uiPid == lParam)    // 找到进程对应的主窗口句柄
                {
                    processWnd[uiPid] = hwnd;   // 把句柄缓存起来
                    SetLastError(0);    // 设置无错误
                    return false;   // 返回 false 以终止枚举窗口
                }
            }        return true;
        }
    }
     
      

  5.   

    zgke 已给出正确答案了。楼主。
      

  6.   

      System.Diagnostics.Process p = new System.Diagnostics.Process();//创建进程
                    p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
                    p.Start();//开始进程
                   int hWnd = FindWindow(null, "SoftBoard");
                          if (hWnd == 0)
                          {                      }
                        else
                        {
                          GetWindowThreadProcessId(hWnd, out id);
                        }
                    RECT _Rect = new RECT();
                    GetWindowRect(hWnd, ref _Rect);                SetWindowPos(hWnd, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);不是这样啊?
      

  7.   

    哎~~ 先常识 下  p.MainWindowHandle   不行在根据名字找把.
      

  8.   


    p.MainWindowHandle不行 还是不能让窗体处于最前端
      

  9.   

    7楼的大哥 窗体句柄是怎么获得啊  我按照你的代码 出错了哦   int hWnd = FindWindow(null, "SoftBoard");  这个返回的是个int  GetWindowRect(传到这里的是个intPrt, ref _Rect);     
      

  10.   

     int hWnd = FindWindow(null, "SoftBoard");
    这里要写任务管理器里 应用程序的名字,而不是进程名
      

  11.   

    在你进程启动后。加一句
    p.WaitForInputIdle();
    我实际用zege的代码测试过。是可以的。我估计你代码执行太快。获取窗体句柄的时候窗体还没注册号。所以结果不对。
    新建控制台程序后。贴如下代码。可以看到效果:
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;namespace CSharpConsole06
    {    class Program
        {
            static void Main(string[] args)
            {
                Process p = Process.Start("Notepad.exe");
                p.WaitForInputIdle();
                RECT _Rect = new RECT();
                bool res = GetWindowRect(p.MainWindowHandle, ref _Rect);
                res = SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);            Console.ReadKey();
            }        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);        [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            private static void TestChar()
            {
                string digit = "零壹贰叁肆伍陆柒捌玖";
                string Str = "1224";
                foreach (char c in Str)
                {
                    Console.Write(digit[c - '0']);
                }
            }
        }
    }
      

  12.   

     int hWnd = FindWindow(null, "SoftBoard 4.1 屏幕软键盘");
                    if (hWnd == 0)
                    {                }
                    else
                    {
                        GetWindowThreadProcessId(hWnd, out id);
                    }                
                    RECT _Rect = new RECT();
                    GetWindowRect(这里要传intPtr类型, ref _Rect);                SetWindowPos(这里要传intPtr类型, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);写好了  那怎么传到里面去呢  这里是个int类型 传到方法里要是个IntPtr的类型
      

  13.   

       System.Diagnostics.Process p = new System.Diagnostics.Process();//创建进程
                    p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
                    p.Start();//开始进程
                    p.WaitForInputIdle();
                    RECT _Rect = new RECT();
                    GetWindowRect(p.MainWindowHandle, ref _Rect);                SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);按照你说的做了 但是还是不行 因为我浏览器窗体也是处于最顶端的 我想是不是 2个窗体都处于最顶端导致冲突了
      

  14.   

           Process p = Process.Start("SoftBoard.exe");
                    p.WaitForInputIdle();
                    RECT _Rect = new RECT();
                   GetWindowRect(p.MainWindowHandle, ref _Rect);
                  SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);还是不行 我郁闷啊
      

  15.   

    哎~ 还没弄好呢.. 感觉 SoftBoard.exe 里的主窗体不存在 或则 主窗体不是你要的窗体..
      

  16.   

    LZ  前几天也问这个问题了SoftBoard.exe  是你自己写做的吗    
      

  17.   

    如果是的话  将SetWindowPos在Form_Load()中
    加上
      

  18.   

    int id;
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
      public static extern int FindWindow(
      string lpClassName,
      string lpWindowName
      );
      [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
      public static extern int GetWindowThreadProcessId(
      int hwnd,
      out int lpdwProcessId
      );hWnd = FindWindow(null, "SoftBoard");
      if (hWnd == 0)
    {//没启动}
    else
    {
      GetWindowThreadProcessId(hWnd, out id);
    }