Windows系统中用以激活上下文帮助的有两种方式,
1. F1,
2. 标题栏上的问号按钮.
SWT支持第一种方式。只要对一个控件加上SWT.Help事件的Listener就可以了。
我却没有找到怎样在标题栏上加上一个问号的方法。只好借助JNI了,先查需要哪个Win32 API的函数。那是
SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_CONTEXTHELP)函数,其中hWnd是对话框的句柄,WS_EX_CONTEXTHELP激活了上下文问号按钮. 然后了我们可以定义一个Java Native的方法以及相应的C函数, 并把C函数做成一个DLL的库, 从Java中调用SetWindowLong以激活问号按钮。
幸运的是SWT已经为我们作了大部分工作了,org.eclipse.swt.internal.win32.OS类中已经定义了SetWindowLong函数, 也就是说我们不需要自己写JNI的接口. 当然OS类位于一个internal的包了, internal包里的类要谨慎使用. 另外即使在SWT的CVS库里也没有定义WS_EX_CONTEXTHELP这个常数, 这说明SWT目前还不支持标题栏上的问号按钮, 可能是因为其他的窗口系统下不支持的缘故.在这篇里,我只考虑Windows的情况.
在我们写代码之前,需要知道WS_EX_CONTEXTHELP具体的值, 查MSDN或者用VC++里的打开定义功能, 查到WS_EX_CONTEXTHELP = 0x400.
public class HelpButton {
public static final String HELP_TEXT = "HELP_TEXT";
public static final String HELP_IMAGE = "HELP_IMAGE";
private Shell parent;
private Shell helpShell;
private Label helpImageLabel;
private Label helpTextLabel;
private static final int WS_EX_CONTEXTHELP = 0x400;
public HelpButton(Shell shell) {
int style = OS.GetWindowLong(shell.handle, OS.GWL_EXSTYLE);
OS.SetWindowLong(shell.handle, OS.GWL_EXSTYLE, style
| WS_EX_CONTEXTHELP);
this.parent = shell;
helpShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.marginWidth = 2;
gridLayout.marginHeight = 2;
helpShell.setLayout(gridLayout);
Display display = shell.getDisplay();
helpShell.setBackground(display
.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
helpImageLabel = new Label(helpShell, SWT.NONE);
helpImageLabel.setForeground(display
.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
helpImageLabel.setBackground(display
.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
helpImageLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_CENTER));
helpTextLabel = new Label(helpShell, SWT.NONE);
helpTextLabel.setForeground(display
.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
helpTextLabel.setBackground(display
.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
helpTextLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_CENTER));
Listener listener = new Listener() {
public void handleEvent(Event event) {
if (helpShell.isVisible())
helpShell.setVisible(false);
}
};
helpTextLabel.addListener(SWT.MouseDown, listener);
helpImageLabel.addListener(SWT.MouseDown, listener);
parent.addListener(SWT.MouseDown, listener);
}
public void activeHelpListener(final Control control) {
control.addListener(SWT.Help, new Listener() {
public void handleEvent(Event event) {
Control control = (Control) event.widget;
String text = control.getData(HELP_TEXT).toString();
Object img = control.getData(HELP_IMAGE);
if (img instanceof Image) {
helpImageLabel.setImage((Image) img);
}
helpTextLabel.setText(text);
Rectangle controlRect = control.getBounds();
Display display = parent.getDisplay();
Point pt = display.map(parent, null, control.getLocation());
helpShell.pack();
helpShell.setLocation(pt.x + controlRect.width / 2, pt.y
+ controlRect.height / 2);
helpShell.setVisible(true);
}
});
}
}
测试程序
public class HelpButtonTest {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.DIALOG_TRIM);
shell.setLayout(new RowLayout());
HelpButton helpButton = new HelpButton(shell);
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
helpButton.activeHelpListener(button);
button.setData(HelpButton.HELP_TEXT, "Click me to get 1 million EURO.");
Image image = new Image(display, HelpButtonTest.class
.getResourceAsStream("information.gif"));
button.setData(HelpButton.HELP_IMAGE, image);
button = new Button(shell, SWT.PUSH);
button.setText("Eclipse SWT");
helpButton.activeHelpListener(button);
button.setData(HelpButton.HELP_TEXT, "Click me to get 100 million EURO.");
image = new Image(display, HelpButtonTest.class
.getResourceAsStream("warning.gif"));
button.setData(HelpButton.HELP_IMAGE, image);
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
if (image != null)
image.dispose();
display.dispose();
}
}
转载请保留
http://www.blogjava.net/xilaile/archive/2007/02/23/100397.html