Beispiel #1
0
        public static Size GetTextSize(Graphics graphics, string text, Font font)
        {
            if (text == null)
            {
                return(new Size(0, 0));
            }

            IntPtr hdc               = graphics.GetHdc();
            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);

            Win32.RECT rect = new Win32.RECT();
            rect.left   = 0;
            rect.right  = 0;
            rect.top    = 0;
            rect.bottom = 0;

            WindowsAPI.DrawText(hdc, text, text.Length, ref rect,
                                (int)(DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_LEFT | DrawTextFormatFlags.DT_CALCRECT));
            WindowsAPI.SelectObject(hdc, currentFontHandle);
            WindowsAPI.DeleteObject(fontHandle);
            graphics.ReleaseHdc(hdc);

            return(new Size(rect.right - rect.left, rect.bottom - rect.top));
        }
        public static Size GetTextSize(Graphics g, string text, Font font)
        {
            IntPtr hdc = GetGraphicsHDC(g);

            IntPtr hFont    = font.ToHfont();
            IntPtr hOldFont = WindowsAPI.SelectObject(hdc, hFont);

            RECT rect = new RECT();

            WindowsAPI.DrawText(hdc, text, text.Length, ref rect, DEF_TEXTFORMAT);
            WindowsAPI.SelectObject(hdc, hOldFont);
            WindowsAPI.DeleteObject(hFont);

            ReleaseGraphicHDC(g, hdc);

            return(( Size )rect);
        }
Beispiel #3
0
        public static Size GetTextSize(Graphics graphics, string text, Font font)
        {
#if !__MonoCS__
            IntPtr hdc = IntPtr.Zero;
            if (graphics != null)
            {
                // Get device context from the graphics passed in
                hdc = graphics.GetHdc();
            }
            else
            {
                // Get screen device context
                hdc = WindowsAPI.GetDC(IntPtr.Zero);
            }

            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);

            Win32.RECT rect = new Win32.RECT();
            rect.left   = 0;
            rect.right  = 0;
            rect.top    = 0;
            rect.bottom = 0;

            WindowsAPI.DrawText(hdc, text, text.Length, ref rect,
                                (int)(DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_LEFT | DrawTextFormatFlags.DT_CALCRECT));
            WindowsAPI.SelectObject(hdc, currentFontHandle);
            WindowsAPI.DeleteObject(fontHandle);

            if (graphics != null)
            {
                graphics.ReleaseHdc(hdc);
            }
            else
            {
                WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);
            }

            return(new Size(rect.right - rect.left, rect.bottom - rect.top));
#else
            // TODO-Linux: Implement this using a portable method.
            return(new Size(10, 10));
#endif
        }
        public static Size GetTextSize(Graphics g, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags)
        {
            IntPtr hdc      = GetGraphicsHDC(g);
            IntPtr hFont    = font.ToHfont();
            IntPtr hOldFont = WindowsAPI.SelectObject(hdc, hFont);

            NWin32.RECT rect = new NWin32.RECT();
            rect.left   = rc.Left;
            rect.right  = rc.Right;
            rect.top    = rc.Top;
            rect.bottom = rc.Bottom;

            WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags);
            WindowsAPI.SelectObject(hdc, hOldFont);
            WindowsAPI.DeleteObject(hFont);

            ReleaseGraphicHDC(g, hdc);

            return(( Size )rect);
        }
Beispiel #5
0
        public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags)
        {
            IntPtr hdc = IntPtr.Zero;

            if (graphics != null)
            {
                // Get device context from the graphics passed in
                hdc = graphics.GetHdc();
            }
            else
            {
                // Get screen device context
                hdc = WindowsAPI.GetDC(IntPtr.Zero);
            }

            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);

            Win32.RECT rect = new Win32.RECT();
            rect.left   = rc.Left;
            rect.right  = rc.Right;
            rect.top    = rc.Top;
            rect.bottom = rc.Bottom;

            WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags);
            WindowsAPI.SelectObject(hdc, currentFontHandle);
            WindowsAPI.DeleteObject(fontHandle);

            if (graphics != null)
            {
                graphics.ReleaseHdc(hdc);
            }
            else
            {
                WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);
            }

            return(new Size(rect.right - rect.left, rect.bottom - rect.top));
        }
Beispiel #6
0
        public static void DrawText(Graphics graphics, string text, Font font, Rectangle rect)
        {
            IntPtr hdc               = graphics.GetHdc();
            IntPtr fontHandle        = font.ToHfont();
            IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);

            WindowsAPI.SetBkMode(hdc, BackgroundMode.TRANSPARENT);

            RECT rc = new RECT();

            rc.left   = rect.Left;
            rc.top    = rect.Top;
            rc.right  = rc.left + rect.Width;
            rc.bottom = rc.top + rect.Height;

            WindowsAPI.DrawText(hdc, text, text.Length, ref rc,
                                (int)(DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_LEFT
                                      | DrawTextFormatFlags.DT_MODIFYSTRING | DrawTextFormatFlags.DT_WORD_ELLIPSIS));
            WindowsAPI.SelectObject(hdc, currentFontHandle);
            WindowsAPI.DeleteObject(fontHandle);
            graphics.ReleaseHdc(hdc);
        }