怎樣用java做托盤程序?

lihaihui 2002-01-10 04:04:44
怎樣用java做托盤程序?
...全文
115 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
whubhjt 2002-01-11
  • 打赏
  • 举报
回复
vj6.0中有类似的程序,可以借鉴一下至于java的,我不太清楚
vj6.0中的那个在msdn中的例子里
不过切记,和java 有区别
import com.ms.wfc.app.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.core.*;
import com.ms.wfc.win32.*;
import com.ms.wfc.util.*;
import com.ms.win32.*;


public class TrayIcon extends Component {
public static final int MB_LEFT = MouseButton.LEFT;
public static final int MB_RIGHT = MouseButton.RIGHT;
public static final int MB_MIDDLE = MouseButton.MIDDLE;

private static final Object EVENT_MOUSEDOWN = new Object();
private static final Object EVENT_MOUSEENTER = new Object();
private static final Object EVENT_MOUSELEAVE = new Object();
private static final Object EVENT_MOUSEMOVE = new Object();
private static final Object EVENT_MOUSEUP = new Object();
private static final Object EVENT_CLICK = new Object();
private static final Object EVENT_DOUBLECLICK = new Object();

/*
* This API creates a unique message ID, and associates it with a string
* that you specify. It allows multiple applications to get the same
* ID for the same message, and to ensure that the ID doesn't collide with
* other Win32 message IDs already defined. That's what we use it for here.
*/
private static final int WM_TRAYMOUSEMESSAGE = RegisterWindowMessage( "TrayIconNotification" ); // win.WM_USER + 1024;
/** @dll.import( "user32", auto ) */
private native static int RegisterWindowMessage( String messageName );

private Icon icon = null;
private String text = null;
private int id = 0;
private boolean visible = false;
private boolean added = false;
private boolean enabled = true;
private TrayIconWindow window = null;

private static int nextId = 0;
private static NOTIFYICONDATA data = new NOTIFYICONDATA();
static {
data.uCallbackMessage = WM_TRAYMOUSEMESSAGE;
}

public TrayIcon() {
id = ++nextId;
window = new TrayIconWindow(this);
}

public void addOnMouseDown(MouseEventHandler value) {
addEventHandler(EVENT_MOUSEDOWN, value);
}

public void addOnMouseMove(MouseEventHandler value) {
addEventHandler(EVENT_MOUSEMOVE, value);
}

public void addOnMouseUp(MouseEventHandler value) {
addEventHandler(EVENT_MOUSEUP, value);
}

protected void fireMouseEvent(Object key, MouseEvent e) {
com.ms.lang.Delegate handler = getEventHandler(key);
if (handler != null) ((MouseEventHandler)handler).invoke(this, e);
}

public void finalize() {
updateIcon(false);
window.destroyHandle();
}

public boolean getEnabled() {
return enabled;
}

public String getText() {
return text;
}

public Icon getIcon() {
return icon;
}

public int getHandle() {
return window.getHandle();
}

public boolean getVisible() {
return visible;
}

protected void onMouseDown(MouseEvent e) {
if (enabled)
fireMouseEvent(EVENT_MOUSEDOWN, e);
}

protected void onMouseMove(MouseEvent e) {
if (enabled)
fireMouseEvent(EVENT_MOUSEMOVE, e);
}

protected void onMouseUp(MouseEvent e) {
if (enabled)
fireMouseEvent(EVENT_MOUSEUP, e);
}

public void removeOnMouseDown(MouseEventHandler value) {
removeEventHandler(EVENT_MOUSEDOWN, value);
}

public void removeOnMouseMove(MouseEventHandler value) {
removeEventHandler(EVENT_MOUSEMOVE, value);
}

public void removeOnMouseUp(MouseEventHandler value) {
removeEventHandler(EVENT_MOUSEUP, value);
}

public void setText(String text) {
if (!Utils.equals(this.text, text)) {
this.text = text;
if (added) {
updateIcon(true);
}
}
}

public void setIcon(Icon icon) {
this.icon = icon;
if (added) {
updateIcon(true);
}
}

public void setVisible(boolean value) {
if (visible != value) {
updateIcon(value);
visible = value;
}
}

public void setEnabled( boolean value ) {
enabled = value;
}

private synchronized void updateIcon(boolean visible) {
// Bail if in design mode...
IComponentSite site = getComponentSite();
if (site != null) {
if (site.getDesignMode()) {
return;
}
}

data.uFlags = win.NIF_MESSAGE;
if (visible) {
if (window.getHandle() == 0) {
window.createHandle(new CreateParams());
}

data.hWnd = window.getHandle();
}
data.uID = id;
data.hIcon = 0;
data.szTip = null;
if (icon != null) {
data.uFlags |= win.NIF_ICON;
try {
data.hIcon = icon.getHandle();
}
catch (Exception e) {
}
}
if (text != null) {
data.uFlags |= win.NIF_TIP;
data.szTip = text;
}

if (visible) {
if (!added) {
Shell32.Shell_NotifyIcon(win.NIM_ADD, data);
added = true;
}
else {
Shell32.Shell_NotifyIcon(win.NIM_MODIFY, data);
}
}
else if (added) {
Shell32.Shell_NotifyIcon(win.NIM_DELETE, data);
added = false;
}
}

private void wmMouseDown(Message m, int button, int clicks) {
onMouseDown(new MouseEvent(button, clicks, 0, 0, 0));
}

private void wmMouseMove(Message m) {
onMouseMove(new MouseEvent(Control.getMouseButtons(), 0, 0, 0, 0));
}

private void wmMouseUp(Message m, int button) {
onMouseUp(new MouseEvent(button, 0, 0, 0, 0));
}

protected void wndProc(Message msg) {
if (msg.msg == WM_TRAYMOUSEMESSAGE) {
switch (msg.lParam) {
case win.WM_LBUTTONDBLCLK:
wmMouseDown(msg, MB_LEFT, 2);
break;
case win.WM_LBUTTONDOWN:
wmMouseDown(msg, MB_LEFT, 1);
break;
case win.WM_LBUTTONUP:
wmMouseUp(msg, MB_LEFT);
break;
case win.WM_MBUTTONDBLCLK:
wmMouseDown(msg, MB_MIDDLE, 2);
break;
case win.WM_MBUTTONDOWN:
wmMouseDown(msg, MB_MIDDLE, 1);
break;
case win.WM_MBUTTONUP:
wmMouseUp(msg, MB_MIDDLE);
break;
case win.WM_MOUSEMOVE:
wmMouseMove(msg);
break;
case win.WM_RBUTTONDBLCLK:
wmMouseDown(msg, MB_RIGHT, 2);
break;
case win.WM_RBUTTONDOWN:
wmMouseDown(msg, MB_RIGHT, 1);
break;
case win.WM_RBUTTONUP:
wmMouseUp(msg, MB_RIGHT);
break;
}
}
else if (msg.msg == win.WM_COMMAND) {
if (0 == msg.lParam)
if (Command.dispatchID(msg.wParam & 0xFFFF)) return;
}
}

private static class TrayIconWindow extends Window {
com.ms.vm.WeakReference ref;
TrayIcon rootRef;

TrayIconWindow(TrayIcon control) {
ref = new com.ms.vm.WeakReference(control);
}

protected void wndProc(Message m) {
TrayIcon control = (TrayIcon)ref.getReferent();
control.wndProc(m);
super.wndProc(m);
}
}

public static class ClassInfo extends Component.ClassInfo {

public static final PropertyInfo Text =
new PropertyInfo(TrayIcon.class, "text", String.class,
CategoryAttribute.Appearance,
LocalizableAttribute.YES,
new DescriptionAttribute("The text that will be displayed when the mouse hovers over the icon."));

public static final PropertyInfo Icon =
new PropertyInfo(TrayIcon.class, "icon", Icon.class,
CategoryAttribute.Appearance,
LocalizableAttribute.YES,
new DescriptionAttribute("The icon to display in the system tray."));

public static final PropertyInfo Visible =
new PropertyInfo(TrayIcon.class, "visible", Boolean.TYPE,
CategoryAttribute.Behavior,
new DefaultValueAttribute(Boolean.FALSE),
new DescriptionAttribute("Determines whether the control is visible or hidden"));

public static final PropertyInfo Enabled =
new PropertyInfo( TrayIcon.class, "enabled", Boolean.TYPE,
CategoryAttribute.Behavior,
new DefaultValueAttribute( null ),
new DescriptionAttribute( "Determines whether the control is enabled or disabled" )
);

public static final EventInfo OnMouseDown =
new EventInfo(TrayIcon.class, "mouseDown", MouseEventHandler.class,
CategoryAttribute.Mouse,
new DescriptionAttribute("Occurs when a mouse button is pressed"));

public static final EventInfo OnMouseMove =
new EventInfo(TrayIcon.class, "mouseMove", MouseEventHandler.class,
CategoryAttribute.Mouse,
new DescriptionAttribute("Occurs when the mouse is moved"));

public static final EventInfo OnMouseUp =
new EventInfo(TrayIcon.class, "mouseUp", MouseEventHandler.class,
CategoryAttribute.Mouse,
new DescriptionAttribute("Occurs when a mouse button is released"));

public void getAttributes( IAttributes attr ) {
attr.add( ShowInToolboxAttribute.YES );
}

public String getDefaultPropertyName() {
return "text";
}

public String getDefaultEventName() {
return "mouseDown";
}

public void getEvents(IEvents events) {
super.getEvents(events);
events.add(OnMouseDown);
events.add(OnMouseMove);
events.add(OnMouseUp);
}

public void getProperties(IProperties props) {
super.getProperties(props);
props.add(Enabled);
props.add(Icon);
props.add(Text);
props.add(Visible);
}
}
}

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧