/// <summary> /// Creates a new Graphics object from the device context handle associated with the Graphics /// parameter /// </summary> /// <param name="oldGraphics">Graphics instance to obtain the parameter from</param> /// <returns>A new GDI+ drawing surface</returns> public static System.Drawing.Graphics CreateGraphics(System.Drawing.Graphics oldGraphics) { System.Drawing.Graphics createdGraphics; System.IntPtr hdc = oldGraphics.GetHdc(); createdGraphics = System.Drawing.Graphics.FromHdc(hdc); oldGraphics.ReleaseHdc(hdc); return createdGraphics; }
public static ABC GetCharWidthABC(char ch, Font font, System.Drawing.Graphics gr) { ABC[] _temp = new ABC[1]; IntPtr hDC = gr.GetHdc(); Font ft = (Font)font.Clone(); IntPtr hFt = ft.ToHfont(); SelectObject(hDC, hFt); GetCharABCWidthsW(hDC, ch, ch, _temp); DeleteObject(hFt); gr.ReleaseHdc(); return _temp[0]; }
/// <summary> /// Draws the part defined by the current renderer onto a specified <see cref="Graphics"/> surface, within the /// given clipping bounds. /// </summary> /// <param name="graphics">The <see cref="Graphics"/> surface on which the part is drawn.</param> /// <param name="state">A constant which determines the current state of the part.</param> /// <param name="bounds">The bounding rectangle in which the part is drawn.</param> /// <param name="clipBounds">The bounding rectangle in which the part is clipped.</param> public void Draw(System.Drawing.Graphics graphics, int state, Rectangle bounds, Rectangle clipBounds) { if (Supported) { IntPtr hdc = IntPtr.Zero; try { DrawCore(hdc = graphics.GetHdc(), state, bounds, clipBounds); } finally { if (hdc != IntPtr.Zero) graphics.ReleaseHdc(hdc); } } }
private void DrawBorder(System.Drawing.Graphics g) { IntPtr hTheme = UxTheme.OpenThemeData(this.Handle, "EDIT"); // Makes sure Windows XP is running and // a .manifest file exists for the EXE. if (Environment.OSVersion.Version.Major >= 5 && Environment.OSVersion.Version.Minor > 0 && System.IO.File.Exists(Application.ExecutablePath + ".manifest") && (hTheme != IntPtr.Zero)) { //Get DC IntPtr hDC = g.GetHdc(); int state = (int) UxTheme.ETS_NORMAL; switch (this.m_state) { case State.Disabled : state = (int) UxTheme.ETS_DISABLED; break; default: break; } try { RECT r = new RECT(this.ClientRectangle.Left, this.ClientRectangle.Right, this.ClientRectangle.Top, this.ClientRectangle.Bottom); //Render button IntPtr hr = UxTheme.DrawThemeBackground( hTheme, hDC, UxTheme.EP_EDITTEXT, state, r, null); } finally { //Release DC g.ReleaseHdc(hDC); UxTheme.CloseThemeData(hTheme); } } else { using (Graphics y = this.CreateGraphics()) { ControlPaint.DrawBorder(y, ClientRectangle, SystemColors.Control, ButtonBorderStyle.Inset); } } }
// Queries APC spacing for the specified character. static float? GetCharacterWidth(char character, Font font, System.Drawing.Graphics graphics) { // Look up the native device context and font handles. IntPtr hdc = graphics.GetHdc(); try { IntPtr hFont = font.ToHfont(); try { // Select our font into the DC. IntPtr oldFont = NativeMethods.SelectObject(hdc, hFont); try { // Query the character spacing. var result = new NativeMethods.ABCFloat[1]; if (NativeMethods.GetCharABCWidthsFloatW(hdc, character, character, result)) { return result[0].A + result[0].B + result[0].C; } else { return null; } } finally { NativeMethods.SelectObject(hdc, oldFont); } } finally { NativeMethods.DeleteObject(hFont); } } finally { graphics.ReleaseHdc(hdc); } }
public static void DrawText(System.Drawing.Graphics graphics, string text, System.Drawing.Font font, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Drawing.Rectangle bounds, Skybound.Windows.Forms.TextFormatFlags formatFlags) { if ((text == null) || (text.Length == 0) || (graphics == null) || (font == null) || bounds.Size == System.Drawing.Size.Empty || foreColor.Equals(System.Drawing.Color.Empty) || foreColor.Equals(System.Drawing.Color.Transparent)) return; System.IntPtr intPtr1 = graphics.GetHdc(); try { System.IntPtr intPtr2 = Skybound.Windows.Forms.TextRenderer.GetCachedFont(font); System.IntPtr intPtr3 = Skybound.Windows.Forms.TextRenderer.SelectObject(intPtr1, intPtr2); if (backColor.Equals(System.Drawing.Color.Empty) || backColor.Equals(System.Drawing.Color.Transparent)) { Skybound.Windows.Forms.TextRenderer.SetBkMode(intPtr1, 1); } else { Skybound.Windows.Forms.TextRenderer.SetBkMode(intPtr1, 2); Skybound.Windows.Forms.TextRenderer.SetBkColor(intPtr1, System.Drawing.ColorTranslator.ToWin32(backColor)); } int i = Skybound.Windows.Forms.TextRenderer.GetTextColor(intPtr1); Skybound.Windows.Forms.TextRenderer.SetTextColor(intPtr1, System.Drawing.ColorTranslator.ToWin32(foreColor)); Skybound.Windows.Forms.TextRenderer.RECT rect = new Skybound.Windows.Forms.TextRenderer.RECT(bounds); Skybound.Windows.Forms.TextRenderer.DrawText(intPtr1, text, text.Length, ref rect, (int)formatFlags); Skybound.Windows.Forms.TextRenderer.SetTextColor(intPtr1, i); Skybound.Windows.Forms.TextRenderer.SelectObject(intPtr1, intPtr3); } finally { graphics.ReleaseHdc(intPtr1); } }
public static bool GetStringPath( System.Drawing.Graphics pGraphics, System.Drawing.Drawing2D.GraphicsPath ppPath, string pszText, LOGFONT plf, System.Drawing.Rectangle rtDraw) { IntPtr hDC = pGraphics.GetHdc(); int nPrevMode = SetBkMode(hDC, TRANSPARENT); // create and select it IntPtr hFont = CreateFontIndirect(plf); if (null == hFont) return false; IntPtr hOldFont = (IntPtr)SelectObject(hDC, hFont); RECT rect = new RECT(rtDraw); // use a path to record how the text was drawn const uint DT_CENTER = 0x00000001; BeginPath(hDC); DrawText(hDC, pszText, pszText.Length, ref rect, DT_CENTER); EndPath(hDC); // Find out how many points are in the path. Note that // for long strings or complex fonts, this number might be // gigantic! int nNumPts = GetPath(hDC, null, null, 0); if (nNumPts == 0) return false; // Allocate memory to hold points and stroke types from // the path. POINT[] lpPoints = new POINT[nNumPts]; byte[] lpTypes = new byte[nNumPts]; // Now that we have the memory, really get the path data. nNumPts = GetPath(hDC, lpPoints, lpTypes, nNumPts); // If it worked, draw the lines. Win95 and Win98 don't support // the PolyDraw API, so we use our own member function to do // similar work. If you're targeting only Windows NT, you can // use the PolyDraw() API and avoid the COutlineView::PolyDraw() // member function. if (nNumPts != -1) PolyDraw(ppPath, lpPoints, lpTypes, nNumPts); // Put back the old font SelectObject(hDC, hOldFont); DeleteObject(hFont); SetBkMode(hDC, nPrevMode); pGraphics.ReleaseHdc(hDC); return true; }