?? gc.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.graphics;import org.eclipse.swt.internal.*;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;/** * Class <code>GC</code> is where all of the drawing capabilities that are * supported by SWT are located. Instances are used to draw on either an * <code>Image</code>, a <code>Control</code>, or directly on a <code>Display</code>. * <dl> * <dt><b>Styles:</b></dt> * <dd>LEFT_TO_RIGHT, RIGHT_TO_LEFT</dd> * </dl> * * <p> * The SWT drawing coordinate system is the two-dimensional space with the origin * (0,0) at the top left corner of the drawing area and with (x,y) values increasing * to the right and downward respectively. * </p> * * <p> * Application code must explicitly invoke the <code>GC.dispose()</code> * method to release the operating system resources managed by each instance * when those instances are no longer required. This is <em>particularly</em> * important on Windows95 and Windows98 where the operating system has a limited * number of device contexts available. * </p> * * <p> * Note: Only one of LEFT_TO_RIGHT and RIGHT_TO_LEFT may be specified. * </p> * * @see org.eclipse.swt.events.PaintEvent */public final class GC { /** * the handle to the OS device context * (Warning: This field is platform dependent) */ public int handle; Drawable drawable; GCData data;/** * Prevents uninitialized instances from being created outside the package. */GC() {}/** * Constructs a new instance of this class which has been * configured to draw on the specified drawable. Sets the * foreground and background color in the GC to match those * in the drawable. * <p> * You must dispose the graphics context when it is no longer required. * </p> * @param drawable the drawable to draw on * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the drawable is null</li> * <li>ERROR_NULL_ARGUMENT - if there is no current device</li> * <li>ERROR_INVALID_ARGUMENT * - if the drawable is an image that is not a bitmap or an icon * - if the drawable is an image or printer that is already selected * into another graphics context</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for gc creation</li> * </ul> */public GC(Drawable drawable) { this(drawable, SWT.NONE);}/** * Constructs a new instance of this class which has been * configured to draw on the specified drawable. Sets the * foreground and background color in the GC to match those * in the drawable. * <p> * You must dispose the graphics context when it is no longer required. * </p> * * @param drawable the drawable to draw on * @param style the style of GC to construct * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the drawable is null</li> * <li>ERROR_NULL_ARGUMENT - if there is no current device</li> * <li>ERROR_INVALID_ARGUMENT * - if the drawable is an image that is not a bitmap or an icon * - if the drawable is an image or printer that is already selected * into another graphics context</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for gc creation</li> * </ul> * * @since 2.1.2 */public GC(Drawable drawable, int style) { if (drawable == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); GCData data = new GCData (); data.style = checkStyle(style); int hDC = drawable.internal_new_GC(data); Device device = data.device; if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); data.device = device; init (drawable, data, hDC); if (device.tracking) device.new_Object(this); }static int checkStyle(int style) { if ((style & SWT.LEFT_TO_RIGHT) != 0) style &= ~SWT.RIGHT_TO_LEFT; return style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);}/** * Copies a rectangular area of the receiver at the specified * position into the image, which must be of type <code>SWT.BITMAP</code>. * * @param image the image to copy into * @param x the x coordinate in the receiver of the area to be copied * @param y the y coordinate in the receiver of the area to be copied * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the image is null</li> * <li>ERROR_INVALID_ARGUMENT - if the image is not a bitmap or has been disposed</li> * </ul> * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void copyArea(Image image, int x, int y) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (image.type != SWT.BITMAP || image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); /* Get the HDC for the device */ Device device = data.device; int hDC = device.internal_new_GC(null); /* Copy the bitmap area */ Rectangle rect = image.getBounds(); int memHdc = OS.CreateCompatibleDC(hDC); int hOldBitmap = OS.SelectObject(memHdc, image.handle); OS.BitBlt(memHdc, 0, 0, rect.width, rect.height, handle, x, y, OS.SRCCOPY); OS.SelectObject(memHdc, hOldBitmap); OS.DeleteDC(memHdc); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null);}/** * Copies a rectangular area of the receiver at the source * position onto the receiver at the destination position. * * @param srcX the x coordinate in the receiver of the area to be copied * @param srcY the y coordinate in the receiver of the area to be copied * @param width the width of the area to copy * @param height the height of the area to copy * @param destX the x coordinate in the receiver of the area to copy to * @param destY the y coordinate in the receiver of the area to copy to * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void copyArea(int srcX, int srcY, int width, int height, int destX, int destY) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); /* * Feature in WinCE. The function WindowFromDC is not part of the * WinCE SDK. The fix is to remember the HWND. */ int hwnd = data.hwnd; if (hwnd == 0) { OS.BitBlt(handle, destX, destY, width, height, handle, srcX, srcY, OS.SRCCOPY); } else { RECT lprcClip = null; int hrgn = OS.CreateRectRgn(0, 0, 0, 0); if (OS.GetClipRgn(handle, hrgn) == 1) { lprcClip = new RECT(); OS.GetRgnBox(hrgn, lprcClip); } OS.DeleteObject(hrgn); RECT lprcScroll = new RECT(); OS.SetRect(lprcScroll, srcX, srcY, srcX + width, srcY + height); int res = OS.ScrollWindowEx(hwnd, destX - srcX, destY - srcY, lprcScroll, lprcClip, 0, null, OS.SW_INVALIDATE | OS.SW_ERASE); /* * Feature in WinCE. ScrollWindowEx does not accept combined * vertical and horizontal scrolling. The fix is to do a * BitBlt and invalidate the appropriate source area. */ if (res == 0 && OS.IsWinCE) { OS.BitBlt(handle, destX, destY, width, height, handle, srcX, srcY, OS.SRCCOPY); int deltaX = destX - srcX, deltaY = destY - srcY; boolean disjoint = (destX + width < srcX) || (srcX + width < destX) || (destY + height < srcY) || (srcY + height < destY); if (disjoint) { OS.InvalidateRect(hwnd, lprcScroll, true); } else { if (deltaX != 0) { int newX = destX - deltaX; if (deltaX < 0) newX = destX + width; OS.SetRect(lprcScroll, newX, srcY, newX + Math.abs(deltaX), srcY + height); OS.InvalidateRect(hwnd, lprcScroll, true); } if (deltaY != 0) { int newY = destY - deltaY; if (deltaY < 0) newY = destY + height; OS.SetRect(lprcScroll, srcX, newY, srcX + width, newY + Math.abs(deltaY)); OS.InvalidateRect(hwnd, lprcScroll, true); } } } }}int createDIB(int width, int height) { short depth = 32; BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER(); bmiHeader.biSize = BITMAPINFOHEADER.sizeof; bmiHeader.biWidth = width; bmiHeader.biHeight = -height; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = depth; if (OS.IsWinCE) bmiHeader.biCompression = OS.BI_BITFIELDS; else bmiHeader.biCompression = OS.BI_RGB; byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + (OS.IsWinCE ? 12 : 0)]; OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof); /* Set the rgb colors into the bitmap info */ if (OS.IsWinCE) { int redMask = 0xFF00; int greenMask = 0xFF0000; int blueMask = 0xFF000000; /* big endian */ int offset = BITMAPINFOHEADER.sizeof; bmi[offset] = (byte)((redMask & 0xFF000000) >> 24); bmi[offset + 1] = (byte)((redMask & 0xFF0000) >> 16); bmi[offset + 2] = (byte)((redMask & 0xFF00) >> 8); bmi[offset + 3] = (byte)((redMask & 0xFF) >> 0); bmi[offset + 4] = (byte)((greenMask & 0xFF000000) >> 24); bmi[offset + 5] = (byte)((greenMask & 0xFF0000) >> 16); bmi[offset + 6] = (byte)((greenMask & 0xFF00) >> 8); bmi[offset + 7] = (byte)((greenMask & 0xFF) >> 0); bmi[offset + 8] = (byte)((blueMask & 0xFF000000) >> 24); bmi[offset + 9] = (byte)((blueMask & 0xFF0000) >> 16); bmi[offset + 10] = (byte)((blueMask & 0xFF00) >> 8); bmi[offset + 11] = (byte)((blueMask & 0xFF) >> 0); } int[] pBits = new int[1]; int hDib = OS.CreateDIBSection(0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0); if (hDib == 0) SWT.error(SWT.ERROR_NO_HANDLES); return hDib;}/** * Disposes of the operating system resources associated with * the graphics context. Applications must dispose of all GCs * which they allocate. */public void dispose() { if (handle == 0) return; if (data.device.isDisposed()) return; /* Select stock pen and brush objects and free resources */ if (data.hPen != 0) { int nullPen = OS.GetStockObject(OS.NULL_PEN); OS.SelectObject(handle, nullPen); OS.DeleteObject(data.hPen); data.hPen = 0; } if (data.hBrush != 0) { int nullBrush = OS.GetStockObject(OS.NULL_BRUSH); OS.SelectObject(handle, nullBrush); OS.DeleteObject(data.hBrush); data.hBrush = 0; } /* * Put back the original bitmap into the device context. * This will ensure that we have not left a bitmap * selected in it when we delete the HDC. */ int hNullBitmap = data.hNullBitmap; if (hNullBitmap != 0) { OS.SelectObject(handle, hNullBitmap); data.hNullBitmap = 0; } Image image = data.image; if (image != null) image.memGC = null; /* * Dispose the HDC. */ Device device = data.device; if (drawable != null) drawable.internal_dispose_GC(handle, data); drawable = null; handle = 0; data.image = null; data.ps = null; if (device.tracking) device.dispose_Object(this); data.device = null; data = null;}/** * Draws the outline of a circular or elliptical arc * within the specified rectangular area. * <p> * The resulting arc begins at <code>startAngle</code> and extends * for <code>arcAngle</code> degrees, using the current color. * Angles are interpreted such that 0 degrees is at the 3 o'clock * position. A positive value indicates a counter-clockwise rotation * while a negative value indicates a clockwise rotation. * </p><p> * The center of the arc is the center of the rectangle whose origin * is (<code>x</code>, <code>y</code>) and whose size is specified by the * <code>width</code> and <code>height</code> arguments. * </p><p> * The resulting arc covers an area <code>width + 1</code> pixels wide * by <code>height + 1</code> pixels tall. * </p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -