JFrame 过渡效果 - 当调用 setState(Frame.ICONIFIED) 时,它只是进入任务栏而没有动画

2022-01-24 00:00:00 java swing jframe

我现在有一个问题 - 当我使用自定义按钮调用 frame.setState(Frame.ICONIFIED) 时(我没有使用默认的 JFrame 最小化按钮 - JFrame 设置为 setUndecorated(true) ),JFrame 只是进入任务栏,没有任何动画.在正常情况下,它应该逐渐去任务栏最小化自己.但是,如果我在任务栏上按图标化的 JFrame,它会用动画恢复到正常大小.这种情况是在 Windows XP 上,未在其他系统上测试过,但我想它的行为方式相同.

I have a problem now - when I call frame.setState(Frame.ICONIFIED) with my custom button (I'm not using default JFrame minimize button - JFrame set to setUndecorated(true) ), the JFrame just goes to Taskbar without any animation. In normal situation it should gradually go to Taskbar minimizing itself. But if I press iconfied JFrame on Taskbar, it restores with animation to normal size. The situation is on Windows XP, not tested on other systems, but I suppose it would behave in the same way.

推荐答案

如果您说的是真的,并且您未装饰的窗口实际上在单击任务栏中的图标时会显示动画.然后,您可以使用 JNA 在代码中触发相同的操作.

If what you are saying is true, and your undecorated windows do in fact animate when clicking the icons in the task bar. Then you can trigger that same action in your code using JNA.

要使此解决方案起作用,您需要包含 jna.jar 和 jna-platform.jar 在你的类路径中.

For this solution to work, you'll need to include jna.jar and jna-platform.jar in your classpath.

这不是 JNA 教程,周围有很多.这只是使用 JNA 调用 user32.dll 函数的代码 关闭窗口 和 打开图标;这些是当您单击应用程序的托盘图标时 Windows 调用的函数 -- (说实话,我不确定这些是实际的函数,但它们的反应相同).

This isn't a JNA tutorial, there are plenty of those around. This is just code that uses JNA to call the user32.dll functions CloseWindow and OpenIcon; These are the functions that Windows calls when you click the application's tray icon -- (truth be told I'm not certain that these are the actual functions, but they react the same).

import java.awt.Component;
import java.awt.Window;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;

public class IconifyUtilityClass
{
    public static void minimize(Window window)
    {
        HWND hWnd = getHWND(window);
        User32dll.INSTANCE.CloseWindow(hWnd); //call CloseWindow with this windows handle
    }

    public static void restore(Window window)
    {
        HWND hWnd = getHWND(window);
        User32dll.INSTANCE.OpenIcon(hWnd); //call OpenIcon with this windows handle
    }

    private interface User32dll extends Library
    {
        User32dll INSTANCE = (User32dll) Native.loadLibrary("user32.dll", User32dll.class);
        boolean OpenIcon(HWND hWnd);
        boolean CloseWindow(HWND hWnd);
    }

    private static HWND getHWND(Component comp)
    {
        return new HWND(Native.getComponentPointer(comp));
    }
}

要调用代码,只需像这样传入你的框架或对话框

To call the code just pass in your frame or dialog like so

JFrame frame = new JFrame();
...
IconifyUtilityClass.minimize(frame); //minimize your frame
...
IconifyUtilityClass.restore(frame); //restore your frame

<小时>

值得注意的是,在 Windows 7 中,未修饰的框架根本没有动画效果(即使使用 user32.dll AnimateWindow 函数).因此,如果您的目标是让您的帧在任何地方进行动画处理,那么您是对的,您必须自己动手.JavaFx 有一些非常好的动画内容可以帮助解决这个问题.


It's worth noting that in Windows 7 undecorated frames don't animate at all (even using the user32.dll AnimateWindow function). So if your goal is to get your frames to animate anywhere, you are right, you'll have to do it yourself. JavaFx has some really nice animation stuff that would help with this.

相关文章