/// <summary> /// 获取文字大小 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <returns>字体大小</returns> public SIZE TextSize(string text, FONT font) { SIZE size = new SIZE(); IntPtr hFont = CreateFont ( font.FontSize, 0, 0, 0, font.Bold ? FW_BOLD : FW_REGULAR, font.Italic ? 1 : 0, font.Underline ? 1 : 0, 0, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, font.FontFamily ); IntPtr hOldFont = SelectObject(m_hDC, hFont); GetTextExtentPoint32(m_hDC, text, text.Length, ref size); SelectObject(m_hDC, hOldFont); DeleteObject(hFont); return(size); }
/// <summary> /// 绘制文字 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <param name="color">颜色</param> /// <param name="rect">矩形区域</param> public void DrawText(string text, FONT font, double dwPenColor, RECT rect) { if (dwPenColor == COLOR.Empty()) { return; } if (dwPenColor < 0) { dwPenColor = Math.Abs(dwPenColor); } IntPtr hFont = CreateFont ( font.FontSize, 0, 0, 0, font.Bold ? FW_BOLD : FW_REGULAR, font.Italic ? 1 : 0, font.Underline ? 1 : 0, 0, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, font.FontFamily ); RECT newRect = new RECT(rect.left + m_offsetX, rect.top + m_offsetY, rect.right + m_offsetX, rect.bottom + m_offsetY); SetBkMode(m_hDC, TRANSPARENT); SetTextColor(m_hDC, (int)dwPenColor); IntPtr hOldFont = SelectObject(m_hDC, hFont); DrawText(m_hDC, text, -1, ref newRect, 0 | DT_NOPREFIX); SelectObject(m_hDC, hOldFont); DeleteObject(hFont); }
/// <summary> /// 绘制文字 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <param name="color">颜色</param> /// <param name="mp">坐标</param> public void DrawText(string text, FONT font, double dwPenColor, POINT mp) { if (dwPenColor == COLOR.Empty()) { return; } if (dwPenColor < 0) { dwPenColor = Math.Abs(dwPenColor); } IntPtr hFont = CreateFont ( font.FontSize, 0, 0, 0, font.Bold ? FW_BOLD : FW_REGULAR, 0, 0, 0, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, font.FontFamily ); SIZE size = TextSize(text, font); RECT newRect = new RECT(mp.x + m_offsetX, mp.y + m_offsetY, mp.x + m_offsetX + size.cx, mp.y + m_offsetY + size.cy); SetBkMode(m_hDC, TRANSPARENT); SetTextColor(m_hDC, (int)dwPenColor); IntPtr hOldFont = SelectObject(m_hDC, hFont); DrawText(m_hDC, text, -1, ref newRect, 0 | DT_NOPREFIX); SelectObject(m_hDC, hOldFont); DeleteObject(hFont); }
/// <summary> /// 获取文字大小 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <returns>字体大小</returns> public SIZE TextSize(string text, FONT font) { if (m_emptyStringFormat == null) { m_emptyStringFormat = StringFormat.GenericTypographic; } Size gdiPlusSize = m_g.MeasureString(text, MCommon.Instance.GetFont(font), Int32.MaxValue, m_emptyStringFormat).ToSize(); return(new SIZE(gdiPlusSize.Width, gdiPlusSize.Height)); }
/// <summary> /// 绘制文字 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <param name="color">颜色</param> /// <param name="mp">坐标</param> public void DrawText(string text, FONT font, double dwPenColor, POINT mp) { if (dwPenColor == COLOR.Empty()) { return; } if (m_emptyStringFormat == null) { m_emptyStringFormat = StringFormat.GenericTypographic; } m_g.DrawString(text, MCommon.Instance.GetFont(font), MCommon.Instance.GetBrush(dwPenColor), mp.x + m_offsetX, mp.y + m_offsetY, m_emptyStringFormat); }
/// <summary> /// 绘制文字 /// </summary> /// <param name="text">文字</param> /// <param name="font">字体</param> /// <param name="color">颜色</param> /// <param name="rect">矩形区域</param> public void DrawText(string text, FONT font, double dwPenColor, RECT rect) { if (dwPenColor == COLOR.Empty()) { return; } if (m_emptyStringFormat == null) { m_emptyStringFormat = StringFormat.GenericTypographic; } Rectangle gdiPlusRect = new Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top); m_g.DrawString(text, MCommon.Instance.GetFont(font), MCommon.Instance.GetBrush(dwPenColor), gdiPlusRect, m_emptyStringFormat); }
/// <summary> /// 获取Gdi字体 /// </summary> /// <param name="font">字体</param> /// <returns>Gdi字体</returns> public Font GetFont(FONT font) { string key = font.ToString(); if (!dy.ContainsKey(key)) { Font f; if (font.Bold && font.Underline && font.Italic) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Bold | FontStyle.Underline | FontStyle.Italic, GraphicsUnit.Pixel); } else if (font.Bold && font.Underline) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Bold | FontStyle.Underline, GraphicsUnit.Pixel); } else if (font.Bold && font.Italic) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Pixel); } else if (font.Underline && font.Italic) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Underline | FontStyle.Italic, GraphicsUnit.Pixel); } else if (font.Bold) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Bold, GraphicsUnit.Pixel); } else if (font.Underline) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Underline, GraphicsUnit.Pixel); } else if (font.Italic) { f = new Font(font.FontFamily, font.FontSize, FontStyle.Italic, GraphicsUnit.Pixel); } else { f = new Font(font.FontFamily, font.FontSize, GraphicsUnit.Pixel); } dy.Add(key, f); } return(dy[key]); }
/// <summary> /// 开始绘图 /// </summary> /// <param name="hdc">HDC</param> /// <param name="wRect">窗体区域</param> /// <param name="pRect">刷新区域</param> public void BeginPaint(Graphics g, RECT wRect, RECT pRect) { if (m_bitmap != null) { DeleteObject(m_hDC); m_bitmap.Dispose(); m_bitmap = null; m_hDC = IntPtr.Zero; m_g.Dispose(); m_g = null; } graphics = g; if (m_wndHdc == IntPtr.Zero) { m_wndHdc = graphics.GetHdc(); } m_rect = pRect; int imageW = wRect.right - wRect.left; int imageH = wRect.bottom - wRect.top; if (imageW == 0) { imageW = 1; } if (imageH == 0) { imageH = 1; } m_bitmap = new Bitmap(imageW, imageH); m_g = Graphics.FromImage(m_bitmap); m_hDC = m_g.GetHdc(); RECT rc = new RECT(-1, -1, 1, 1); FONT font = new FONT(); DrawText(" ", font, 0, rc); }