* You are viewing Posts Tagged ‘Icon’

Extracting executable file icon under SWT

Recently one of my colleagues at work found out that in pure SWT you’re not able to extract icon from an executable file, what you can only do is to get an icon from a file that is associated with specific file type (extension). So, I decided to write a method of my own, and here is it.

public ImageData getExecutableFileIcon(String fileName, boolean large)
{
    int nIconIndex = 0;
    ImageData imageData = null;

    TCHAR lpszFile = new TCHAR(0, fileName, true);
    int /*long*/ [] phiconSmall = new int /*long*/[1];
    int /*long*/ [] phiconLarge = new int /*long*/[1];
    OS.ExtractIconEx(lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);

    if (phiconSmall[0] != 0 && !large)
    {
        Image image = Image.win32_new(null, SWT.ICON, phiconSmall[0]);
        imageData = image.getImageData();
        image.dispose();
    }

    if (phiconLarge[0] != 0 && large)
    {
        Image image = Image.win32_new(null, SWT.ICON, phiconLarge[0]);
        imageData = image.getImageData();
        image.dispose();
    }

    return imageData;
}

At this moment I don’t really know whether we should call OS.ExtractIconEx for both images, or depending on the flag ask for one of them, dunno.

Although, I have a question for Eclipse community, why similar functionality isn’t available in SWT? Or if it is, where to find it?