Hit Testing : Mouse Event « Event « C# / C Sharp






Hit Testing

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

1.Which mouse button was clicked?
2.Mouse left click, right click, middle click, hoverMouse left click, right click, middle click, hover
3.Register Form Mouse Move, down and up actionRegister Form Mouse Move, down and up action
4.Block Mouse
5.Mouse Enter actionMouse Enter action
6.Mouse Exit actionMouse Exit action
7.Mouse click actionMouse click action
8.Mouse Hover actionMouse Hover action
9.Mouse Down actionMouse Down action
10.Mouse up actionMouse up action
11.Mouse Double Click eventMouse Double Click event
12.Handle Mouse click event on an ImageHandle Mouse click event on an Image
13.Catch Mouse Click event inside a strange shapeCatch Mouse Click event inside a strange shape
14.Mouse MovementMouse Movement
15.Mouse Buttons ClickMouse Buttons Click
16.Mouse moveMouse move
17.Full screen and KeyEvent and MouseEvent
18.Mouse HandlerMouse Handler