3 messages in net.java.dev.jna.users[jna-users] Windows Tray Icon
FromSent OnAttachments
Barnaby AstlesDec 2, 2008 7:11 am 
Steve Sobol (JDN)Dec 2, 2008 7:30 am 
Barnaby AstlesDec 2, 2008 7:35 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] Windows Tray IconActions...
From:Barnaby Astles (bjas@gmail.com)
Date:Dec 2, 2008 7:11:14 am
List:net.java.dev.jna.users

I am having some problems trying to interfaces to the system tray. The icon remains but I can not add an image to the icon, nor can I access the messages coming from the icon, or show a balloon message.

Here is the code, I know it is a bit complicated I'm trying to integrate from multiple sources like JDIC

public class Test implements W32API { private static final int WINDOWS_TASKBAR_ICON_WIDTH = 16; private static final int WINDOWS_TASKBAR_ICON_HEIGHT = 16;

static final int TRAY_NOTIFYICON =(User32.WM_APP+100);

public static void main(String[] args) throws InterruptedException {

WND_MESSAGE msgWnd = new WND_MESSAGE();

while(true) { showBalloonMessage(msgWnd.messageWindow, null, 5200000, "TEST".toCharArray(), "BLALALALALA".toCharArray(), 2); Thread.sleep(5000); } }

static class WND_MESSAGE extends Thread { ATOM wndClass; HWND messageWindow = new HWND(); HINSTANCE hInstance; String g_szClassName = "JDIC_Tray"; String g_szTitle = "Tray Window";

User32.WNDPROC wndProc = new User32.WNDPROC(){ int msgRestartTaskbar = 0; public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam) { System.out.println("Callback uMsg=" + uMsg + " wParam=" + wParam.longValue() + " lParam=" + lParam.longValue()); if (msgRestartTaskbar == 0 && uMsg == User32.WM_CREATE) { //msgRestartTaskbar = User32.INSTANCE.RegisterWindowMessageW("TaskbarCreated"); System.out.println("1"); }

if (msgRestartTaskbar != 0 && uMsg == msgRestartTaskbar) { System.out.println("2"); return new LRESULT(1); }

if (uMsg >= TRAY_NOTIFYICON && lParam.intValue() != User32.WM_MOUSEMOVE) { User32.POINT pt = new User32.POINT(); User32.INSTANCE.GetCursorPos(pt); System.out.println("3"); return new LRESULT(1); } LRESULT res = User32.INSTANCE.DefWindowProcW(hWnd, uMsg, wParam, lParam); System.out.println(res.longValue()); return res; } };

public WND_MESSAGE() {

User32.WNDCLASSW wc = new User32.WNDCLASSW();

wc.style = User32.CS_HREDRAW | User32.CS_VREDRAW; wc.lpfnWndProc = wndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = null; wc.hCursor = User32.INSTANCE.LoadCursorW(null, User32.IDC_ARROW); wc.hbrBackground = GDI32.INSTANCE.GetStockObject(GDI32.WHITE_BRUSH); wc.lpszClassName = g_szClassName;

wndClass = User32.INSTANCE.RegisterClassW(wc); messageWindow = User32.INSTANCE.CreateWindowExW(0, g_szClassName, g_szTitle, User32.WS_OVERLAPPEDWINDOW | User32.WS_CLIPCHILDREN, 0, 0, 0, 0, null, null, hInstance, null); start(); }

public void run() { HICON hIcon = User32.INSTANCE.LoadIcon(null, User32.IDI_ASTERISK);

Shell32.NOTIFYICONDATAW data = new Shell32.NOTIFYICONDATAW();

data.hWnd = messageWindow; data.uCallbackMessage = TRAY_NOTIFYICON; //data.hIcon = hIcon; data.uID = 55; data.uFlags = Shell32.NIF_INFO;

data.dwInfoFlags = Shell32.NIF_MESSAGE|Shell32.NIF_ICON|Shell32.NIF_TIP; data.szTip = "Test App".toCharArray(); data.cbSize = data.size();

Shell32.INSTANCE.Shell_NotifyIcon(Shell32.NIM_ADD, data);

for(User32.MSG msg = new User32.MSG();User32.INSTANCE.GetMessage(msg, messageWindow, 0, 0) != 0;){ User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); }

System.out.println("Dead"); }

public HWND getMessageWindow() { return messageWindow; } }

static void showBalloonMessage(HWND wnd, HICON icon, int id, char[] title, char[] message, int type) { Shell32.NOTIFYICONDATAW tnd = new Shell32.NOTIFYICONDATAW();

tnd.cbSize = tnd.size(); tnd.hWnd = wnd; tnd.uID = id; tnd.uFlags = Shell32.NIF_INFO; switch (type) { case 0 : tnd.dwInfoFlags = Shell32.NIIF_INFO; break; case 1 : tnd.dwInfoFlags = Shell32.NIIF_ERROR; break; case 2 : tnd.dwInfoFlags = Shell32.NIIF_WARNING; break; case 3 : tnd.dwInfoFlags = Shell32.NIIF_NONE; }

tnd.hIcon = icon; tnd.szInfoTitle = title; tnd.szInfo = message;

System.out.println(Shell32.INSTANCE.Shell_NotifyIcon(Shell32.NIM_MODIFY, tnd)); } }