📄 gdkgc-win32.c
字号:
/** * gdk_win32_hdc_get: * @drawable: destination #GdkDrawable * @gc: #GdkGC to use for drawing on @drawable * @usage: mask indicating what properties needs to be set up * * Allocates a Windows device context handle (HDC) for drawing into * @drawable, and sets it up appropriately according to @usage. * * Each #GdkGC can at one time have only one HDC associated with it. * * The following flags in @mask are handled: * * If %GDK_GC_FOREGROUND is set in @mask, a solid brush of the * foreground color in @gc is selected into the HDC. The text color of * the HDC is also set. If the @drawable has a palette (256-color * mode), the palette is selected and realized. * * If any of the line attribute flags (%GDK_GC_LINE_WIDTH, * %GDK_GC_LINE_STYLE, %GDK_GC_CAP_STYLE and %GDK_GC_JOIN_STYLE) is * set in @mask, a solid pen of the foreground color and appropriate * width and stule is created and selected into the HDC. Note that the * dash properties are not completely implemented. * * If the %GDK_GC_FONT flag is set, the background mix mode is set to * %TRANSPARENT. and the text alignment is set to * %TA_BASELINE|%TA_LEFT. Note that no font gets selected into the HDC * by this function. * * Some things are done regardless of @mask: If the function in @gc is * any other than %GDK_COPY, the raster operation of the HDC is * set. If @gc has a clip mask, the clip region of the HDC is set. * * Note that the fill style, tile, stipple, and tile and stipple * origins in the @gc are ignored by this function. (In general, tiles * and stipples can't be implemented directly on Win32; you need to do * multiple pass drawing and blitting to implement tiles or * stipples. GDK does just that when you call the GDK drawing * functions with a GC that asks for tiles or stipples.) * * When the HDC is no longer used, it should be released by calling * <function>gdk_win32_hdc_release()</function> with the same * parameters. * * If you modify the HDC by calling <function>SelectObject</function> * you should undo those modifications before calling * <function>gdk_win32_hdc_release()</function>. * * Return value: The HDC. **/HDCgdk_win32_hdc_get (GdkDrawable *drawable, GdkGC *gc, GdkGCValuesMask usage){ GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc; GdkDrawableImplWin32 *impl = NULL; gboolean ok = TRUE; COLORREF fg = RGB (0, 0, 0), bg = RGB (255, 255, 255); HPEN hpen; HBRUSH hbr; g_assert (win32_gc->hdc == NULL); if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32(drawable); else if (GDK_IS_WINDOW (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl); else if (GDK_IS_PIXMAP (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl); else g_assert_not_reached (); win32_gc->hwnd = impl->handle; if (GDK_IS_PIXMAP_IMPL_WIN32 (impl)) { if ((win32_gc->hdc = CreateCompatibleDC (NULL)) == NULL) WIN32_GDI_FAILED ("CreateCompatibleDC"), ok = FALSE; if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0) WIN32_GDI_FAILED ("SaveDC"), ok = FALSE; if (ok && SelectObject (win32_gc->hdc, win32_gc->hwnd) == NULL) WIN32_GDI_FAILED ("SelectObject"), ok = FALSE; } else { if ((win32_gc->hdc = GetDC (win32_gc->hwnd)) == NULL) WIN32_GDI_FAILED ("GetDC"); if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0) WIN32_GDI_FAILED ("SaveDC"); } if (ok && (usage & (GDK_GC_FOREGROUND | GDK_GC_BACKGROUND))) ok = predraw (gc, impl->colormap); if (ok && (usage & GDK_GC_FOREGROUND)) { fg = _gdk_win32_colormap_color (impl->colormap, win32_gc->foreground); if ((hbr = CreateSolidBrush (fg)) == NULL) WIN32_GDI_FAILED ("CreateSolidBrush"), ok = FALSE; if (ok && SelectObject (win32_gc->hdc, hbr) == NULL) WIN32_GDI_FAILED ("SelectObject"), ok = FALSE; if (ok && SetTextColor (win32_gc->hdc, fg) == CLR_INVALID) WIN32_GDI_FAILED ("SetTextColor"), ok = FALSE; } if (ok && (usage & LINE_ATTRIBUTES)) { /* For drawing GDK_LINE_DOUBLE_DASH */ if ((usage & GDK_GC_BACKGROUND) && win32_gc->line_style == GDK_LINE_DOUBLE_DASH) { bg = _gdk_win32_colormap_color (impl->colormap, win32_gc->background); if ((win32_gc->pen_hbrbg = CreateSolidBrush (bg)) == NULL) WIN32_GDI_FAILED ("CreateSolidBrush"), ok = FALSE; } if (ok) { LOGBRUSH logbrush; DWORD style_count = 0; const DWORD *style = NULL; /* Create and select pen */ logbrush.lbStyle = BS_SOLID; logbrush.lbColor = fg; logbrush.lbHatch = 0; if ((win32_gc->pen_style & PS_STYLE_MASK) == PS_USERSTYLE) { style_count = win32_gc->pen_num_dashes; style = win32_gc->pen_dashes; } if ((hpen = ExtCreatePen (win32_gc->pen_style, MAX (win32_gc->pen_width, 1), &logbrush, style_count, style)) == NULL) WIN32_GDI_FAILED ("ExtCreatePen"), ok = FALSE; if (ok && SelectObject (win32_gc->hdc, hpen) == NULL) WIN32_GDI_FAILED ("SelectObject"), ok = FALSE; } } if (ok && (usage & GDK_GC_FONT)) { if (SetBkMode (win32_gc->hdc, TRANSPARENT) == 0) WIN32_GDI_FAILED ("SetBkMode"), ok = FALSE; if (ok && SetTextAlign (win32_gc->hdc, TA_BASELINE|TA_LEFT|TA_NOUPDATECP) == GDI_ERROR) WIN32_GDI_FAILED ("SetTextAlign"), ok = FALSE; } if (ok && win32_gc->rop2 != R2_COPYPEN) if (SetROP2 (win32_gc->hdc, win32_gc->rop2) == 0) WIN32_GDI_FAILED ("SetROP2"), ok = FALSE; if (ok && (win32_gc->values_mask & GDK_GC_CLIP_MASK) && win32_gc->hcliprgn != NULL) { if (SelectClipRgn (win32_gc->hdc, win32_gc->hcliprgn) == ERROR) WIN32_API_FAILED ("SelectClipRgn"), ok = FALSE; if (ok && win32_gc->values_mask & (GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN) && OffsetClipRgn (win32_gc->hdc, win32_gc->values_mask & GDK_GC_CLIP_X_ORIGIN ? gc->clip_x_origin : 0, win32_gc->values_mask & GDK_GC_CLIP_Y_ORIGIN ? gc->clip_y_origin : 0) == ERROR) WIN32_API_FAILED ("OffsetClipRgn"), ok = FALSE; } GDK_NOTE (GC, (g_print ("gdk_win32_hdc_get: %p (%s): ", win32_gc, _gdk_win32_gcvalues_mask_to_string (usage)), _gdk_win32_print_dc (win32_gc->hdc))); return win32_gc->hdc;}/** * gdk_win32_hdc_release: * @drawable: destination #GdkDrawable * @gc: #GdkGC to use for drawing on @drawable * @usage: mask indicating what properties were set up * * This function deallocates the Windows device context allocated by * <funcion>gdk_win32_hdc_get()</function>. It should be called with * the same parameters. **/voidgdk_win32_hdc_release (GdkDrawable *drawable, GdkGC *gc, GdkGCValuesMask usage){ GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc; GdkDrawableImplWin32 *impl = NULL; HGDIOBJ hpen = NULL; HGDIOBJ hbr = NULL; GDK_NOTE (GC, g_print ("gdk_win32_hdc_release: %p: %p (%s)\n", win32_gc, win32_gc->hdc, _gdk_win32_gcvalues_mask_to_string (usage))); if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32(drawable); else if (GDK_IS_WINDOW (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl); else if (GDK_IS_PIXMAP (drawable)) impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl); else g_assert_not_reached (); if (win32_gc->holdpal != NULL) { gint k; if (!SelectPalette (win32_gc->hdc, win32_gc->holdpal, FALSE)) WIN32_GDI_FAILED ("SelectPalette"); else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR) WIN32_GDI_FAILED ("RealizePalette"); else if (k > 0) GDK_NOTE (COLORMAP, g_print ("gdk_win32_hdc_release: realized %p: %d colors\n", win32_gc->holdpal, k)); win32_gc->holdpal = NULL; } if (usage & LINE_ATTRIBUTES) if ((hpen = GetCurrentObject (win32_gc->hdc, OBJ_PEN)) == NULL) WIN32_GDI_FAILED ("GetCurrentObject"); if (usage & GDK_GC_FOREGROUND) if ((hbr = GetCurrentObject (win32_gc->hdc, OBJ_BRUSH)) == NULL) WIN32_GDI_FAILED ("GetCurrentObject"); GDI_CALL (RestoreDC, (win32_gc->hdc, win32_gc->saved_dc)); if (GDK_IS_PIXMAP_IMPL_WIN32 (impl)) GDI_CALL (DeleteDC, (win32_gc->hdc)); else GDI_CALL (ReleaseDC, (win32_gc->hwnd, win32_gc->hdc)); if (hpen != NULL) GDI_CALL (DeleteObject, (hpen)); if (hbr != NULL) GDI_CALL (DeleteObject, (hbr)); if (win32_gc->pen_hbrbg != NULL) GDI_CALL (DeleteObject, (win32_gc->pen_hbrbg)); win32_gc->hdc = NULL;}/* This function originally from Jean-Edouard Lachand-Robert, and * available at www.codeguru.com. Simplified for our needs, not sure * how much of the original code left any longer. Now handles just * one-bit deep bitmaps (in Window parlance, ie those that GDK calls * bitmaps (and not pixmaps), with zero pixels being transparent. *//* _gdk_win32_bitmap_to_hrgn : Create a region from the * "non-transparent" pixels of a bitmap. */HRGN_gdk_win32_bitmap_to_hrgn (GdkPixmap *pixmap){ HRGN hRgn = NULL; HRGN h; DWORD maxRects; RGNDATA *pData; guchar *bits; gint width, height, bpl; guchar *p; gint x, y; g_assert (GDK_PIXMAP_OBJECT(pixmap)->depth == 1); bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits; width = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->width; height = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->height; bpl = ((width - 1)/32 + 1)*4; /* For better performances, we will use the ExtCreateRegion() * function to create the region. This function take a RGNDATA * structure on entry. We will add rectangles by amount of * ALLOC_UNIT number in this structure. */ #define ALLOC_UNIT 100 maxRects = ALLOC_UNIT; pData = g_malloc (sizeof (RGNDATAHEADER) + (sizeof (RECT) * maxRects)); pData->rdh.dwSize = sizeof (RGNDATAHEADER); pData->rdh.iType = RDH_RECTANGLES; pData->rdh.nCount = pData->rdh.nRgnSize = 0; SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0); for (y = 0; y < height; y++) { /* Scan each bitmap row from left to right*/ p = (guchar *) bits + y * bpl; for (x = 0; x < width; x++) { /* Search for a continuous range of "non transparent pixels"*/ gint x0 = x; while (x < width) { if ((((p[x/8])>>(7-(x%8)))&1) == 0) /* This pixel is "transparent"*/ break; x++; } if (x > x0) { RECT *pr; /* Add the pixels (x0, y) to (x, y+1) as a new rectangle * in the region */ if (pData->rdh.nCount >= maxRects) { maxRects += ALLOC_UNIT; pData = g_realloc (pData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects)); } pr = (RECT *) &pData->Buffer; SetRect (&pr[pData->rdh.nCount], x0, y, x, y+1); if (x0 < pData->rdh.rcBound.left) pData->rdh.rcBound.left = x0; if (y < pData->rdh.rcBound.top) pData->rdh.rcBound.top = y; if (x > pData->rdh.rcBound.right) pData->rdh.rcBound.right = x; if (y+1 > pData->rdh.rcBound.bottom) pData->rdh.rcBound.bottom = y+1; pData->rdh.nCount++; /* On Windows98, ExtCreateRegion() may fail if the * number of rectangles is too large (ie: > * 4000). Therefore, we have to create the region by * multiple steps. */ if (pData->rdh.nCount == 2000) { HRGN h = ExtCreateRegion (NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData); if (hRgn) { CombineRgn(hRgn, hRgn, h, RGN_OR); DeleteObject(h); } else hRgn = h; pData->rdh.nCount = 0; SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0); } } } } /* Create or extend the region with the remaining rectangles*/ h = ExtCreateRegion (NULL, sizeof (RGNDATAHEADER) + (sizeof (RECT) * maxRects), pData); if (hRgn) { CombineRgn (hRgn, hRgn, h, RGN_OR); DeleteObject (h); } else hRgn = h; /* Clean up*/ g_free (pData); return hRgn;}HRGN_gdk_win32_gdkregion_to_hrgn (GdkRegion *region, gint x_origin, gint y_origin){ HRGN hrgn; RGNDATA *rgndata; RECT *rect; GdkRegionBox *boxes = region->rects; guint nbytes = sizeof (RGNDATAHEADER) + (sizeof (RECT) * region->numRects); int i; rgndata = g_malloc (nbytes); rgndata->rdh.dwSize = sizeof (RGNDATAHEADER); rgndata->rdh.iType = RDH_RECTANGLES; rgndata->rdh.nCount = rgndata->rdh.nRgnSize = 0; SetRect (&rgndata->rdh.rcBound, G_MAXLONG, G_MAXLONG, G_MINLONG, G_MINLONG); for (i = 0; i < region->numRects; i++) { rect = ((RECT *) rgndata->Buffer) + rgndata->rdh.nCount++; rect->left = boxes[i].x1 + x_origin; rect->right = boxes[i].x2 + x_origin; rect->top = boxes[i].y1 + y_origin; rect->bottom = boxes[i].y2 + y_origin; if (rect->left < rgndata->rdh.rcBound.left) rgndata->rdh.rcBound.left = rect->left; if (rect->right > rgndata->rdh.rcBound.right) rgndata->rdh.rcBound.right = rect->right; if (rect->top < rgndata->rdh.rcBound.top) rgndata->rdh.rcBound.top = rect->top; if (rect->bottom > rgndata->rdh.rcBound.bottom) rgndata->rdh.rcBound.bottom = rect->bottom; } if ((hrgn = ExtCreateRegion (NULL, nbytes, rgndata)) == NULL) WIN32_API_FAILED ("ExtCreateRegion"); g_free (rgndata); return (hrgn);}void_gdk_windowing_gc_get_foreground (GdkGC *gc, GdkColor *color){ GdkGCWin32 *win32_gc; GdkColormap *cmap; g_return_if_fail (GDK_IS_GC_WIN32 (gc)); win32_gc = GDK_GC_WIN32 (gc); color->pixel = win32_gc->foreground; cmap = gdk_gc_get_colormap (gc); if (cmap) gdk_colormap_query_color (cmap, win32_gc->foreground, color); else g_warning ("No colormap in _gdk_windowing_gc_get_foreground");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -