0

For IE browser, I know we can use SHDocVw.ShellWindows to control the IE navigate the web page in the same tab.

The sample code in IE:

SHDocVw.ShellWindows windows = new SHDocVw.ShellWindows();

//enumerate windows
foreach (SHDocVw.InternetExplorer window in windows)
{
  if (window.LocationURL.Contains(matchUrl))
  {
       window.Navigate(originalUrl, null, null, null, null);
       break;
  }
}

I want to know it's possible open the web page in the same tab in Edge browser? Thanks.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
jerry
  • 317
  • 2
  • 20
  • Check this out https://stackoverflow.com/questions/8454510/open-url-in-same-window-and-in-same-tab – Chinmay T Nov 02 '21 at 03:38
  • The following may be helpful: https://learn.microsoft.com/en-us/microsoft-edge/webview2/ – Tu deschizi eu inchid Nov 02 '21 at 04:33
  • @Chinmay, I need to do it with C#. – jerry Nov 02 '21 at 04:48
  • @user9938, the webview2 is inappropriate in my case. I need to open lots of edge's windows, but the url with the same domain should be in the same tab. and some website can' t be rendered in webview2. – jerry Nov 02 '21 at 04:55
  • 1
    WebView2 is still under development. If you find issues (or would like features added) you can report them (or make your suggestions) here: https://github.com/MicrosoftEdge/WebView2Feedback/issues . Of course if you are attempting to do something malicious, you're out of luck. – Tu deschizi eu inchid Nov 02 '21 at 14:34

1 Answers1

0

You can try to use the UIAutomation API in C# to achieve similar requirements.

Edit:After jerry reminded, considering that when the application is minimized, you may need to use the ShowWindow API to display the Edge in the foreground.

A simple demo, please refer it:

class Program
{
    [DllImport("User32.dll")]
    static extern int SetForegroundWindow(IntPtr point);

    [DllImport("User32")]
    private static extern int ShowWindow(int hwnd, int nCmdShow);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl);

    private struct Windowplacement
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }

    static void Main(string[] args)
    {
        Process[] procsEdge = System.Diagnostics.Process.GetProcessesByName("msedge");
        foreach (Process proc in procsEdge)
        {
            Windowplacement placement = new Windowplacement();
            GetWindowPlacement(proc.MainWindowHandle, ref placement);

            // Check if window is minimized
            if (placement.showCmd == 2)
            {
                //the window is hidden so we restore it
                ShowWindow(proc.MainWindowHandle.ToInt32(), 9);
            }
            
            //Switch Edge tab to the first one
            SetForegroundWindow(proc.MainWindowHandle);
            SendKeys.SendWait("^1");
            
            if (proc.MainWindowHandle == IntPtr.Zero)
                continue;

            string matchUrl = "https://www.bing.com";
            string originalUrl = "http://www.google.com/";
            int numTabs = procsEdge.Length;
            int index = 1;
            //loop all tabs in Edge
            while (index <= numTabs)
            {
                //get the url of tab
                AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
                var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
                if (SearchBar != null)
                {
                    string str = (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
                    //Determine whether the url matches and redirect
                    if (str.Contains(matchUrl))
                    {
                        SetForegroundWindow(proc.MainWindowHandle);
                        SendKeys.SendWait("^l");
                        SendKeys.SendWait(originalUrl);
                        SendKeys.SendWait("{Enter}");
                        break;
                    }
                }
                index++;
                SendKeys.SendWait("^{TAB}"); // change focus to next tab
            }
        }
    }
}

Note: Please modify the page url parameters appropriately according to your own situation.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Xudong Peng
  • 1,463
  • 1
  • 4
  • 9
  • Thanks for your answer. It really help me. I just found if the edge browser is minimized and it can't be bring to the foreground. so I use the ShowWindow API to restore it and it works. – jerry Nov 03 '21 at 02:53
  • Hi @jerry , I am glad that your needs has been achieved. It seems that I have not considered this request thoroughly, and your reply reminded me. According to the ShowWindow API you mentioned, I used it to complete the solution. Hope this can help other members of the community who have encountered similar problems. – Xudong Peng Nov 03 '21 at 05:29
  • Thanks again. I think the best solution is that we can call the API opened by edge. But it seems edge not support as IE does. – jerry Nov 03 '21 at 07:30
  • On the other hand, if user force to change focus to other windows, then the result is not as expected. – jerry Nov 03 '21 at 08:08