将光标类是相当抽象的-
所有重要的东西委托给本机代码,所以你不能只画上一个在图形上下文。尚无立即显而易见的方法来绕过预定义图标或在本机代码中进行 *** 作的需要。
您能帮我使用您提到的功能吗?
以下是一些使用JNA库绘制内置Windows光标的代码。如果可以使用JNA,则可以避免使用C
++编译器。
我可能打了太多本地电话,但是对于一次生成图标来说,花费并不大。
用Java
http://f.imagehost.org/0709/hand.png绘制的手形光标
将光标显示为Java图像的代码:
public class LoadCursor { public static void draw(BufferedImage image, int cursor, int diFlags) { int width = image.getWidth(); int height = image.getHeight(); User32 user32 = User32.INSTANCE; Gdi32 gdi32 = Gdi32.INSTANCE; Pointer hIcon = user32 .LoadCursorW(Pointer.NULL, cursor); Pointer hdc = gdi32.CreateCompatibleDC(Pointer.NULL); Pointer bitmap = gdi32.CreateCompatibleBitmap(hdc, width, height); gdi32.SelectObject(hdc, bitmap); user32.DrawIconEx(hdc, 0, 0, hIcon, width, height, 0, Pointer.NULL, diFlags); for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { int rgb = gdi32.GetPixel(hdc, x, y); image.setRGB(x, y, rgb); } } gdi32.DeleteObject(bitmap); gdi32.DeleteDC(hdc); } public static void main(String[] args) { final int width = 128; final int height = 128; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); draw(image, User32.IDC_HAND, User32.DI_NORMAL); BufferedImage mask = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); draw(mask, User32.IDC_HAND, User32.DI_MASK); applyMask(image, mask); JLabel icon = new JLabel(); icon.setIcon(new ImageIcon(image)); Jframe frame = new Jframe(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setContentPane(icon); frame.pack(); frame.setVisible(true); } private static void applyMask(BufferedImage image, BufferedImage mask) { int width = image.getWidth(); int height = image.getHeight(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int masked = mask.getRGB(x, y); if ((masked & 0x00FFFFFF) == 0) { int rgb = image.getRGB(x, y); rgb = 0xFF000000 | rgb; image.setRGB(x, y, rgb); } } } }}
User32.dll接口:
public interface User32 extends Library { public static User32 INSTANCE = (User32) Native .loadLibrary("User32", User32.class); public static final int IDC_ARROW = 32512; public static final int IDC_IBEAM = 32513; public static final int IDC_WAIT = 32514; public static final int IDC_CROSS = 32515; public static final int IDC_UPARROW = 32516; public static final int IDC_SIZENWSE = 32642; public static final int IDC_SIZENESW = 32643; public static final int IDC_SIZEWE = 32644; public static final int IDC_SIZENS = 32645; public static final int IDC_SIZEALL = 32646; public static final int IDC_NO = 32648; public static final int IDC_HAND = 32649; public static final int IDC_APPSTARTING = 32650; public static final int IDC_HELP = 32651; public static final int IDC_ICON = 32641; public static final int IDC_SIZE = 32640; public static final int DI_COMPAT = 4; public static final int DI_DEFAULTSIZE = 8; public static final int DI_IMAGE = 2; public static final int DI_MASK = 1; public static final int DI_NORMAL = 3; public static final int DI_APPBANDING = 1; public Pointer LoadCursorW(Pointer hInstance, int lpCursorName); public boolean DrawIconEx(Pointer hdc, int xLeft, int yTop, Pointer hIcon, int cxWidth, int cyWidth, int istepIfAniCur, Pointer hbrFlickerFreeDraw, int diFlags);}
Gdi32.dll接口:
public interface Gdi32 extends Library { public static Gdi32 INSTANCE = (Gdi32) Native .loadLibrary("Gdi32", Gdi32.class); public Pointer CreateCompatibleDC(Pointer hdc); public Pointer CreateCompatibleBitmap(Pointer hdc, int nWidth, int nHeight); public Pointer SelectObject(Pointer hdc, Pointer hgdiobj); public int SetPixel(Pointer hdc, int X, int Y, int crColor); public int GetPixel(Pointer hdc, int nXPos, int nYPos); public boolean DeleteObject(Pointer hObject); public boolean DeleteDC(Pointer hdc);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)