简体   繁体   English

如何检查窗口是否已打开?

[英]How can I check if a window is already open?

I have a program that creates a .png image and then opens, using the default program, the image to show the user the image. 我有一个程序,该程序创建一个.png图像,然后使用默认程序打开该图像以向用户显示该图像。

My problem is that I don't want it to open again the image if the user left it open, what I mean is that if the user doesn't close the image I don't want a second window opening showing the same thing. 我的问题是,如果用户保持打开状态,我不希望它再次打开图像,我的意思是,如果用户不关闭图像,则我不希望再次打开显示相同内容的窗口。

System.Diagnostics.Process myProcess = new Process();
myProcess.StartInfo.FileName = @"Labyrinth.png";
myProcess.Start(); 

Would anyone know a way to check if the image is open with the windows photo viewer or whatever default program you use. 有谁知道用Windows Photo Viewer或您使用的任何默认程序检查图像是否打开的方法。

You can use Process.MainWindowHandle and use Win32 Native API to check detail of that window. 您可以使用Process.MainWindowHandle并使用Win32本机API来检查该窗口的详细信息。

Following link may help you on this. 以下链接可能会帮助您。

Unexpected behaviour of Process.MainWindowHandle Process.MainWindowHandle的异常行为

Getting MainWindowHandle of a process in C# 在C#中获取进程的MainWindowHandle

As per my knowledge .NET framework not supported this thing directly but you can use P/Invoke to Win32 API and get detail. 据我所知,.NET框架不直接支持此功能,但是您可以使用P / Invoke到Win32 API并获取详细信息。

Hope this help you. 希望这对您有所帮助。

The main problem here is that you basically know nothing about the program you're launching, only that it's associated with .png files, so checking for windows is possibly very situation-specific and not generally reliable. 这里的主要问题是,您基本上对正在启动的程序一无所知,只知道它与.png文件相关联,因此检查Windows可能非常因情况而异,并且通常不可靠。

What I would do is to check for the whole process instead. 我要做的是改为检查整个过程。 At the time of opening the second png, check if the first process is still alive and don't open if it's still is. 在打开第二个png时,请检查第一个进程是否仍在运行,如果仍然存在则不要打开。 The HasExited property serves that purpose. HasExited属性用于此目的。 Try something like this: 尝试这样的事情:

private Process previousProcess = null;

public CreateImage()
{
    //Here put png creation as you already have
    //Now attempt to open it if the previous instance has closed

    if(this.previousProcess == null || this.previousProcess.HasExited)
    {
        //Either there was no previous image opened or it was already closed, go ahead and open it
        Process myProcess = new Process();
        myProcess.StartInfo.FileName = @"Labyrinth.png";
        myProcess.Start();

        //Cache the newly launched process to check for it afterwards
        this.previousProcess = myProcess;
    }
}

Note that this relies on your program to remain open within two consecutive generations, so it will not detect if you leave the image open, but close and reopen your program instead. 请注意,这依赖于您的程序在连续两个世代中保持打开状态,因此它不会检测到图像是否保持打开状态,而是关闭并重新打开程序。 Also, the user might reuse the same program opened for the image for doing something else, effectively closing the image but not the process, in which case further generated images will not be shown. 同样,用户可能会重复使用为图像打开的相同程序来执行其他操作,从而有效地关闭图像,但不会关闭该过程,在这种情况下,将不会显示进一步生成的图像。

Of course a workaround to all this is to provide some way to display the image in your own program, and optionally giving the user an option to launch a program with it if the like, thus avoiding the problem entirely. 当然,所有这一切的解决方法是提供某种方式在您自己的程序中显示图像,并且如果有类似的选择,还可以为用户提供启动程序的选项,从而完全避免了问题。

Edit 编辑

It seems that it's not necesary to Dispose() the previous object if the underlying OS process already has been terminated, so removed that line. 如果基础OS进程已经终止,则似乎不需要Dispose()上一个对象,因此删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM