我用Process要开Notepad.exe,想让它只开一个,如果打开了,就让它的主窗口在桌面最前面激活??
解决方案 »
- QQ邮箱上传超大文件速度怎么这么快
- 如何计算当前时间是本月份的第几个周几?
- 大家推荐几个专业的C#网站,谢谢
- 求C#.net、ASP.net容易入门的电子书。高分!
- 用ADO.Net怎么得到数据库中表的一个列的定义长度(字符型数)?
- C# 中 DataGridViewRow.HeaderCell.Value 值居中
- TreeView 节点选择怪问题
- 请教下,我们写的程序应该如何提高CPU利用率呢?
- 诚交.Net朋友.QQ:123558469
- Mysql win7 32位找不到Mysql Server安装文件?
- 急求 基于C#打印视频教程!!
- DateTimePicker弹出问题(Winform)
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
...........................
定义一个全局变量
Process mNotePad ;
// 判断notepad是否已经运行了
Process[] tmpPro = System.Diagnostics.Process.GetProcessesByName("Notepad");
if (tmpPro.Length == 0)// 判断是否已经运行了,
{
mNotePad = Process.Start(“Notepad.exe”);
}
else
{
SetForegroundWindow(mNotePad.MainWindowHandle);
}
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using StockMain;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;namespace StockMain
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new fmMain());
//Application.Run(new FormTest());
}
else
{
HandleRunningInstance(instance);
}
} public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
if (process.Id!=current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/","\\")==current.MainModule.FileName)
{
return process;
}
}
}
return null;
} public static void HandleRunningInstance(Process instance)
{
////确保窗口没有被最小化或最大化
//ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
} [DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
}
{
mNotePad = Process.Start("notepad"); }
else
{
mNotePad = processes[0];
SetForegroundWindow(mNotePad.MainWindowHandle);
}
窗口最小化就不能激活了,要加楼上的这一段
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
就能全部解决了
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);这个函数没有用上呢??
帮我举个打开NotePad的示例吧
补充:在外围加上
DllImport("user32.dll")]
private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private const int SW_SHOWNORMAL = 1;并且ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); 中WS_SHOWNORMAL改为SW_SHOWNORMAL