Hit Testing
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace HitTesting
{
/// <summary>
/// Summary description for HitTesting.
/// </summary>
public class HitTesting : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
GraphicsPath gP;
string mes = "Move to the big I!";
FontFamily fF = new FontFamily("Times new roman");
public HitTesting()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
CreateFont();
this.Text = "Hit Testing";
this.label1.Font = new Font(fF, 12);
//
// 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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(160, 23);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// HitTesting
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 109);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "HitTesting";
this.Text = "HitTesting";
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.HitTesting_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new HitTesting());
}
void CreateFont()
{
Graphics g = this.CreateGraphics();
label1.Text = mes;
string s = "I";
int fSt = (int)FontStyle.Regular;
Point xy = new Point(50, 10);
StringFormat sFr = StringFormat.GenericDefault;
gP = new GraphicsPath(); // gp is a class member
gP.AddString(s, fF, fSt, 50, xy, sFr); // add the string to the path
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawPath(Pens.Black, gP); // draw the path to the surface
g.Dispose();
}
private void HitTesting_MouseMove(object sender, MouseEventArgs e)
{
Region reg = new Region(gP);
if(reg.IsVisible(new Point(e.X, e.Y)))
mes = "You touched me ...";
else
mes = "Move to the big I!";
CreateFont();
}
}
}
Related examples in the same category