[英]Bring already opened winform application to front without API?
Hi, 嗨,
Say that we got a WinForm application(app1) running in the background, now another application(app2)(the topmost active application) trigger a startProcess with the app1. 假设我们在后台运行了一个WinForm应用程序(app1),现在另一个应用程序(app2)(最顶层的活动应用程序)触发了一个带有app1的startProcess。
Now I need app1 to use the existing instance and bring it to topmost application(not only within the app1 application). 现在我需要app1来使用现有实例并将其带到最顶层的应用程序(不仅仅是在app1应用程序中)。
I have found this : http://sanity-free.org/143/csharp_dotnet_single_instance_application.html 我发现了这个: http : //sanity-free.org/143/csharp_dotnet_single_instance_application.html
Is it true that its not possible to do this without API? 如果没有API,它是不是可以做到这一点? I have looked att bringToFront, Activate and Focus but all these does seem to only effect within a application and not between applications?
我看过attToFront,Activate和Focus,但所有这些似乎只在一个应用程序中产生,而不是在应用程序之间?
I don't know what you mean "without API" or why that matters. 我不知道你的意思是“没有API”或者为什么重要。
However the simplest way is via WindowsFormsApplicationBase
. 但最简单的方法是通过
WindowsFormsApplicationBase
。 It gives you all you need, with just a few lines of code. 只需几行代码,它就能满足您的所有需求。
You need to add a reference to the Microsoft.VisualBasic
assembly - but it can be used through C#. 您需要添加对
Microsoft.VisualBasic
程序集的引用 - 但它可以通过C#使用。
Make this class: 创建这个课程:
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
IsSingleInstance = true;
}
public static void Run(Form form)
{
var app = new SingleInstanceApplication
{
MainForm = form
};
app.StartupNextInstance += (s, e) => e.BringToForeground = true;
app.Run(Environment.GetCommandLineArgs());
}
}
And in your Program.cs, change the run line to use it: 在Program.cs中,更改运行行以使用它:
//Application.Run(new Form1());
SingleInstanceApplication.Run(new Form1());
You really need some sort of communications between 2 apps. 你真的需要2个应用程序之间的某种通信。 In article link to you posted communications is through WinApi messages.
在文章中链接到您发布的通信是通过WinApi消息。 Also you can do that through sockets or through files and FileWatchers.
您也可以通过套接字或文件和FileWatchers来实现。
UPD1: Code to simulate minimize with timer simulation message from another app and maximize on that message: UPD1:使用来自其他应用程序的计时器模拟消息模拟最小化的代码,并最大化该消息:
public partial class Form1 : Form
{
private Timer _timer = null;
public Form1()
{
InitializeComponent();
this.Load += OnFormLoad;
}
private void OnFormLoad(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Hide and top most on timer";
btn.Width = 200;
btn.Click += OnButtonClick;
this.Controls.Add(btn);
}
private void OnButtonClick(object sender, EventArgs e)
{
//minimize app to task bar
WindowState = FormWindowState.Minimized;
//timer to simulate message from another app
_timer = new Timer();
//time after wich form will be maximize
_timer.Interval = 2000;
_timer.Tick += new EventHandler(OnTimerTick);
_timer.Start();
}
private void OnTimerTick(object sender, EventArgs e)
{
_timer.Stop();
//message from another app came - we should
WindowState = FormWindowState.Normal;
TopMost = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.