110,537
社区成员
发帖
与我相关
我的任务
分享
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
public void RaiseOtherProcess()
{
Process proc = Process.GetCurrentProcess();
Process.GetProcesses();
foreach (Process otherProc in
Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
//ignore "this" process
if (proc.Id != otherProc.Id)
{
// Found a "same named process".
// Assume it is the one we want brought to the foreground.
// Use the Win32 API to bring it to the foreground.
IntPtr hWnd = otherProc.MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, 9);
}
SetForegroundWindow(hWnd);
break;
}
}
}
[DllImport("user32.dll ")]
public static extern IntPtr FindWindow(string 类名, string 程序标题);
[DllImport( "user32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private void button3_Click(object sender, EventArgs e)
{
IntPtr hwnd = FindWindow(null, "新建 文本文档 (6).txt - 记事本"); //放回句柄
int WM_PASTE = // 具体的消息号你可以网上查下
if (hwnd.ToInt32 () != 0)
{
SetForegroundWindow(hwnd);
SendMessage(hwnd,WM_PASTE,0,0); // 具体的消息原型你可以网上查下
}
else { MessageBox.Show("记事本没有运行 "); }
}