-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: shashijeevan
Message 2 in Discussion

Hi Nilesh,   Here is the sample code to Draw given text using GDI and PInvoke in C#.   
I have used TextOut function to draw the text. If you want to draw formatted text you 
have to use DrawText or DrawTextEx functions using the same approach.   Specify the 
text in the textbox and click on the button to draw it.   regards, shashi   //Code  
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Runtime.InteropServices; 
namespace GDIForm 
{ 
/// <summary> 
/// Summary description for Form1. 
/// </summary> 
public class Form1 : System.Windows.Forms.Form 
{ 
private System.Windows.Forms.TextBox textBox1; 
private System.Windows.Forms.Button button1; 
private System.Windows.Forms.Panel panel1; 
/// <summary> 
/// Required designer variable. 
/// </summary> 
private System.ComponentModel.Container components = null; 
//This method draw text at the specified location 
[DllImport("gdi32.dll")] 
public static extern bool TextOut( 
IntPtr hdc, // handle to DC 
int nXStart, // x-coordinate of starting position 
int nYStart, // y-coordinate of starting position 
string lpString, // character string 
int cbString // number of characters 
); 
public Form1() 
{ 
// 
// Required for Windows Form Designer support 
// 
InitializeComponent(); 
// 
// TODO: Add any constructor code after InitializeComponent call 
// 
} 
/// <summary> 
/// Clean up any resources being used. 
/// </summary> 
protected override void Dispose( bool disposing ) 
{ 
if( disposing ) 
{ 
if (components != null)  
{ 
components.Dispose(); 
} 
} 
base.Dispose( disposing ); 
} 
#region Windows Form Designer generated code 
/// <summary> 
/// Required method for Designer support - do not modify 
/// the contents of this method with the code editor. 
/// </summary> 
private void InitializeComponent() 
{ 
this.textBox1 = new System.Windows.Forms.TextBox(); 
this.button1 = new System.Windows.Forms.Button(); 
this.panel1 = new System.Windows.Forms.Panel(); 
this.SuspendLayout(); 
//  
// textBox1 
//  
this.textBox1.Location = new System.Drawing.Point(32, 48); 
this.textBox1.Name = "textBox1"; 
this.textBox1.TabIndex = 0; 
this.textBox1.Text = ""; 
//  
// button1 
//  
this.button1.Location = new System.Drawing.Point(184, 48); 
this.button1.Name = "button1"; 
this.button1.TabIndex = 1; 
this.button1.Text = "DrawText"; 
this.button1.Click += new System.EventHandler(this.button1_Click); 
//  
// panel1 
//  
this.panel1.Location = new System.Drawing.Point(32, 120); 
this.panel1.Name = "panel1"; 
this.panel1.Size = new System.Drawing.Size(232, 120); 
this.panel1.TabIndex = 2; 
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); 
//  
// Form1 
//  
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
this.ClientSize = new System.Drawing.Size(292, 273); 
this.Controls.AddRange(new System.Windows.Forms.Control[] { 
this.panel1, 
this.button1, 
this.textBox1}); 
this.Name = "Form1"; 
this.Text = "Form1"; 
this.ResumeLayout(false); 
} 
#endregion 
/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 
static void Main()  
{ 
Application.Run(new Form1()); 
} 
private void button1_Click(object sender, System.EventArgs e) 
{ 
panel1.Invalidate(); 
} 
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{ 
IntPtr hdc = new IntPtr(); 
hdc = e.Graphics.GetHdc(); 
 
string text = textBox1.Text; 
//Draws text a location 10,10 
TextOut(hdc, 10, 10, text, text.Length); 
//Don't forget to release the HDC 
e.Graphics.ReleaseHdc(hdc); 
} 
} 
} 
//End of Code

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to