Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to embed Google Chrome in panel1 using Process.Start funciton in Csharp Winform project.

I come across some weired behavour using SetWindowPos.

The code works perfectly in Windows 8.1 but it works with error in Windows 10.

In windows 10 and laptop, the embedded chrome is covered with some black backround in right and bottom sides.

I tried with some different parameters with hWndInsertAfter and uFlags in SetWindowPos.

However, they do not work.

Does anyone know what causes this black coverage over the embedded process and possible fix for this issue ?

Please have a look at the screenshot uploaded for better understanding of the problem.

SetWindowPos Error Image[^]

Depending on the monitor size, it works sometimes and it does not work sometimes. I need to see the vertical scroll bar from chrome. Black coverage like above is hiding the vertical scroll bar, I guess.

How to see the vertical scroll bar again regardless of the monitor screen size ?

What I have tried:

var rect = new User32.Rect();

foreach (Process proc in procs)
  {
      User32.GetWindowRect(proc.MainWindowHandle, ref rect);
      width = rect.right - rect.left;
      height = rect.bottom - rect.top;

      if (width != 0 && height != 0)
      {
          SetParent(process.MainWindowHandle, panel1.Handle);
          process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
          SetForegroundWindow(process.MainWindowHandle);
          SetWindowPos(process.MainWindowHandle, 0, 0, 0, panel1.Width, panel1.Height, 0);
          break;
      }
  }
Posted
Updated 13-Feb-21 6:15am
v4
Comments
Richard MacCutchan 13-Feb-21 5:37am    
It looks like the Width and Height values are not correct. Check with your debugger to see what values they are.
[no name] 13-Feb-21 9:06am    
This worked fine for me in W8.1. and also in W10 (.Net FW 4.6.1)
namespace WFACP210213a
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetWindowRect(IntPtr hwnd, out Rectangle lpRect);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var procs = Process.GetProcessesByName("chrome");

            foreach (var proc in procs)
            {

                Rectangle rect = new Rectangle();

                GetWindowRect(proc.MainWindowHandle, out rect);
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;

                if (width != 0 && height != 0)
                {
                    SetParent(proc.MainWindowHandle, panel2.Handle);
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    SetForegroundWindow(proc.MainWindowHandle);
                    SetWindowPos(proc.MainWindowHandle, 0, 0, 0, panel2.Width, panel2.Height, 0);
                    break;
                }
            }
        }
    }
}
auto9817 13-Feb-21 12:02pm    
Depending on the monitor size, it works sometimes and it does not work sometimes. I need to see the vertical scroll bar from chrome. Black coverage like above is hiding the vertical scroll bar, I guess.
[no name] 13-Feb-21 12:38pm    
What .net FW version?
What monitor size work, and what other do not work?
auto9817 13-Feb-21 12:40pm    
Framework 4.52 and in general, it does not work in 15 inches laptop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900