mouse coordinates in editbox

Hi all,

I'm new to this forum, I have searched through several sections already in the forum before I registered to find an answer. I can't find the answer on my question. Here it is. I'm making a GUI form in Borland and I have inserted an image there.

When I move my mouse over that image, the X and Y coordinates of the mouse position should appear in two edit boxes.

I know I have to do that with the OnMouseMove Event, but I don't know how. Can anybody give me a help on that? I have no code made yet, due to this issue.

Thanks a lot for your help!

pinoynl
Is this MFC? WinAPI?
Sorry..it's winAPI :)
Hi Disch,

I have found it already. After all it seemed to be quite simple :)
I have figured out the code below and it works, only I still have to find out how to give the coordinates of the image and not the whole screen.

1
2
3
4
5
6
7
8
       
        struct point {int x; int y;};

        POINT pos;
        GetCursorPos(&pos);
        this->Edit1->Text = (pos.x);
        this->Edit2->Text = (pos.y);


Thanks anyway
Cheers
pinoynl
You trap the WM_MOUSEMOVE message. The LPARAM of the message is the X and Y position of the mouse cursor in the window client area.

You know where your image is within the window (or you should do ).

You then check whether the mouse position is within the rectangle area of the image.
hi guestgulkan,

thanks for your reply. Can I use ScreenToClient and what parameters do I have to use for it? I have no clue how to trap the WM_MOUSEMOVE message...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
include <vcl.h>
#pragma hdrstop

#include "test.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        struct point {int x; int y;};

        POINT pos;
        GetCursorPos(&pos);
        ScreenToClient( );        this->Edit1->Text = (pos.x);
        this->Edit2->Text = (pos.y);

}


//---------------------------------------------------------------------------
This function void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
where did this come from?

I would think that X and Y are already relative to the image (topleft) anyway.

What is your actual problem??
I will show you the whole code. The program should determine the position of the mouse, when you hoover over an image and put it in two editboxes. When you click the record button first, and then click on a position on the image, it will store the position of the mouse in a vector. Then when you click the play button all the stored positions will become visible after each other on the image in red.

I have fixed the problem of determining the position of the mouse, but...now I can't make it visible on the image when I press the playbutton.

Anybody got a clue how to do this?? I assume it has to do with the void __fastcall TForm1::buttonReverseClick(TObject *Sender) function but I can't figure out what the parameters of the ellipse should be?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

#include <vcl.h>
#include <vector>
#include <time.h>
#pragma hdrstop

#include "test.h"
using namespace std;

vector<int> x;
vector<int> y;

int i = 0;

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        struct point {int X; int Y;};

        POINT pos;
        GetCursorPos(&pos);
        this->Edit1->Text = (pos.x);
        this->Edit2->Text = (pos.y);

}

void __fastcall TForm1::buttonQuitClick(TObject *Sender)
{
        this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1Click(TObject *Sender)
{
        while(CheckBox1->Checked == true)
        {
          CheckBox1->Checked = false;
        }

        if(buttonPlay->Enabled == false)
        {
           x.push_back(this->Edit1->Text.ToInt());
           y.push_back(this->Edit2->Text.ToInt());
           Edit3->Text = x.size();
           Edit4->Text = x.capacity();
        }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::buttonReverseClick(TObject *Sender)
{
        vector<int> xhulp;
        vector<int> yhulp;
        int count = x.size();

        if (!(count <= 0))
        {
           count -= 1;
           for (int index = 0; (index < x.size()) && (count >= 0); index++)
           {
              xhulp.push_back(x.at(count));
              yhulp.push_back(y.at(count));
              count--;
           }
        x = xhulp;
        y = yhulp;
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::buttonAllClick(TObject *Sender)
{
        x.clear();
        y.clear();

        if (x.empty() && y.empty())
        {
           Edit3->Text = x.size();
           Edit4->Text = y.size();
           CheckBox1->Checked = true;
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::buttonRemoveClick(TObject *Sender)
{
        if (!(x.empty()) && !(y.empty()))
        {
           x.pop_back();
           y.pop_back();
           Edit3->Text = x.size();
        }
        else
        {
           Edit3->Text = x.size();
           Edit4->Text = y.size();
           CheckBox1->Checked = true;
        }
}

void __fastcall TForm1::buttonRecordClick(TObject *Sender)
{
        if(buttonRecord->Caption == "record")
        {
            buttonRecord->Caption = "stop";
            buttonPlay->Enabled = false;
        }
        else
        {
           buttonRecord->Caption = "record";
           buttonPlay->Enabled = true;
        }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::buttonPlayClick(TObject *Sender)
{
        Timer1->Enabled = true;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
        int c;

        if(Timer1->Enabled == true)
        {
           c = x.size();
           this->Image1->Canvas->Brush->Color = clRed;
           this->Canvas->Ellipse(10, 10, 10, 10);
           this->ProgressBar1->StepBy(c);
           i++;
        }
}
//---------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        struct point {int X; int Y;};

        POINT pos;
        GetCursorPos(&pos);
        this->Edit1->Text = (pos.x);
        this->Edit2->Text = (pos.y);

}


I would think that the mouse position in the image area are passed to you in the X and Y variables.

I wouldn't think you would need all that point stuff.

All it seems you need is to get a string representation of the X and Y values. Does Borland not have a intToStr function for this purpose?

Then your function would look something like this:
1
2
3
4
5
6
7
8
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{

        this->Edit1->Text = ( intToStr(X) );
        this->Edit2->Text = (intToStr(Y) );

}
wow thanks for your help! it works better now! :)

Sorry for asking too much, but how do I clean up the image again after the playback?
Topic archived. No new replies allowed.