示例#1
0
文件: Gdi.cs 项目: CecleCW/ProductMan
 public static extern int SetWorldTransform( IntPtr hdc, ref XFORM lpXform );
示例#2
0
 public static extern int GetWorldTransform(IntPtr hdc, ref XFORM lpXform);
示例#3
0
        public static void DrawString( Graphics g, string text, Font font, Color color, Rectangle rect, Alignment alignment, VAlignment valignment, TextSplitting textSplitting, Ampersands ampersands )
        {
            if( g == null )
            {
                throw new ArgumentNullException( "g" );
            }
            if( text == null )
            {
                throw new ArgumentNullException( "text" );
            }
            if( font == null )
            {
                throw new ArgumentNullException( "font" );
            }

            if( ampersands == Ampersands.Display )
            {
                text = text.Replace( "&", "&&" );
            }

            float[] txValues = g.Transform.Elements;
            IntPtr hClipRgn = g.Clip.GetHrgn( g );
            IntPtr hDC = g.GetHdc();

            Gdi.SelectClipRgn( hDC, hClipRgn );

            int oldGraphicsMode = Gdi.SetGraphicsMode( hDC, 2 );
            XFORM oldXForm = new XFORM();

            Gdi.GetWorldTransform( hDC, ref oldXForm );

            XFORM newXForm = new XFORM();

            newXForm.eM11 = txValues[0];
            newXForm.eM12 = txValues[1];
            newXForm.eM21 = txValues[2];
            newXForm.eM22 = txValues[3];
            newXForm.eDx = txValues[4];
            newXForm.eDy = txValues[5];

            Gdi.SetWorldTransform( hDC, ref newXForm );

            try
            {
                IntPtr hFont = font.ToHfont();
                IntPtr hOldFont = Gdi.SelectObject( hDC, hFont );

                try
                {
                    BinaryComponents.Utility.Win32.Common.RECT r = new BinaryComponents.Utility.Win32.Common.RECT( rect );
                    User.DrawTextFlags uFormat;

                    switch( textSplitting )
                    {
                        case TextSplitting.SingleLineEllipsis:
                            uFormat
                                    = User.DrawTextFlags.DT_WORD_ELLIPSIS
                                        | User.DrawTextFlags.DT_END_ELLIPSIS;
                            break;
                        case TextSplitting.MultiLine:
                            uFormat
                                    = User.DrawTextFlags.DT_WORDBREAK;
                            break;
                        default:
                            throw new InvalidOperationException();
                    }

                    switch( alignment )
                    {
                        case Alignment.Left:
                            break;
                        case Alignment.Center:
                            uFormat
                                    = User.DrawTextFlags.DT_CENTER;
                            break;
                        case Alignment.Right:
                            uFormat
                                    = User.DrawTextFlags.DT_RIGHT;
                            break;
                        default:
                            throw new InvalidOperationException();
                    }
                    switch( valignment )
                    {
                        case VAlignment.Top:
                            break;
                        case VAlignment.Bottom:
                            uFormat |= User.DrawTextFlags.DT_BOTTOM | User.DrawTextFlags.DT_SINGLELINE;
                            break;
                        case VAlignment.Center:
                            uFormat |= User.DrawTextFlags.DT_VCENTER | User.DrawTextFlags.DT_SINGLELINE;
                            break;
                    }

                    uint bgr = (uint)((color.B << 16) | (color.G << 8) | (color.R));
                    uint oldColor = Gdi.SetTextColor( hDC, bgr );

                    try
                    {
                        BackgroundMode oldBackgroundMode = Gdi.SetBkMode( hDC, BackgroundMode.TRANSPARENT );

                        try
                        {
                            User.DrawText( hDC, text, text.Length, ref r, uFormat );
                        }
                        finally
                        {
                            Gdi.SetBkMode( hDC, oldBackgroundMode );
                        }
                    }
                    finally
                    {
                        Gdi.SetTextColor( hDC, oldColor );
                    }
                }
                finally
                {
                    Gdi.SelectObject( hDC, hOldFont );
                    Gdi.DeleteObject( hFont );
                }
            }
            finally
            {
                if( oldGraphicsMode == 1 )
                {
                    oldXForm.eM11 = 1;
                    oldXForm.eM12 = 0;
                    oldXForm.eM21 = 0;
                    oldXForm.eM22 = 1;
                    oldXForm.eDx = 0;
                    oldXForm.eDx = 0;
                }

                Gdi.SetWorldTransform( hDC, ref oldXForm );
                Gdi.SetGraphicsMode( hDC, oldGraphicsMode );

                g.ReleaseHdc( hDC );

                if( hClipRgn != IntPtr.Zero )
                {
                    g.Clip.ReleaseHrgn( hClipRgn );
                }
            }
        }