tags:

views:

45

answers:

2

Hello ! I have a winform app and when i click on a button, i would like to have a string text move after my cursor. I've written some code but it doesn't seem to work . please Help !

Here is the code :

 private void corectionBrushToolStripMenuItem_Click(object sender, EventArgs e)
    {

        this.MouseMove += new MouseEventHandler(On_MouseMove);
        this.Paint += new PaintEventHandler(DrawRect); 
    }

    private void DrawRect(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawString("da",Font,Brushes.Black,new Point(mouseMoveX,mouseMoveY));

    }

    public void On_MouseMove(object sender, MouseEventArgs mouseEv)
    {
        mouseMoveX = mouseEv.X;
        mouseMoveY = mouseEv.Y;

        this.Invalidate();


    }

Regards, Alex Badescu

A: 

How about:

Add the desired text to a label control, and change the position in MouseMove.

Following works fine:

int mX = 0;
int mY = 0;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    mX = e.X;
    mY = e.Y;
    this.Invalidate();
}

Random rr = new Random(123123);

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.DrawString("This a test"+rr.Next().ToString(), Font, Brushes.Black, new Point(mX, mY));
}
KMan
well i can't really do that , because i actually want to draw a rectangle at the cursor position. I inserted the text for starters.
Badescu Alexandru
@Badescu Alexandru: Please see my edit in response to your comment.
KMan
thanks! but i have another question : i have an image on the background that also is called in paint to be drawn, and when i move the cursor, the image gets flashy because it is redrawn every time. How can i avoid this? this is why i wanted a particular paintEventHandler, so that i would only call the DrawString method
Badescu Alexandru
@Badescu Alexandru: I would add a bool and check in the Paint method, to know what needs to be drawn. For instance, `if(bDrawImage) DrawYourImage();` and `if (bDrawText) DrawYourText();` I hope you get the idea.
KMan
A: 

This is an example that does what you want I think. the offset in the mouse move is so that it shows up not under the mouse.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool DrawText = false;

        private void button1_Click(object sender, EventArgs e)
        {
            DrawText = !DrawText;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            if(DrawText)
            {
                if (lp != p)
                {
                    this.Invalidate();
                }

                e.Graphics.DrawString("hi", SystemFonts.DefaultFont, Brushes.Green, p);
                lp = p; 
            }

        }

        private PointF p;
        private PointF lp;
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            p = new PointF(e.X -10, e.Y);
            this.Invalidate();
        }
    }
}
rerun
the program works just fine when i put the Form1_Paint content in the "onPaint" override function , but i need a custom paint function to call..
Badescu Alexandru
do you have any ideas how to do that ?
Badescu Alexandru
You would still have to call that function from the onpait
rerun