java - 如何知道外部程序的窗口是否打开

时间:2011-04-27 08:59:37

标签: java window vlc

我有一些启动外部程序(VLC)的代码执行某些操作并关闭外部程序(VLC)。

问题是我无法知道VLC是否已打开或者是否仍在加载,是否有任何方法可以让Java知道VLC的窗口是否已打开?

编辑:

这是代码

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class myClass extends JDialog {

    public Dimension dim;
    public Process pr;

    public myClass(final JFrame parent, String title, String message) {
        super(parent, title, true);
        try {
            Runtime rt = Runtime.getRuntime();
            pr = rt.exec("C:\\Programmi\\VideoLAN\\VLC\\vlc.exe dshow:// --dshow-vdev=\"TridVid Capture\" --dshow-video-input=1 -Ihttp");
            Thread.sleep(500); //this is a workaround
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        if (parent != null) {
            Toolkit toolkit =  Toolkit.getDefaultToolkit ();
            dim = toolkit.getScreenSize();
            Dimension parentSize = parent.getSize(); 
            Point p = parent.getLocation(); 
            setBounds(p.x + parentSize.width / 4, p.y + parentSize.height / 4, dim.width, dim.height);
        }
        JPanel messagePane = new JPanel();
        Image image = new ImageIcon("targa_prova.jpg").getImage();
        ImagePanel panel = new ImagePanel(image.getScaledInstance(dim.width, dim.height, Image.SCALE_SMOOTH));

        messagePane.add(new JLabel(message));
        getContentPane().add(panel);

        Timer timer = new Timer(5000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pr.destroy();
                parent.setVisible(false);
                parent.dispose();
            }
        });

        timer.setRepeats(false);
        timer.start();
        try {
            Image backgroundImage = javax.imageio.ImageIO.read(new File("targa_prova.jpg")); 
            messagePane.paintComponents(backgroundImage.getGraphics());
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        setVisible(true);

    }

    public static void main(String[] a) {
        new myClass(new JFrame(), "Configuring Cam", "Configurazione della telecamera in corso...\nAttendere.");
    }

}

class ImagePanel extends JPanel {

      private Image img;

      public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
      }

      public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
      }

      public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
      }

    }

上面的代码启动vlc,打开一个覆盖vlc窗口的对话框,而不是关闭vlc和对话框,现在我得到它的延迟,但它很糟糕,无法在一些慢速计算机上工作。

0 个答案:

没有答案