我已经能够用程序找到“记事本”窗口。但是我怎么做能够把 记事本 窗口获得焦点呢? try:[DllImport("user32.dll")]private static extern int SetForegroundWindow (int hwnd);SetForegroundWindow(ParenthWnd); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [DllImport("user32.dll")]public static extern IntPtr SetFocus(IntPtr hwnd);...SetFocus(hwnd); [DllImport("user32.dll")]private static extern int SetForegroundWindow (int hwnd);SetForegroundWindow(ParenthWnd);==========================这是我想要的答案。给分。谢谢 BearRui(我有点笨,但我很特别,所以我特别笨!) 帖子结的还是挺快的,好同志!!!虽然你的问题用SetForegroundWindow能打开窗口,但是如果你的窗口是最小化的话就不行.要想完善一下还要加两个API才行,我写了一个如下的代码你可以参考一下:const int SWP_SHOWWINDOW = 0x40;const int SWP_NOOWNERZORDER = 0x200;const int SWP_NOSENDCHANGING = 0x0400;const int SWP_NOSIZE = 0x0001;const int SWP_NOMOVE = 0x0002;const int HWND_TOP =0;[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern IntPtr FindWindow(string strclassName, string strWindowName);[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr pWndInsertAfter,int x,int y,int cx,int cy,uint nFlags );[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool SetWindowText(IntPtr hWnd, string lpString );[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool OpenIcon(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool IsIconic(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern int SetForegroundWindow(IntPtr hWnd);private void OpenForm_Click(object sender, System.EventArgs e){ string FormTitle = this.textBox1.Text; IntPtr hWnd = FindWindow(null, FormTitle); if (hWnd != IntPtr.Zero) { //SetForegroundWindow(o); //return; bool isIcon = IsIconic(hWnd); bool isSuccess = false; if ( !isIcon ) { isSuccess = SetWindowPos( hWnd, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); } else { isSuccess = OpenIcon(hWnd); } //if (isSuccess) //SetWindowText( o, "新的标题"); } } winform 之间的数据传送 急啊!如何获得<div id="aa" title="123"></div>的title值 SplitContainer 中 Panel2 部分的闪动 在线等待,怎么把字节数组存入到sqlserver2000数据库中去!!! 各位怎么快速对于一个编程原档有一个整体概略的了解﹖ Tencent QQ 通常使用哪几个端口? C#中如何把字符串写进word文档 如何捕捉关闭傲游浏览器时引发的事件 求救,分不够再加,一个一直没搞清楚的问题! 求助winform richtextbox保存和读取ASCII值不相同的问题 关于RichTextBox 变量问题???
public static extern IntPtr SetFocus(IntPtr hwnd);
...
SetFocus(hwnd);
private static extern int SetForegroundWindow (int hwnd);SetForegroundWindow(ParenthWnd);
==========================
这是我想要的答案。给分。
谢谢 BearRui(我有点笨,但我很特别,所以我特别笨!)
虽然你的问题用SetForegroundWindow能打开窗口,但是如果你的窗口是最小化的话就不行.要想完善一下还要加两个API才行,我写了一个如下的代码你可以参考一下:
const int SWP_SHOWWINDOW = 0x40;
const int SWP_NOOWNERZORDER = 0x200;
const int SWP_NOSENDCHANGING = 0x0400;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int HWND_TOP =0;[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr FindWindow(string strclassName, string strWindowName);[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr pWndInsertAfter,int x,int y,int cx,int cy,uint nFlags );[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetWindowText(IntPtr hWnd, string lpString );[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool OpenIcon(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);private void OpenForm_Click(object sender, System.EventArgs e)
{
string FormTitle = this.textBox1.Text;
IntPtr hWnd = FindWindow(null, FormTitle);
if (hWnd != IntPtr.Zero)
{
//SetForegroundWindow(o);
//return;
bool isIcon = IsIconic(hWnd);
bool isSuccess = false;
if ( !isIcon )
{
isSuccess = SetWindowPos( hWnd, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
else
{
isSuccess = OpenIcon(hWnd);
}
//if (isSuccess)
//SetWindowText( o, "新的标题");
}
}