Board index » cppbuilder » Moving the cursor

Moving the cursor

I have a TImage on my form and can track the cursor.  I would like to be
able to move the cursor around a pixel at a time with the arrow keys but
don't know how.

Is there a simple (more or less) way of doing this?

Thanks

--
Robert N. Lockwood
USDA Forest Service
Forest Fire Laboratory
Riverside, CA
RNLockw...@attglobal.net

 

Re:Moving the cursor


Try something like this, modifying it as needed to suit your needs:

void __fastcall Object::KeyPress(TObject *Sender, char &Key)
{
    POINT pos;
    GetCursorPos(&pos);

    switch(Key)
    {
        case VK_LEFT:
            SetCursorPos(pos.x-1, pos.y);
            break;
        case VK_RIGHT:
            SetCursorPos(pos.x+1, pos.y);
            break;
        case VK_UP:
            SetCursorPos(pos.x, pos.y-1);
            break;
        case VK_DOWN:
            SetCursorPos(pos.x, pos.y+1);
            break;
    }

Quote
}

Gambit

"Robert N. Lockwood" <rnlockw...@attglobal.net> wrote in message
news:38471A67.C8906F25@attglobal.net...

Quote
> I have a TImage on my form and can track the cursor.  I would like to be
> able to move the cursor around a pixel at a time with the arrow keys but
> don't know how.

> Is there a simple (more or less) way of doing this?

Re:Moving the cursor


: "Robert N. Lockwood" <rnlockw...@attglobal.net> wrote:

Quote
>I would like to be
>able to move the cursor

Please have a look at the SetCursor Windows API.

--
General information:
  * Post to the right group - http://www.borland.com/newsgroups/
    * Do not cross- or multipost
      * Research at http://www.mers.com/searchsite.html

Stefan Hoffmeister - http://www.econos.de/
(TeamB - http://www.teamb.com/)

Re:Moving the cursor


Great, this will get me started.
I didn't find any key press events in TImage. I will have to trap them
on my main form, map the coordinates, and set a new position only when
the cursor is over the image - is this correct?

Thanks,  
Bob

Quote
Remy Lebeau wrote:

> Try something like this, modifying it as needed to suit your needs:

> void __fastcall Object::KeyPress(TObject *Sender, char &Key)
> {
>     POINT pos;
>     GetCursorPos(&pos);

>     switch(Key)
>     {
>         case VK_LEFT:
>             SetCursorPos(pos.x-1, pos.y);
>             break;
>         case VK_RIGHT:
>             SetCursorPos(pos.x+1, pos.y);
>             break;
>         case VK_UP:
>             SetCursorPos(pos.x, pos.y-1);
>             break;
>         case VK_DOWN:
>             SetCursorPos(pos.x, pos.y+1);
>             break;
>     }
> }

> Gambit

> "Robert N. Lockwood" <rnlockw...@attglobal.net> wrote in message
> news:38471A67.C8906F25@attglobal.net...
> > I have a TImage on my form and can track the cursor.  I would like to be
> > able to move the cursor around a pixel at a time with the arrow keys but
> > don't know how.

> > Is there a simple (more or less) way of doing this?

--
Robert N. Lockwood
USDA Forest Service
Forest Fire Laboratory
Riverside, CA
RNLockw...@attglobal.net

Re:Moving the cursor


Correct.

Set the gorm's KeyPreview property to true, and use the form's OnKeyPress
event, something like this:

void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
    POINT cursorPos, imagePos;
    GetCursorPos(&pos);
    imagePos = Image1->ClientOrigin;

    if((cursorPos.x >= imagePos.x && cursorPos.x <=
(imagePos.x+Image1->Width)) && (cursorPos.y >= imagePos.y && cursorPos.y <=
(imagePos.y+Image->Height)))
    {
        switch(Key)
        {
            case VK_LEFT:
                SetCursorPos(pos.x-1, pos.y);
                break;
            case VK_RIGHT:
                SetCursorPos(pos.x+1, pos.y);
                break;
            case VK_UP:
                SetCursorPos(pos.x, pos.y-1);
                break;
            case VK_DOWN:
                SetCursorPos(pos.x, pos.y+1);
            break;
        }
    }

Quote
}

Gambit

Robert N. Lockwood <rnlockw...@attglobal.net> wrote in message
news:384811A8.EA4D8AD1@attglobal.net...

Quote
> Great, this will get me started.
> I didn't find any key press events in TImage. I will have to trap them
> on my main form, map the coordinates, and set a new position only when
> the cursor is over the image - is this correct?

Re:Moving the cursor


That should be the SetCursorPos function.  SetCursor is for changing the
cursor itself, not it's position.

Gambit

Stefan Hoffmeister (TeamB) <Stefan.Hoffmeis...@Econos.de> wrote in message
news:4nbe4ss7grv3p5pj5fr8442fld45a4j57i@4ax.com...

Quote
> : "Robert N. Lockwood" <rnlockw...@attglobal.net> wrote:

> >I would like to be
> >able to move the cursor

> Please have a look at the SetCursor Windows API.

Re:Moving the cursor


Thanks again.  I had no idea that this was available:
Image1->ClientOrigin.  

Bob

Quote
Remy Lebeau wrote:

> Correct.

> Set the gorm's KeyPreview property to true, and use the form's OnKeyPress
> event, something like this:

> void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
> {
>     POINT cursorPos, imagePos;
>     GetCursorPos(&pos);
>     imagePos = Image1->ClientOrigin;

>     if((cursorPos.x >= imagePos.x && cursorPos.x <=
> (imagePos.x+Image1->Width)) && (cursorPos.y >= imagePos.y && cursorPos.y <=
> (imagePos.y+Image->Height)))
>     {
>         switch(Key)
>         {
>             case VK_LEFT:
>                 SetCursorPos(pos.x-1, pos.y);
>                 break;
>             case VK_RIGHT:
>                 SetCursorPos(pos.x+1, pos.y);
>                 break;
>             case VK_UP:
>                 SetCursorPos(pos.x, pos.y-1);
>                 break;
>             case VK_DOWN:
>                 SetCursorPos(pos.x, pos.y+1);
>             break;
>         }
>     }
> }

> Gambit

> Robert N. Lockwood <rnlockw...@attglobal.net> wrote in message
> news:384811A8.EA4D8AD1@attglobal.net...
> > Great, this will get me started.
> > I didn't find any key press events in TImage. I will have to trap them
> > on my main form, map the coordinates, and set a new position only when
> > the cursor is over the image - is this correct?

--
Robert N. Lockwood
USDA Forest Service
Forest Fire Laboratory
Riverside, CA
RNLockw...@attglobal.net

Re:Moving the cursor


Sometimes it pays to look at the help files ;-)

Gambit

"Robert N. Lockwood" <rnlockw...@attglobal.net> wrote in message
news:38483C23.5D949BB7@attglobal.net...

Quote
> Thanks again.  I had no idea that this was available:
> Image1->ClientOrigin.

Re:Moving the cursor


Well, with a little modification to keep the cursor in the bounds of
Image1 I'm sure that the code would work if I could trap the arrow
keys.  I tried both OnKeyPress and OnKeyDown and when I could trap most
of the keys on the keyboard except a few, including tab and the arrow
keys I checked HELP.

The arrow keys are in a set called navigation keys and they don't
generate a keyboard event so he keyboard event functions don't get
called.

Is this Catch 22?

Bob  

--
Robert N. Lockwood
USDA Forest Service
Forest Fire Laboratory
Riverside, CA 92507
RNLockw...@attglobal.net

Other Threads