如何设置对焦并启动在C#.NET3.5按钮单击事件已经运行的应用程序? [英] how to set focus and launch the already running application on button click event in c#.net3.5?

查看:168
本文介绍了如何设置对焦并启动在C#.NET3.5按钮单击事件已经运行的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用互斥试图在code,但未能即时消息后,按一下按钮,打开我的EXE 即时通讯成功不是按一下按钮使任务栏上的应用程序的多个条目,但我的应用程序启动只有当我结束我的表格.. 我想我推出按钮点击应用,如果应用程序已经启动,然后我需要把重点放在previous运行的应用程序.. 我怎么能能解决我的需要,推出,以及的focusIn和再重新打开该应用程序.. 即时发送ü我的code,即时通讯使用按钮单击事件和PLZ修改我的错误...

编码在Program.cs的

 静态无效的主要()
{

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(假);

    Application.Run(新Form1中());
    System.Diagnostics.Process.Start(文件名.exe);
}
 

进行编码在Form1.cs中

 私人无效的button1_Click(对象发件人,EventArgs的)
{
    尝试
    {
       布尔createdNew;
       互斥M =新的互斥(真,电子记录,出createdNew);
       System.Diagnostics.ProcessStartInfo F =新System.Diagnostics.ProcessStartInfo(C:\\ \\的窗户SYSTEM32 \\ RUNDLL32.EXE,C:\\ \\的窗户SYSTEM32 \\ shimgvw.dll中,ImageView_Fullscreen+文件名.EXE.TrimEnd(空));

        如果(createdNew)启动();

        其他
        {
             的MessageBox.show(电子记录已经运行!,多重背景);
         }
     }
     赶上(例外前)
     {
          System.Diagnostics.Debug.WriteLine(ex.ToString());
     }
}
 

解决方案

这是能够找到并切换到一个已经运行的进程相匹配你尝试启动。

  [的DllImport(user32.dll中)
公共静态的extern BOOL ShowWindowAsync(HandleRef的hWnd,诠释的nCmdShow);
公共const int的SW_RESTORE = 9;

公共无效SwitchToCurrent(){
  的IntPtr的HWND = IntPtr.Zero;
  工艺过程= Process.GetCurrentProcess();
  流程[]进程= Process.GetProcessesByName(process.ProcessName);
  的foreach(在工艺过程_process){
    //获取,是不是这种情况下,第一个实例,有
    //同一进程名,并从相同的文件名开始
    //和位置。还要检查过程中有一个有效
    //在这个会话窗口句柄来过滤掉其他用户
    //流程。
    如果(_process.Id = process.Id和放大器;!&安培;
      _process.MainModule.FileName == process.MainModule.FileName和放大器;&安培;
      _process.MainWindowHandle!= IntPtr.Zero){
      的hWnd = _process.MainWindowHandle;

      ShowWindowAsync(NativeMethods.HRef(HWND),SW_RESTORE);
      打破;
    }
  }
 }
 

i have been trying the code using mutex but im unable to open my exe after button click im successful in not making the multiple entries of the application on the taskbar at button click but my application is launched only when i close my form.. i want to launch my application on button click and if the application is already launched then i need to focus on the previous running application.. how could i able to resolve my need to launch as well as focusin and reopening that application again.. im sending u my code that im using on button click event and plz modify my errors...

coding at program.cs

static void Main()  
{

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new Form1());
    System.Diagnostics.Process.Start("filename.exe");
}

:

coding done at form1.cs

private void button1_Click(object sender, EventArgs e)
{
    try
    {
       bool createdNew;
       Mutex m = new Mutex(true, "e-Recording", out createdNew);
       System.Diagnostics.ProcessStartInfo f = new System.Diagnostics.ProcessStartInfo("C:\\windows\\system32\\rundll32.exe", "C:\\windows\\system32\\shimgvw.dll,ImageView_Fullscreen " + "filename.exe".TrimEnd(null));

        if (createdNew) Launch();

        else
        {
             MessageBox.Show("e-Recording is already running!", "Multiple Instances");
         }
     }
     catch (Exception ex)
     {
          System.Diagnostics.Debug.WriteLine(ex.ToString());
     }
}

解决方案

This is be able to find and switch to an already running process that matches what you are trying to start.

[DllImport( "user32.dll" )]
public static extern bool ShowWindowAsync( HandleRef hWnd, int nCmdShow );
public const int SW_RESTORE = 9;

public void SwitchToCurrent() {
  IntPtr hWnd = IntPtr.Zero;
  Process process = Process.GetCurrentProcess();
  Process[] processes = Process.GetProcessesByName( process.ProcessName );
  foreach ( Process _process in processes ) {
    // Get the first instance that is not this instance, has the
    // same process name and was started from the same file name
    // and location. Also check that the process has a valid
    // window handle in this session to filter out other user's
    // processes.
    if ( _process.Id != process.Id &&
      _process.MainModule.FileName == process.MainModule.FileName &&
      _process.MainWindowHandle != IntPtr.Zero ) {
      hWnd = _process.MainWindowHandle;

      ShowWindowAsync( NativeMethods.HRef( hWnd ), SW_RESTORE );
      break;
    }
  }
 }

这篇关于如何设置对焦并启动在C#.NET3.5按钮单击事件已经运行的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆