问题
I'm writing a screensaver in Java. It's primarily for Windows, though I'd prefer it to be as portable as practical.
According to http://support.microsoft.com/kb/182383, when a screensaver is invoked with command line args /p HWND
, the screensaver should "Preview Screen Saver as child of window ." Presumably this is how screensavers should their little preview in the Screen Saver Settings dialog.
So how, in Java, do you create a JFrame or JComponent that's a child of a window that belongs to somebody else? I've looked at Embed HWND (Window Handle) in a JPanel which is sort of the reverse question: The developer controls the parent window and wants to embed a child window. In my case, I control a child component that I want into embed into a parent that is not "mine". Can I use JNA to do that?
I'd rather not have to maintain C code in addition to Java code. (So JNI is not preferred.) I understand that JDIC was supposed to meet this kind of need, but I've read that JDIC is essentially dead.
I'm running Windows 7, FWIW.
回答1:
Have a look at Jdic, it has an SDK for screensavers, so at least you may study their code..
回答2:
You need to call a win32 api. That api is in C. You'll need to the interop layer to do this. I think every option will be gross.
For Java, I think that is JNI. See this on how to do it.
You will also need to pass that value to SetParent.
The screen saver runs at the same security rights as the screensaver dialog so this isn't a security issue.
来源:https://stackoverflow.com/questions/9855743/create-child-window-of-another-processs-hwnd-e-g-screensaver-preview