Exemplo n.º 1
0
        public void DrawText(string text, Point point, Font font, Color foreColor)
        {
            IntPtr fontHandle    = font.ToHfont();
            IntPtr oldFontHandle = NativeMethods.SelectObject(this.graphicsHandle, fontHandle);

            int oldBkMode    = NativeMethods.SetBkMode(this.graphicsHandle, NativeMethods.TRANSPARENT);
            int oldTextColor = NativeMethods.SetTextColor(this.graphicsHandle, Color.FromArgb(0, foreColor.R, foreColor.G, foreColor.B).ToArgb());

            Size size = this.MeassureTextInternal(text);

            NativeMethods.RECT clip = new NativeMethods.RECT();
            clip.left   = point.X;
            clip.top    = point.Y;
            clip.right  = clip.left + size.Width;
            clip.bottom = clip.top + size.Height;

            // ExtTextOut does not show Mnemonics highlighting.
            NativeMethods.DrawText(this.graphicsHandle, text, text.Length, ref clip, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT);

            NativeMethods.SetTextColor(this.graphicsHandle, oldTextColor);
            NativeMethods.SetBkMode(this.graphicsHandle, oldBkMode);

            NativeMethods.SelectObject(this.graphicsHandle, oldFontHandle);
            NativeMethods.DeleteObject(fontHandle);
        }
Exemplo n.º 2
0
        private Size MeassureTextInternal(string text)
        {
            NativeMethods.RECT rect = new NativeMethods.RECT();
            rect.left   = 0;
            rect.right  = 0;
            rect.top    = 0;
            rect.bottom = 0;

            // ExtTextOut does not show Mnemonics highlighting.
            NativeMethods.DrawText(this.graphicsHandle, text, text.Length, ref rect, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT | NativeMethods.DT_CALCRECT);

            return(new Size(rect.right, rect.bottom));
        }