张静茹作者
11年5个月前IP:山东
776440
5楼
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
        private void Form2_Load(object sender, EventArgs e)
        {
            webBrowser.ScriptErrorsSuppressed = true;
        }
        IntPtr _form1;
        public void Navigate(string urlString, IntPtr __form1)
        {
            _form1 = __form1;
            webBrowser.Navigate(urlString);
    
        }
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            SetForegroundWindow(_form1);
        }
    
        private void Form2_Activated(object sender, EventArgs e)
        {
            SetForegroundWindow(_form1);
        }
    }
引用 acmilan:
源码能否一发?



public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //调用API
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow(); //获得本窗体的句柄
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
        //定义变量,句柄类型
        public IntPtr han;
        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //Text = string.Format("当前选中位置 {0} 当前选中字符长度 {1} 当前选中文本 {2}", textBoxSou.SelectionStart, textBoxSou.SelectionLength, textBoxSou.SelectedText);
    
    
            string poram  = WordStartEnd(textBoxSou.Text,textBoxSou.SelectionStart);
            if (string.IsNullOrEmpty(poram))
            {
                return;
            }
    
            IntPtr _form1 = GetForegroundWindow();
            F2.Navigate(string.Format("http://www.bing.com/dict/search?q={0}&go=%E6%8F%90%E4%BA%A4&qs=bs&form=CM", poram), _form1);
                
            Text = _form1.ToString("X");
    
            //this.Focus();
            //SetForegroundWindow(_form1);
            //MessageBox.Show(textBoxSou.Text.Substring(start, end - start));
        }
        /// <summary>
        /// 寻找单词起点与终点
        /// </summary>
        /// <param name="Text">
        /// <param name="SelectionStart">
        /// <param name="start">
        /// <param name="end">
        private string WordStartEnd(string Text, int SelectionStart)
        {
            int start = 0,end = 0;
            for (int i = SelectionStart; i < Text.Length; i++) //确定结束点
            {
                if (!char.IsLetter(Text))
                {
                    end = i;
                    break;
                }
            }
            if (end == 0) end = Text.Length;
    
            for (int i = SelectionStart - 1; i >= 0; i--) //确定起始点
            {
                if (!char.IsLetter(Text))
                {
                    start = i + 1;
                    break;
                }
            }
    
            string temp = Text.Substring(start, end - start);
            if (!Regex.IsMatch(temp,"[a-z]"))
            {
                return temp;
            }
    
            for (int i = SelectionStart; i < end; i++) //往终点寻找大写
            {
                if(char.IsUpper(Text))
                {
                    end = i;
                    break;
                }
            }
            for (int i = SelectionStart-1; i >= start; i--)//往起点寻找大写
            {
                if (char.IsUpper(Text))
                {
                    start = i;
                    break;
                }
            }
    
            return Text.Substring(start, end - start);
        }
    
        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("asd");
        }
    
        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog字体.Font = textBoxSou.Font;
            fontDialog字体.Color = textBoxSou.ForeColor;
    
            fontDialog字体.ShowColor = true;
            fontDialog字体.ShowDialog();
            textBoxSou.Font = fontDialog字体.Font;
            textBoxSou.ForeColor = fontDialog字体.Color;
        }
    
        private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog颜色.ShowDialog();
            textBoxSou.BackColor = colorDialog颜色.Color;
        }
    
        private void textBoxSou_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tbtem = (TextBox)sender;
            if (e.Control && e.KeyCode == Keys.A)
            {
                tbtem.SelectionStart = 0;
                tbtem.SelectionLength = tbtem.Text.Length;
                e.Handled = true;
            }
        }
    
        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //webBrowser.DocumentText = Http.Get("http://www.bing.com/dict/search?q=" + "browser" + "&go=%E6%8F%90%E4%BA%A4&qs=bs&form=CM", "");
            IntPtr ip = GetForegroundWindow();
            int i = ip.ToInt32();
            Text = i.ToString("X");
    
            SetForegroundWindow(new IntPtr(0x00391422));
    
        }
        Form2 F2 ;
        private void Form1_Load(object sender, EventArgs e)
        {
            F2 = new Form2();
            F2.Show();
        }
    }
评论
文号 / 776440

千古风流
名片专栏发私信
学术分 1
总主题 146 帖总回复 1873 楼拥有证书:进士 学者 机友 笔友
注册于 2010/12/30 03:32:26最后登录 2026/06/20 19:33:53
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:0

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的