我有一个JFrame,我使用以下方法最小化到托盘:
这表示:
Frame.this.Minimized = false;
Frame.this.setVisible(true);
systemTray.remove(systemTrayIcon);
Frame.this.setExtendedState(JFrame.NORMAL);
这是为了隐藏:
if (SystemTray.isSupported()) {
systemTray.add(systemTrayIcon);
Frame.this.setVisible(false);
Frame.this.Minimized = true;
}
Frame.this.setExtendedState(JFrame.ICONIFIED);
但是,我不想将框架设置为不可见..当我将其设置为不可见时,它会删除我喜欢的任务栏图标。有没有办法删除框架的任务栏图标而不将可见性设置为false?
原因是因为当我最小化我的应用程序时,我可以发送命令并执行它们,但是第二个我将其可见性设置为false,它会停止执行来自外部应用程序的任何命令。我需要做的就是在最小化时从任务栏中删除图标,并在正常时显示图标。
有什么想法吗?
答案 0 :(得分:5)
叹息很好看,因为很长一段时间没有回复..我决定用C ++ / JNI解决它,反思如下:
在Java方面:
package apptotray;
import java.awt.*;
import java.io.File;
import java.nio.file.Paths;
import javax.swing.*;
public class AppToTray {
public static void main(String[] args) {
JFrame frame = new JFrame("Some Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JPanel(), BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
System.load(new File("JNI.dll").getAbsolutePath());
try {
System.out.println("Icon is showing..");
Thread.sleep(3000);
} catch (Exception Ex) {
Ex.printStackTrace();
}
removeFromTaskBar(getWindowHandle(frame));
try {
System.out.println("Icon is not showing..");
Thread.sleep(3000);
} catch (Exception Ex) {
Ex.printStackTrace();
}
addToTaskBar(getWindowHandle(frame));
System.out.println("Icon is showing again..");
}
public static native void addToTaskBar(long WindowHandle);
public static native void removeFromTaskBar(long WindowHandle);
public static long getWindowHandle(java.awt.Frame frame) {
return (Long)invokeMethod(invokeMethod(frame, "getPeer"), "getHWnd");
}
protected static Object invokeMethod(Object o, String methodName) {
Class c = o.getClass();
for (java.lang.reflect.Method m : c.getMethods()) {
if (m.getName().equals(methodName)) {
try {
return m.invoke(o);
} catch (IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException Ex) {
Ex.printStackTrace();
break;
}
}
}
return null;
}
}
在JNI / C ++方面(Main.cpp):
#include <windows.h>
#include <shobjidl.h>
#include "jni.h"
#if defined _WIN32 || defined _WIN64
extern "C"
{
const GUID CLSID_TaskbarList = {0x56FDF344, 0xFD6D, 0x11D0, {0x95, 0x8A, 0x00, 0x60, 0x97, 0xC9, 0xA0, 0x90}};
const GUID IID_ITaskbarList = {0x56FDF342, 0xFD6D, 0x11D0, {0x95, 0x8A, 0x00, 0x60, 0x97, 0xC9, 0xA0, 0x90}};
const GUID IID_ITaskbarList2 = {0x602D4995, 0xB13A, 0x429b, {0xA6, 0x6E, 0x19, 0x35, 0xE4, 0x4F, 0x43, 0x17}};
const GUID IID_ITaskList3 = {0xEA1AFB91, 0x9E28, 0x4B86, {0x90, 0xE9, 0x9E, 0x9F, 0x8A, 0x5E, 0xEF, 0xAF}};
}
#endif
extern "C" JNIEXPORT void JNICALL Java_apptotray_AppToTray_addToTaskBar(JNIEnv *, jclass, jlong WindowHandle)
{
#if defined _WIN32 || defined _WIN64
ITaskbarList* TaskListPtr;
CoInitialize(nullptr);
long Result = !CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_SERVER, IID_ITaskbarList, reinterpret_cast<void**>(&TaskListPtr));
if (Result) TaskListPtr->AddTab(reinterpret_cast<HWND>(WindowHandle));
TaskListPtr->Release();
CoUninitialize();
#endif
}
extern "C" JNIEXPORT void JNICALL Java_apptotray_AppToTray_removeFromTaskBar(JNIEnv *, jclass, jlong WindowHandle)
{
#if defined _WIN32 || defined _WIN64
ITaskbarList* TaskListPtr;
CoInitialize(nullptr);
long Result = !CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_SERVER, IID_ITaskbarList, reinterpret_cast<void**>(&TaskListPtr));
if (Result) TaskListPtr->DeleteTab(reinterpret_cast<HWND>(WindowHandle));
TaskListPtr->Release();
CoUninitialize();
#endif
}
extern "C" bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
使用以下命令编译DLL:
x86_64-w64-mingw32-g ++。exe -O2 -Wall -DBUILD_DLL -std = c ++ 11 -c C:\ Users \ Brandon \ Desktop \ JNI \ main.cpp -o obj \ Release \ main.o
x86_64-w64-mingw32-g ++。exe -shared -Wl, - output-def = bin \ Release \ libJNI.def -Wl, - out-implib = bin \ Release \ libJNI.a -Wl, - -dll obj \ Release \ main.o -o bin \ Release \ JNI.dll -s -static -static-libgcc -static-libstdc ++ -lole32 -lshell32 -luser32
或者只是使用代码块来完成它。
如果其他人有更好的想法,请随意添加或评论或任何东西..我仍然不能相信我必须使用C ++ / JNI,并反思这样做..荒谬..这是2013年, Java需要使用该程序。
答案 1 :(得分:3)
我知道这已经很晚了,但我花了一个小时试图自己解决这个问题。您所要做的就是将JFrame更改为JWindow并完成。我有一个相当复杂的JFrame,我必须删除的唯一内容是setUndecorated()
和setDefaultCloseOperation()
。其他一切都很好。