Code Bye

c# winform寻找窗口中的控件

本人在c# winform里边嵌套了一个chrome内核的浏览器,有时候点击链接时会自动在一个新建的窗口中打开.那么问题来了,本人不知道这个窗口的Name,本人怎么去找到里面的浏览器空间呢?望大婶解答!
解决方案

30

怎么不用IE内核啊?那个很方便的
本人的思路是:根据句柄来获得浏览器控件
1.通过进程获取窗口的句柄
IntPtr hwnd= Process.GetCurrentProcess().MainWindowHandle;
2.使用FindWindowEx api,然后根据父窗口和类名来获取浏览器控件句柄
[DllImport(“User32.dll”, EntryPoint = “FindWindowEx”)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
IntPtr chrome = FindWindowEx(hwnd, null, “Chrome_WidgetWin_1”,null);//可以本人查看一下具体参数
查看句柄,可以使用vs下的spy++,可以获取句柄的基本信息,包括类名和窗口标题等
3.假如知道浏览器的标题,还可以进行判断
[DllImport(“user32.dll”)]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport(“User32.dll”, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
int length = GetWindowTextLength(hWnd);
StringBuilder windowName = new StringBuilder(length + 1);
GetWindowText(hWnd, windowName, windowName.Capacity);
windowName  //窗口标题

70

这个类在WinfForm的示例里面有,看OnBeforePopup里面注释掉的代码。
public class LifeSpanHandler : ILifeSpanHandler
    {
        bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            //Default behaviour
            newBrowser = null;
            return false; //Return true to cancel the popup creation
            //EXPERIMENTAL OPTION #1: Demonstrates using a new instance of ChromiumWebBrowser to host the popup.
            //var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            //ChromiumWebBrowser chromiumBrowser = null;
            //var windowX = windowInfo.X;
            //var windowY = windowInfo.Y;
            //var windowWidth = (windowInfo.Width == int.MinValue) ? 600 : windowInfo.Width;
            //var windowHeight = (windowInfo.Height == int.MinValue) ? 800 : windowInfo.Height;
            //chromiumWebBrowser.Invoke(new Action(() =>
            //{
            //    var owner = chromiumWebBrowser.FindForm();
            //    chromiumBrowser = new ChromiumWebBrowser(targetUrl)
            //    {
            //        LifeSpanHandler = this
            //    };
            //    //NOTE: This is important and must be called before the handle is created
            //    chromiumBrowser.SetAsPopup();
            //    //Ask nicely for the control to create it"s underlying handle as we"ll need to
            //    //pass it to IWindowInfo.SetAsChild
            //    chromiumBrowser.CreateControl();
            //    var popup = new Form
            //    {
            //        Left = windowX,
            //        Top = windowY,
            //        Width = windowWidth,
            //        Height = windowHeight,
            //        Text = targetFrameName
            //    };
            //    owner.AddOwnedForm(popup);
            //    popup.Controls.Add(new Label { Text = "CefSharp Custom Popup" });
            //    popup.Controls.Add(chromiumBrowser);
            //    popup.Show();
            //    var rect = chromiumBrowser.ClientRectangle;
            //    //This is key, need to tell the Browser which handle will it"s parent
            //    //You maybe able to pass in 0 values for left, top, right and bottom though it"s safest to provide them
            //    windowInfo.SetAsChild(chromiumBrowser.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
            //}));
            //newBrowser = chromiumBrowser;
            //return false;
            //EXPERIMENTAL OPTION #2: Use IWindowInfo.SetAsChild to specify the parent handle
            //NOTE: Window resize not yet handled - it should be possible to get the
            // IBrowserHost from the newly created IBrowser instance that represents the popup
            // Then subscribe to window resize notifications and call NotifyMoveOrResizeStarted
            //var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            //var windowX = windowInfo.X;
            //var windowY = windowInfo.Y;
            //var windowWidth = (windowInfo.Width == int.MinValue) ? 600 : windowInfo.Width;
            //var windowHeight = (windowInfo.Height == int.MinValue) ? 800 : windowInfo.Height;
            //chromiumWebBrowser.Invoke(new Action(() =>
            //{
            //    var owner = chromiumWebBrowser.FindForm();
            //    var popup = new Form
            //    {
            //        Left = windowX,
            //        Top = windowY,
            //        Width = windowWidth,
            //        Height = windowHeight,
            //        Text = targetFrameName
            //    };
            //    popup.CreateControl();
            //    owner.AddOwnedForm(popup);
            //    var control = new Control();
            //    control.Dock = DockStyle.Fill;
            //    control.CreateControl();
            //    popup.Controls.Add(control);
            //    popup.Show();
            //    var rect = control.ClientRectangle;
            //    windowInfo.SetAsChild(control.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
            //}));
        }
        void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
        {
            
        }
        bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
        {
            return false;
        }
        public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
        {
            
        }
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# winform寻找窗口中的控件