C# Winfrom项目,如何实现用API.SetWindowPos方法设置固定位置后,能够正常的将打开的第三方的应用程序嵌入到Panel控件中。

sxl_88 2015-04-20 11:06:18
在C# Winfrom项目中,有一个form窗体,一个panel控件.
通过Process.start方法打开第三方的应用程序,将第三方程序嵌入Panel控件中。
用API.SetWindowPos方法改变第三方的应用程序位置以及大小。

目前发现一个问题,
打开第三方的应用程序设置大小及位置,通过获取panel控件的位置可以显示嵌入到Panel控件中,代码如下:
API.SetWindowPos(ptravayaWnd, IntPtr.Zero, this.panel1.Location.X, this.panel1.Location.Y, 956, 536, 0);
API.SetParent(ptravayaWnd, this.panel1.Handle);

打开第三方的应用程序设置大小及位置,通过获设置固定的位置无法显示嵌入到Panel控件中,代码如下:
API.SetWindowPos(ptravayaWnd, IntPtr.Zero,650,0, 956, 536, 0);//设置位置为:650,0 无法显示打开第三方的应用程序嵌入到Panel控件中
API.SetParent(ptravayaWnd, this.panel1.Handle);


请问如何实现用API.SetWindowPos方法设置固定位置后,能够正常的将打开的第三方的应用程序嵌入到Panel控件中。
...全文
267 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxl_88 2015-04-28
  • 打赏
  • 举报
回复
没人知道吗?
sxl_88 2015-04-21
  • 打赏
  • 举报
回复
引用 4 楼 dongxinxi 的回复:
窗口当然是会受到Z-order影响的 你可以试试先SetParent,然后SetWindowPos
那如果窗口是受到Z-order影响的,那下面设置固定位置的代码应该怎么修改。谢谢! API.SetWindowPos(ptravayaWnd, IntPtr.Zero,650,0, 956, 536, 0);//设置位置为:650,0
  • 打赏
  • 举报
回复
窗口当然是会受到Z-order影响的 你可以试试先SetParent,然后SetWindowPos
  • 打赏
  • 举报
回复
先看下取的窗口句柄对了没 SetParent msdn最下面有个备注,就是设置前要移除目标窗口风格,WS_POPUP,并设置WS_CHILD 不知道是否跟这个有关,之前写过了个玩

       public const long WS_CHILD = 0x40000000L, WS_POPUP = 0x80000000L;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern long GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll")]
        public static extern long SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);

        public static void RemoveWindowStyle(IntPtr hwnd, long styles)
        {
            styles = GetWindowLong(hwnd, GWL_STYLE) & ~styles;
            SetWindowLong(hwnd, GWL_STYLE, styles);
        }

        public static void AddWindowStyle(IntPtr hwnd, long styles)
        {
            styles = GetWindowLong(hwnd, GWL_STYLE) | styles;
            SetWindowLong(hwnd, GWL_STYLE, styles);
        }

        public static void SafeSetParent(IntPtr hChildWnd, IntPtr hNewParent)
        {
            //移除父窗口
            if (hNewParent == IntPtr.Zero)
            {
                SetParent(hChildWnd, hNewParent);
                RemoveWindowStyle(hChildWnd, WS_CHILD);                
                AddWindowStyle(hChildWnd, WS_POPUP);
            }
            //设置父窗口
            else
            {
                RemoveWindowStyle(hChildWnd, WS_POPUP);
                AddWindowStyle(hChildWnd, WS_CHILD);
                SetParent(hChildWnd, hNewParent);                
            }
        }
sxl_88 2015-04-20
  • 打赏
  • 举报
回复
是不是要设置下层级,设置固定位置会不会被panel控件遮挡住? 请各位帮忙看看,谢谢。
sxl_88 2015-04-20
  • 打赏
  • 举报
回复
还有没有其它的解决办法?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧