<code class="lang-c">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);
}
}</code>
引用 acmilan:
源码能否一发?
<code class="lang-c">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<i>))
{
end = i;
break;
}
}
if (end == 0) end = Text.Length;
for (int i = SelectionStart - 1; i >= 0; i--) //确定起始点
{
if (!char.IsLetter(Text<i>))
{
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<i>))
{
end = i;
break;
}
}
for (int i = SelectionStart-1; i >= start; i--)//往起点寻找大写
{
if (char.IsUpper(Text<i>))
{
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();
}
}</i></i></i></i></code>