Click to See Complete Forum and Search --> : How to set the text 'horizontal alignment' in TextBox?


xred
March 21st, 2006, 07:06 PM
Hi, I want to set the text 'vertical alignment and horizontal alignment' in TextBox,
but I can't find 'horizontal alignment', so I write some control by myself,
however, I find it not easy to draw the text on the screen! who could give me some
samples? What should I do to draw this control?

The below is my code, it's not hard to understand, if you have some idea, please reply....
Thanks....


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Runtime.InteropServices;


namespace XQJ
{
public class NumberEdit : System.Windows.Forms.TextBox
{
// GDI---------------------------------START
public struct Rect
{
public int Left, Top, Right, Bottom;
public Rect( Rectangle r )
{
this.Left = r.Left;
this.Top = r.Top;
this.Bottom = r.Bottom;
this.Right = r.Right;
}
}

public const int DT_LEFT = 0x00000000;
public const int DT_CENTER = 0x00000001;
public const int DT_RIGHT = 0x00000002;
public const int DT_VCENTER = 0x00000004;
public const int DT_BOTTOM = 0x00000008;
public const int DT_WORDBREAK = 0x00000010;
public const int DT_SINGLELINE = 0x00000020;


[DllImport("User32.dll", CharSet=CharSet.Unicode)]
public static extern int DrawText( IntPtr hDC, string lpStr , int nCount, ref Rect lpRect, long uFormat );

[DllImport("gdi32.dll")]
public static extern int SetTextColor( IntPtr hDC, int crColor );

[DllImport("gdi32.dll")]
public static extern int SetBkColor( IntPtr hDC, int crColor );
// GDI----------------------------------END

private System.ComponentModel.Container components = null;
private Color colorBorder = Color.DarkBlue;
private Color colorBackground = Color.Red;
private Color colorText = Color.Black;
private int iBorderWidth = 1;
private Font fontText = new Font("Tahoma", 10);
private Rectangle rectText;

protected static int WM_PAINT = 0x000F;

public NumberEdit()
{
this.SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true );
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{

DrawBackground();
DrawString( this.Text );
}

protected override void OnTextChanged( EventArgs e )
{
DrawString( this.Text );
}


private void DrawBorder()
{
Graphics dc = this.CreateGraphics();
Pen pen = new Pen( colorBorder, iBorderWidth );

Rectangle client = new Rectangle( this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width-1, this.ClientRectangle.Height-1 );

dc.FillRectangle( new SolidBrush( Color.White ), client );
dc.DrawRectangle( pen, client );
}

private void DrawBackground()
{
Graphics dc = this.CreateGraphics();
dc.FillRectangle( new SolidBrush( colorBackground ), this.ClientRectangle );
}

private void DrawString( string str )
{
Graphics dc = this.CreateGraphics();
IntPtr hdc = dc.GetHdc();
SetTextColor( hdc, ColorTranslator.ToWin32( Color.White ) );
SetBkColor( hdc, ColorTranslator.ToWin32( Color.Blue ) );

Rectangle rect;
rect = new Rectangle( this.ClientRectangle.Left, (int)((this.ClientRectangle.Height - fontText.GetHeight())/2),
this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1 );

Rect _rect = new Rect( rect );

int flags = DT_CENTER | DT_VCENTER | DT_BOTTOM;
DrawText( hdc, str, str.Length, ref _rect, flags );
dc.ReleaseHdc( hdc );
}

// WndProc
protected override void WndProc( ref System.Windows.Forms.Message m )
{
if ( m.Msg == WM_PAINT )
{
Graphics dc = Graphics.FromHwnd( this.Handle );
PaintEventArgs pe = new PaintEventArgs( dc, new Rectangle(0, 0, this.Width, this.Height) );
OnPaint( pe );
}

base.WndProc( ref m );
}
}
}

Athley
March 22nd, 2006, 05:07 PM
Couldn't you use the built in DrawString method?
public class MyTextBox : TextBox
{
public MyTextBox()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, new PointF(0, 0));
base.OnPaint(pe);
}
}/Leyan

xred
March 22nd, 2006, 07:18 PM
Hi, Athley, the reason is that I want to draw some string 'vertical alignment' but
if I use built in DrawString, I can't calculate the string length if there are some unicodes
and ansi codes, do you have some ideas? :-)