How to display system icon for a file in SWT?

后端 未结 4 1422
谎友^
谎友^ 2021-01-02 04:40

I want to display a file tree similarly to java2s.com \'Create a lazy file tree\', but include the actual system icons - especially for folders. SWT does not seem to offer t

4条回答
  •  -上瘾入骨i
    2021-01-02 05:21

    I haven't looked at the code in detail, but I notice you are using TYPE_INT_RGB instead of TYPE_INT_ARGB (which includes alpha/transparency support).


    Looking at Snippet32 on the Eclipse site, I can see that you can usually pick up icons using the Program class. Using the extension ".Folder" doesn't seem to return an instance, even though it is a member of getExtensions().

    This code can get a folder icon:

    Display display = new Display();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.NONE);
    label.setText("Can't find icon");
    Image image = null;
    for (Program p : Program.getPrograms()) {
      if ("Folder".equals(p.getName())) {
        ImageData data = p.getImageData();
        if (data != null) {
          image = new Image(display, data);
          label.setImage(image);
        }
        break;
      }
    }
    label.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    if (image != null)
      image.dispose();
    display.dispose();
    

    That code needs refined, I think, but should be a pointer in the right direction. I only tested on English-language Windows XP.

提交回复
热议问题