Tuesday, 20 August 2013

How to draw Text on Mouse Move in VB.NET or C# or Windows Forms | MouseMove Event in VB.NET or C#

Hi friends, in this article I will explain about how to draw Text on Mouse Move in VB.NET or C# or Windows Forms or MouseMove Event in VB.NET or C#.
In previous article i explained about How can I set a form to have a 100% transparent background. and explained some articles on Windows Forms
Take one new project (file---new---project---give name as Form2.vb)
Double click on the form and select the (Form2 events) as shown in the below figure.

And in declarations select MouseMove event as shown in the below figure.

And write the below code.
In VB.NET:
Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub Form2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim g As Graphics
        Dim i As Integer
        g = Me.CreateGraphics()
        Dim f As New Font("Georgia", 30)
        Dim p As New Pen(New SolidBrush(Color.Red), 1)
        If i Mod 50 = 0 Then
            g.DrawString("Aspdotnet-Roja", f, _
            New SolidBrush(Color.DarkOrchid), _
            New RectangleF(New Point(e.X, e.Y), _
            New SizeF(600, 300)))
        End If
        i = i + 1
        Me.Text = "Mouse Position :-" & e.X & "," & e.Y
        f.Dispose()
        g.Dispose()
    End Sub
End Class

In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
Public Class Form2
{
                private void Form2_Load(System.Object sender, System.EventArgs e)
                {
                }
                private void Form2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                                Graphics g = default(Graphics);
                                int i = 0;
                                g = this.CreateGraphics();
                                Font f = new Font("Georgia", 30);
                                Pen p = new Pen(new SolidBrush(Color.Red), 1);
                                if (i % 50 == 0) {
                                                g.DrawString("Aspdotnet-Roja", f, new SolidBrush(Color.DarkOrchid),              new RectangleF(new Point(e.X, e.Y), new SizeF(600, 300)));
                                }
                                i = i + 1;
                                this.Text = "Mouse Position :-" + e.X + "," + e.Y;
                                f.Dispose();
                                g.Dispose();
                }
    Public Form2()
                {
                                MouseMove += Form2_MouseMove;
                                Load += Form2_Load;
                }

}



The output is as shown in the below figure.
 I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.