您当前的位置: 首页 > 

君子居易

暂无认证

  • 0浏览

    0关注

    210博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

WPF 激活现有窗口并关闭新窗口。

君子居易 发布时间:2021-06-15 09:08:05 ,浏览量:0

//关闭当前程序,显示已打开程序

private void App_Startup( object sender, StartupEventArgs e )
 {
    var current_process = Process.GetCurrentProcess( );
    var other_process = Process.GetProcessesByName( current_process.ProcessName ).FirstOrDefault( p => p.Id != current_process.Id );
    
    if( other_process != null && other_process.MainWindowHandle != IntPtr.Zero )
    {
       if( IsIconic( other_process.MainWindowHandle ) )
       {
          ShowWindow( other_process.MainWindowHandle, SW_RESTORE );
       }
       SetForegroundWindow( other_process.MainWindowHandle );
       Shutdown( );
    }
 }
   //是否窗口最小化 
 [DllImport( "user32" )]
 static extern bool IsIconic( IntPtr hWnd );
    //显示窗口
 [DllImport( "user32" )]
 static extern bool ShowWindow( IntPtr hWnd, int cmdShow );
 const int SW_RESTORE = 9;
    //将窗口显示在最前面
 [DllImport( "user32" )]
 static extern bool SetForegroundWindow( IntPtr hWnd );

 

关注
打赏
1660814979
查看更多评论
立即登录/注册

微信扫码登录

0.0352s