1
除此之外question我明白我無法實現我的目標,我嘗試從圖像創建自己的光標。如何結合圖像作爲SWT光標使用
在我的示例中,我只是試圖在上面繪製一個帶有的綠色框。
我希望得到的結果看起來像
這裏是我的代碼:
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CursorTest {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10, 10, 200, 200);
Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(10, 50, 150, 100);
canvas.setCursor(CursorTest.getCursor(display, new Color(display, new RGB(0, 255 ,0))));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static Cursor getCursor(Display display, Color c) {
ImageDescriptor element = ImageDescriptor.createFromFile(
ImageProvider.class, "arrow.png");
Image a = element.createImage();
int width = a.getBounds().width;
int height = a.getBounds().height;
Image image = makeTransparent(new Image(display, width, height));
GC gc = new GC(image);
gc.setBackground(c);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.fillRectangle(4, 10, 12, 12);
gc.drawRectangle(4, 10, 12, 12);
gc.drawImage(a, 0, 0);
Cursor result = new Cursor(display, image.getImageData(),
0, 0);
gc.dispose();
image.dispose();
return result;
}
private static Image makeTransparent(Image sourceImage) {
ImageData data = sourceImage.getImageData();
data.transparentPixel = data.palette.getPixel(new RGB(255, 255, 255));
Image ret = new Image(sourceImage.getDevice(), data);
sourceImage.dispose();
return ret;
}
}
正如你可以採用相當奇怪的看到這個功能,我無法明白爲什麼。任何人都可以幫忙嗎?
謝謝!是的透明度是一場噩夢,我從來沒有這樣做過。想你。 – Link19