Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

A .NET Wizard control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (89 votes)
24 Apr 2003CPOL7 min read 660.6K   8.7K   216  
A .NET Wizard control for the VS.IDE and client apps
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

using UtilityLibrary.Win32;


namespace UtilityLibrary.General
{
  /// <summary>
  /// Summary description for TextUtil.
  /// </summary>
  public class TextUtil
  {
    #region Class private constants
    private static int DEF_TEXTFORMAT = ( int )DrawTextFormatFlags.DT_SINGLELINE | 
      ( int )DrawTextFormatFlags.DT_LEFT | 
      ( int )DrawTextFormatFlags.DT_CALCRECT;
    #endregion

    #region Constructor
    /// <summary>
    /// To prevent creaion of class instance we declare default constructor as 
    /// a private method
    /// </summary>
    private TextUtil()
    {
    }
    #endregion

    #region Methods
    static private IntPtr GetGraphicsHDC( Graphics g )
    {
      IntPtr hdc = ( g != null ) ? g.GetHdc() : WindowsAPI.GetDC( IntPtr.Zero );
      return hdc;
    }

    static private void   ReleaseGraphicHDC( Graphics g, IntPtr hdc )
    {
      if( g != null )
      {
        g.ReleaseHdc( hdc );
      }
      else
      {
        WindowsAPI.ReleaseDC( IntPtr.Zero, hdc );
      }
    }


    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;
    }

    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 );
      
      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, hOldFont );
      WindowsAPI.DeleteObject( hFont );

      ReleaseGraphicHDC( g, hdc );
              
      return ( Size )rect;
    }

    
    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);
    }

    public static void DrawReverseString( Graphics g, string drawText, Font drawFont, Rectangle drawRect, Brush drawBrush, StringFormat drawFormat )
    {
      GraphicsContainer container = g.BeginContainer();

      // The text will be rotated around the origin (0,0) and so needs moving
      // back into position by using a transform
      g.TranslateTransform(drawRect.Left * 2 + drawRect.Width, 
        drawRect.Top * 2 + drawRect.Height);

      // Rotate the text by 180 degress to reverse the direction 
      g.RotateTransform(180);

      // Draw the string as normal and let then transforms do the work
      g.DrawString(drawText, drawFont, drawBrush, drawRect, drawFormat);

      g.EndContainer(container);
    }


    public static Size MeasureText( string text, Font fnt, int width )
    {
      Bitmap bmp = new Bitmap( 1, 1 );
      Graphics g = Graphics.FromImage( bmp );

      StringFormat format = new StringFormat();
      format.Alignment      = StringAlignment.Near;
      format.LineAlignment  = StringAlignment.Near;
      format.Trimming       = StringTrimming.None;
      format.FormatFlags    = StringFormatFlags.LineLimit;
      format.HotkeyPrefix   = HotkeyPrefix.Hide;

      SizeF size = g.MeasureString( text, fnt, width, format );

      g.Dispose();
      bmp.Dispose();

      return new Size( (int)( size.Width + 0.5 ), (int)( size.Height + 0.5 ) );
    }
    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions