예제 #1
0
        internal FindToolbar(ChromiumWebBrowser wb)
        {
            this.wb = wb;

            Font = new Font("Microsoft Sans Serif", 10);

            SetStyle(ControlStyles.FixedWidth
                     | ControlStyles.FixedHeight
                     | ControlStyles.SupportsTransparentBackColor
                     | ControlStyles.EnableNotifyMessage
                     | ControlStyles.DoubleBuffer
                     | ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.UseTextForAccessibility
                     | ControlStyles.Selectable
                     , false);

            SetStyle(ControlStyles.UserPaint
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.Opaque
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.StandardClick
                     | ControlStyles.StandardDoubleClick
                     | ControlStyles.UserMouse
                     | ControlStyles.CacheText
                     | ControlStyles.ContainerControl
                     , true);

            Visible = false;

            downButton.Parent      = this;
            upButton.Parent        = this;
            matchCaseButton.Parent = this;
            resultLabel.Parent     = this;
            textBox.Parent         = this;
            closeButton.Parent     = this;

            textBox.Font      = new Font(Font, FontStyle.Italic);
            textBox.Left      = 10;
            textBox.Top       = 8;
            textBox.Width     = 200;
            textBox.ForeColor = Color.DimGray;

            textBox.TextChanged += (s, e) => {
                if (textBox.Text.Length == 0)
                {
                    if (textBox.Font.Style != FontStyle.Italic)
                    {
                        textBox.Font = new Font(textBox.Font, FontStyle.Italic);
                    }
                    wb.BrowserHost.StopFinding(true);
                    UpdateMatchInfo(0);
                }
                else
                {
                    if (textBox.Font.Style == FontStyle.Italic)
                    {
                        textBox.Font = new Font(textBox.Font, FontStyle.Regular);
                    }
                    if (!autoSearchSuspended)
                    {
                        Find(true);
                    }
                }
            };

            textBox.KeyDown += (s, e) => {
                if (e.KeyCode == Keys.Return)
                {
                    Find(e.Shift ? false : true);
                    e.Handled = true;
                }
            };

            upButton.Font = new Font(Font.FontFamily, 9);
            upButton.SetBounds(textBox.Left + textBox.Width - 1, textBox.Top, textBox.Height, textBox.Height);
            SetButtonStyle(upButton);
            upButton.Image     = Images.ArrowUp.Create();
            upButton.TextAlign = ContentAlignment.MiddleCenter;

            upButton.Click += (s, e) => {
                Find(false);
            };

            upButton.GotFocus += (s, e) => { textBox.Focus(); };

            ConfigToolTip("Goto previous match", upButton);

            downButton.Font = new Font(Font.FontFamily, 9);
            downButton.SetBounds(upButton.Left + upButton.Width - 1, textBox.Top, textBox.Height, textBox.Height);
            SetButtonStyle(downButton);
            downButton.Image     = Images.ArrowDown.Create();
            downButton.TextAlign = ContentAlignment.MiddleCenter;

            downButton.Click += (s, e) => {
                Find(true);
            };

            downButton.GotFocus += (s, e) => { textBox.Focus(); };

            ConfigToolTip("Goto next match", downButton);

            matchCaseButton.Text      = "Match Case";
            matchCaseButton.ForeColor = Color.DimGray;
            matchCaseButton.Left      = downButton.Left + downButton.Width + 10;
            matchCaseButton.Top       = downButton.Top;
            matchCaseButton.Width     = 110;
            matchCaseButton.Height    = downButton.Height;
            matchCaseButton.FlatStyle = FlatStyle.Flat;
            matchCaseButton.FlatAppearance.BorderSize = 0;
            matchCaseButton.BackColor = Color.Transparent;
            matchCaseButton.TextAlign = ContentAlignment.MiddleCenter;

            matchCaseButton.Click += (s, e) => {
                ChangeMatchCase();
            };

            matchCaseButton.GotFocus += (s, e) => { textBox.Focus(); };

            resultLabel.ForeColor = Color.DimGray;
            resultLabel.Left      = matchCaseButton.Left + matchCaseButton.Width + 10;
            resultLabel.Top       = matchCaseButton.Top + 3;
            resultLabel.AutoSize  = true;

            Height    = textBox.Height + 12;
            BackColor = Color.WhiteSmoke;

            closeButton.Height    = upButton.Height;
            closeButton.Width     = closeButton.Height;
            closeButton.Left      = Width - closeButton.Width - 10;
            closeButton.Top       = upButton.Top;
            closeButton.BackColor = Color.Transparent;
            closeButton.Image     = Images.Cross.Create();
            closeButton.FlatStyle = FlatStyle.Flat;
            closeButton.FlatAppearance.BorderSize = 0;

            ConfigToolTip("Close the Find Bar", closeButton);

            closeButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;


            closeButton.Click += (s, e) => {
                Visible = false;
            };

            closeButton.GotFocus += (s, e) => { textBox.Focus(); };

            // SendMessage(textBox.Handle, 0x1501, 1, "Find in page");
            NativeWindow.SetWatermark(textBox.Handle, "Find in page");

            // Once a find toolbar is created for a browser, it is kept until the browser is destroyed.
            // So there is no need to unsubscribe from this event.
            wb.FindHandler.OnFindResult += (s, e) => {
                if (e.FinalUpdate && e.Identifier == lastFindId)
                {
                    var count = e.Count;
                    BeginInvoke(() => UpdateMatchInfo(count));
                }
            };
        }