/// <summary> /// Draws the formatted string on a given graphics, clipping the text by the /// ClipBounds set on the Graphics. /// </summary> /// <param graphics device context to draw the string in.</param> /// <param name="rect">The rectangle where the string is drawn.</param> /// <exception cref="ArgumentNullException"><i>g</i> is null</exception> public void DrawClipped(Graphics graphics, Rectangle rect) { RectangleF rcClip = graphics.ClipBounds; IntPtr hdc = graphics.GetHdc(); try { IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0); if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1) { Win32Declarations.DeleteObject(clipRgn); clipRgn = IntPtr.Zero; } Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom); Draw(hdc, rect); Win32Declarations.SelectClipRgn(hdc, clipRgn); Win32Declarations.DeleteObject(clipRgn); } finally { graphics.ReleaseHdc(hdc); } }
public static Bitmap ConvertIco2Bmp(Icon icon, Brush backBrush) { Bitmap bmp; // Declare the raw handles IntPtr hdcDisplay = IntPtr.Zero, hdcMem = IntPtr.Zero, hBitmap = IntPtr.Zero; try { // Get the display's Device Context so that the generated bitmaps were compatible hdcDisplay = Win32Declarations.CreateDC("DISPLAY", null, null, IntPtr.Zero); if (hdcDisplay == IntPtr.Zero) { throw new Exception("Failed to get the display device context."); } // Create a pixel format compatible device context, and a bitmap to draw into using this context hdcMem = Win32Declarations.CreateCompatibleDC(hdcDisplay); if (hdcMem == IntPtr.Zero) { throw new Exception("Could not cerate a compatible device context."); } hBitmap = Win32Declarations.CreateCompatibleBitmap(hdcDisplay, icon.Width, icon.Height); if (hBitmap == IntPtr.Zero) { throw new Exception("Could not cerate a compatible offscreen bitmap."); } Win32Declarations.SelectObject(hdcMem, hBitmap); // Attach a GDI+ Device Context to the offscreen facility, and render the scene using (Graphics gm = Graphics.FromHdc(hdcMem)) { gm.FillRectangle(backBrush, 0, 0, icon.Width, icon.Height); gm.DrawIcon(icon, 0, 0); } // Wrap the offscreen bitmap into a .NET bitmap in order to save it bmp = Image.FromHbitmap(hBitmap); } finally { // Cleanup the native resources in use if (hBitmap != IntPtr.Zero) { Win32Declarations.DeleteObject(hBitmap); } if (hdcMem != IntPtr.Zero) { Win32Declarations.DeleteDC(hdcMem); } if (hdcDisplay != IntPtr.Zero) { Win32Declarations.DeleteDC(hdcDisplay); } } return(bmp); }
public void Dispose() { foreach (IntPtr fontHandle in _fontHandleList) { Win32Declarations.DeleteObject(fontHandle); } _fontHandleList.Clear(); _fontList.Clear(); _fontFamilies.Clear(); }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } Win32Declarations.DeleteObject(_fontHandle); base.Dispose(disposing); }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (_unreadResources != null) { _unreadResources.Dispose(); } if (components != null) { components.Dispose(); } } Win32Declarations.DeleteObject(_hFontCounter); Core.ResourceAP.JobFinished -= OnResourceOperationFinished; base.Dispose(disposing); }
protected override void WndProc(ref Message m) { if (m.Msg == Win32Declarations.WM_NCPAINT && BorderStyle == BorderStyle.FixedSingle) { IntPtr hdc = Win32Declarations.GetWindowDC(Handle); IntPtr brush = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(_borderColor)); IntPtr oldBrush = Win32Declarations.SelectObject(hdc, brush); RECT rect = new RECT(0, 0, Width, Height); Win32Declarations.FrameRect(hdc, ref rect, brush); Win32Declarations.SelectObject(hdc, oldBrush); Win32Declarations.DeleteObject(brush); Win32Declarations.ReleaseDC(Handle, hdc); m.Result = IntPtr.Zero; } else { base.WndProc(ref m); } }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (_fontHandle != IntPtr.Zero) { Win32Declarations.DeleteObject(_fontHandle); _fontHandle = IntPtr.Zero; } if (_borderPath != null) { _borderPath.Dispose(); } if (components != null) { components.Dispose(); } } base.Dispose(disposing); }
private void CreateVerticalFont() { if (_fontHandle != IntPtr.Zero) { Win32Declarations.DeleteObject(_fontHandle); _fontHandle = IntPtr.Zero; } int logPixY; using (Graphics g = CreateGraphics()) { IntPtr hdc = g.GetHdc(); logPixY = Win32Declarations.GetDeviceCaps(hdc, Win32Declarations.LOGPIXELSY); g.ReleaseHdc(hdc); } _fontHandle = Win32Declarations.CreateFont((int)(-Font.SizeInPoints * logPixY / 72), 0, _angle * 10, 0, Win32Declarations.FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, Font.Name); }
private static void DrawSingleIcon(RowState state, Graphics g, int index, Rectangle rcCol, int midPointX, int midPointY) { if (index >= Core.ResourceIconManager.ImageList.Images.Count) { // possible on shutdown (OM-8972) return; } RectangleF rcClip = g.ClipBounds; rcClip.Intersect(new RectangleF(rcCol.Left, rcCol.Top, rcCol.Width, rcCol.Height)); IntPtr hdc = g.GetHdc(); try { IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0); if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1) { Win32Declarations.DeleteObject(clipRgn); clipRgn = IntPtr.Zero; } Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom); int ildState = ((state & RowState.ActiveSelected) != 0) ? Win32Declarations.ILD_SELECTED : Win32Declarations.ILD_NORMAL; Win32Declarations.ImageList_Draw(Core.ResourceIconManager.ImageList.Handle, index, hdc, midPointX - 8, midPointY - 8, ildState); Win32Declarations.SelectClipRgn(hdc, clipRgn); Win32Declarations.DeleteObject(clipRgn); } finally { g.ReleaseHdc(hdc); } }
protected override void DrawItem(Graphics g, Rectangle rc, object item, RowState state, string highlightText) { IResource res = item as IResource; if (res != null && res.GetLinksTo(null, TasksPlugin._linkTarget).Count > 0) { int midPointX = rc.Left + Width / 2; int midPointY = (rc.Top + rc.Bottom) / 2; RectangleF rcClip = g.ClipBounds; rcClip.Intersect(new RectangleF(rc.Left, rc.Top, rc.Width, rc.Height)); IntPtr hdc = g.GetHdc(); try { IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0); if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1) { Win32Declarations.DeleteObject(clipRgn); clipRgn = IntPtr.Zero; } Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom); int ildState = ((state & RowState.ActiveSelected) != 0) ? Win32Declarations.ILD_SELECTED : Win32Declarations.ILD_NORMAL; Win32Declarations.ImageList_Draw(TasksPlugin._ImageList.Handle, 0, hdc, midPointX - 8, midPointY - 8, ildState); Win32Declarations.SelectClipRgn(hdc, clipRgn); Win32Declarations.DeleteObject(clipRgn); } finally { g.ReleaseHdc(hdc); } } }
public int DrawText(Graphics g, string text, Font font, Color color, Rectangle rc, StringFormat format) { int height; RectangleF rcClip = g.ClipBounds; RECT rect = new RECT(rc.Left, rc.Top, rc.Right, rc.Bottom); IntPtr hdc = g.GetHdc(); try { IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0); if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1) { Win32Declarations.DeleteObject(clipRgn); clipRgn = IntPtr.Zero; } Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom); IntPtr hFont = _fontCache.GetHFont(font); IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont); int textColor = Win32Declarations.ColorToRGB(color); int oldColor = Win32Declarations.SetTextColor(hdc, textColor); BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT); DrawTextFormatFlags flags = 0; if ((format.FormatFlags & StringFormatFlags.NoWrap) != 0) { flags |= DrawTextFormatFlags.DT_SINGLELINE; } else { flags |= DrawTextFormatFlags.DT_WORDBREAK; } if (format.Alignment == StringAlignment.Center) { flags |= DrawTextFormatFlags.DT_CENTER; } else if (format.Alignment == StringAlignment.Far) { flags |= DrawTextFormatFlags.DT_RIGHT; } if (format.LineAlignment == StringAlignment.Center) { flags |= DrawTextFormatFlags.DT_VCENTER; } if (format.Trimming == StringTrimming.EllipsisCharacter) { flags |= DrawTextFormatFlags.DT_END_ELLIPSIS; } if (format.HotkeyPrefix == HotkeyPrefix.None) { flags |= DrawTextFormatFlags.DT_NOPREFIX; } height = Win32Declarations.DrawText(hdc, text, text.Length, ref rect, flags); Win32Declarations.SelectClipRgn(hdc, clipRgn); Win32Declarations.DeleteObject(clipRgn); Win32Declarations.SetBkMode(hdc, oldMode); Win32Declarations.SetTextColor(hdc, oldColor); Win32Declarations.SelectObject(hdc, oldFont); } finally { g.ReleaseHdc(hdc); } return(height); }
public void Draw(TreeNode node, IntPtr hdc, Rectangle rect) { if (!IsHandled(node) || node.Bounds.IsEmpty || node.IsEditing) { return; } CustomTreeView.CustomTreeView treeView = (CustomTreeView.CustomTreeView)node.TreeView; int offset = CalculateOffset(node); RichText.RichText text = (RichText.RichText)myNodes[node]; Rectangle contentRect = CalculateContentRectangle(node, hdc, offset); rect.X += offset; rect.Width -= offset; Rectangle fullRect = new Rectangle(contentRect.X - 1, node.Bounds.Top, contentRect.Width + 5, node.Bounds.Height); //g.SetClip(fullRect); TreeNode dropHiliteNode = treeView.DraggingOver ? treeView.DropHighlightedNode : null; bool hasFocus = node.TreeView.Focused; bool drawSelected = treeView.DraggingOver ? (node == dropHiliteNode) : node.IsSelected && hasFocus; bool drawNonfocusedSelection = node.IsSelected && !node.TreeView.HideSelection && (!hasFocus || treeView.DraggingOver); Color backColor; if (drawSelected) { backColor = SystemColors.Highlight; } else if (drawNonfocusedSelection) { backColor = SystemColors.Control; } else { backColor = treeView.BackColor; } IntPtr hBrush = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(backColor)); RECT lineRect = new RECT(fullRect.Left - 1, node.Bounds.Top, fullRect.Right + 1, node.Bounds.Bottom); Win32Declarations.FillRect(hdc, ref lineRect, hBrush); Win32Declarations.DeleteObject(hBrush); if (drawSelected) { text = (RichText.RichText)text.Clone(); text.SetColors(SystemColors.HighlightText, SystemColors.Highlight); } else if (drawNonfocusedSelection) { text = (RichText.RichText)text.Clone(); text.SetColors(SystemColors.WindowText, SystemColors.Control); } text.Draw(hdc, contentRect); if (hasFocus && treeView.SelectedNode == node && treeView.NeedFocusRect() && dropHiliteNode == null) { RECT rc = new RECT(fullRect.Left - 1, fullRect.Top, fullRect.Right + 1, fullRect.Bottom); Win32Declarations.DrawFocusRect(hdc, ref rc); } }