How to set taskbar icon in Windows 7?

Started by hughperkins, January 25, 2011, 14:11:05

Previous topic - Next topic

hughperkins

I want to set a taskbar icon for my opengl window in Windows 7
- on linux, using Display.setIcon works fine
- doesn't seem to work for me on Windows 7

I've tried using 32x32, 16x16, and 40x40 images, but in all cases the taskbar icon is entirely missing, not even white or malformed, just entirely missing/transparent.

Any ideas?

CodeBunny

I'm running Windows 7 and I haven't had any problems.

Here's the code I use, if you want a look. It's designed to take any image that ImageIO can read and get sized images appropriately (16, 32, 128). If the image is larger than the desired size, it's shrunk to fit, but if it's smaller it's scaled to be an integer size (to keep pixel clarity`).

The actual call:
Display.setIcon(BufferLoader.loadIcon(filepath));


Then, in BufferLoader:
public static ByteBuffer[] loadIcon(String filepath)
	{
		BufferedImage image = null;
		try
		{
			image = ImageIO.read(BufferLoader.class.getClassLoader().getResource(filepath));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		ByteBuffer[] buffers = new ByteBuffer[3];
		buffers[0] = loadIconInstance(image, 128);
		buffers[1] = loadIconInstance(image, 32);
		buffers[2] = loadIconInstance(image, 16);
		return buffers;
	}
	
	private static ByteBuffer loadIconInstance(BufferedImage image, int dimension)
	{
		BufferedImage scaledIcon = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB_PRE);
		Graphics2D g = scaledIcon.createGraphics();
		double ratio = getIconRatio(image, scaledIcon);
		double width = image.getWidth() * ratio;
		double height = image.getHeight() * ratio;
		g.drawImage(image, (int) ((scaledIcon.getWidth() - width) / 2), 
				(int) ((scaledIcon.getHeight() - height) / 2),
				(int) (width), (int) (height), null);
		g.dispose();
		
		return readImageAsByteBuffer(scaledIcon);
	}
	
	private static double getIconRatio(BufferedImage originalImage, BufferedImage icon)
	{
		double ratio = 1;
		if(originalImage.getWidth() > icon.getWidth())
		{
			ratio = (double) (icon.getWidth()) / originalImage.getWidth();
		}
		else
		{
			ratio = (int) (icon.getWidth() / originalImage.getWidth());
		}
		if(originalImage.getHeight() > icon.getHeight())
		{
			double r2 = (double) (icon.getHeight()) / originalImage.getHeight();
			if(r2 < ratio)
			{
				ratio = r2;
			}
		}
		else
		{
			double r2 =  (int) (icon.getHeight() / originalImage.getHeight());
			if(r2 < ratio)
			{
				ratio = r2;
			}
		}
		return ratio;
	}

	public static ByteBuffer readImageAsByteBuffer(BufferedImage image)
	{
		byte[] imageBuffer = new byte[image.getWidth() * image.getHeight() * 4];
		int counter = 0;
		for(int i = 0; i < image.getHeight(); i++)
		{
			for(int j = 0; j < image.getWidth(); j++)
			{
				int colorSpace = image.getRGB(j, i);
				imageBuffer[counter + 0] =(byte)((colorSpace << 8) >> 24 );
				imageBuffer[counter + 1] =(byte)((colorSpace << 16) >> 24 );
				imageBuffer[counter + 2] =(byte)((colorSpace << 24) >> 24 );
				imageBuffer[counter + 3] =(byte)(colorSpace >> 24 );
				counter += 4;
			}
		}
		return ByteBuffer.wrap(imageBuffer);
	}


Does that work for you?