📄 cursor.java
字号:
* <p> * The mask data is allowed to be null, but in this case the source * must be an ImageData representing an icon that specifies both * color data and mask data. * <p> * You must dispose the cursor when it is no longer required. * </p> * * @param device the device on which to allocate the cursor * @param source the color data for the cursor * @param mask the mask data for the cursor (or null) * @param hotspotX the x coordinate of the cursor's hotspot * @param hotspotY the y coordinate of the cursor's hotspot * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the source is null</li> * <li>ERROR_NULL_ARGUMENT - if the mask is null and the source does not have a mask</li> * <li>ERROR_INVALID_ARGUMENT - if the source and the mask are not the same * size, or either is not of depth one, or if the hotspot is outside * the bounds of the image</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li> * </ul> */public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.device = device; if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (mask == null) { if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } mask = source.getTransparencyMask(); } /* Check the bounds. Mask must be the same size as source */ if (mask.width != source.width || mask.height != source.height) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } /* Check color depths */ if (mask.depth != 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (source.depth != 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); /* Check the hotspots */ if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } /* Make sure source and mask scanline pad is 2 */ byte[] sourceData = ImageData.convertPad(source.data, source.width, source.height, source.depth, source.scanlinePad, 2); byte[] maskData = ImageData.convertPad(mask.data, mask.width, mask.height, mask.depth, mask.scanlinePad, 2); /* Create the cursor */ int hInst = OS.GetModuleHandle(null); if (OS.IsWinCE) SWT.error (SWT.ERROR_NOT_IMPLEMENTED); handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); if (device.tracking) device.new_Object(this);}/** * Constructs a new cursor given a device, image data describing * the desired cursor appearance, and the x and y coordinates of * the <em>hotspot</em> (that is, the point within the area * covered by the cursor which is considered to be where the * on-screen pointer is "pointing"). * <p> * You must dispose the cursor when it is no longer required. * </p> * * @param device the device on which to allocate the cursor * @param source the image data for the cursor * @param hotspotX the x coordinate of the cursor's hotspot * @param hotspotY the y coordinate of the cursor's hotspot * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the image is null</li> * <li>ERROR_INVALID_ARGUMENT - if the hotspot is outside the bounds of the * image</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li> * </ul> * * @since 3.0 */public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.device = device; if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); /* Check the hotspots */ if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } ImageData mask = source.getTransparencyMask(); int[] result = Image.init(device, null, source, mask); int hBitmap = result[0]; int hMask = result[1]; /* Create the icon */ ICONINFO info = new ICONINFO(); info.fIcon = true; info.hbmColor = hBitmap; info.hbmMask = hMask; handle = OS.CreateIconIndirect(info); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); OS.DeleteObject(hBitmap); OS.DeleteObject(hMask); isIcon = true; if (device.tracking) device.new_Object(this);}/** * Disposes of the operating system resources associated with * the cursor. Applications must dispose of all cursors which * they allocate. */public void dispose () { if (handle == 0) return; if (device.isDisposed()) return; /* * It is an error in Windows to destroy the current * cursor. Check that the cursor that is about to * be destroyed is the current cursor. If so, set * the current cursor to be IDC_ARROW. Note that * Windows shares predefined cursors so the call to * LoadCursor() does not leak. */ // TEMPORARY CODE// if (OS.GetCursor() == handle) {// OS.SetCursor(OS.LoadCursor(0, OS.IDC_ARROW));// } if (isIcon) { OS.DestroyIcon(handle); } else { /* * The MSDN states that one should not destroy a shared * cursor, that is, one obtained from LoadCursor. * However, it does not appear to do any harm, so rather * than keep track of how a cursor was created, we just * destroy them all. If this causes problems in the future, * put the flag back in. */ if (!OS.IsWinCE) OS.DestroyCursor(handle); } handle = 0; if (device.tracking) device.dispose_Object(this); device = null;}/** * Compares the argument to the receiver, and returns true * if they represent the <em>same</em> object using a class * specific comparison. * * @param object the object to compare with this object * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise * * @see #hashCode */public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof Cursor)) return false; Cursor cursor = (Cursor) object; return device == cursor.device && handle == cursor.handle;}/** * Returns an integer hash code for the receiver. Any two * objects which return <code>true</code> when passed to * <code>equals</code> must return the same value for this * method. * * @return the receiver's hash * * @see #equals */public int hashCode () { return handle;}/** * Returns <code>true</code> if the cursor has been disposed, * and <code>false</code> otherwise. * <p> * This method gets the dispose state for the cursor. * When a cursor has been disposed, it is an error to * invoke any other method using the cursor. * * @return <code>true</code> when the cursor is disposed and <code>false</code> otherwise */public boolean isDisposed() { return handle == 0;}/** * Returns a string containing a concise, human-readable * description of the receiver. * * @return a string representation of the receiver */public String toString () { if (isDisposed()) return "Cursor {*DISPOSED*}"; return "Cursor {" + handle + "}";}/** * Invokes platform specific functionality to allocate a new cursor. * <p> * <b>IMPORTANT:</b> This method is <em>not</em> part of the public * API for <code>Cursor</code>. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. * </p> * * @param device the device on which to allocate the color * @param handle the handle for the cursor * @return a new cursor object containing the specified device and handle */public static Cursor win32_new(Device device, int handle) { if (device == null) device = Device.getDevice(); Cursor cursor = new Cursor(); cursor.handle = handle; cursor.device = device; return cursor;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -