Board index » cppbuilder » StringGrid-Cell/Mouse Location

StringGrid-Cell/Mouse Location

I'm trying to perform a particular action when the mouse pointer rests in
the upper-right quadrant of a TStringGrid cell for more than a half-second.
This should be fairly simple using the OnIdle handler & a TTimer; however,
I'm not sure how to determine whether the mouse is resting in the
upper-right quadrant of a cell, and which cell it is resting in.

Can anyone here lend a suggestion?  Thanks, in advance, for any help you can
provide.

David

 

Re:StringGrid-Cell/Mouse Location


TStringGrid has MouseToCell() and CellRect() methods you would need to use
for this.  Note that the X and Y parameters of MouseToCell() need to be in
client coordinates relative to the grid's upper-left corner, not in absolute
screen coordinates, so they need to be converted first.

Try something like this:

POINT p;
GetCursorPos(&p); // get the cursor's current screen location
p = StringGrid1->ScreenToClient(p);  // convert to client coordinates

int Col, Row;
StringGrid1->MouseToCell(p.x, p.y, Col, Row); // find the cell
if(Col > -1 && Row > -1)
{
    RECT r = StringGrid1->CellRect(Col, Row);  // get the cell's rect
    int width = (r.right-r.left);
    int height = (r.bottom-r.top);

    // I broke this if() statement up for a little easier reading
    if(
        ( p.x > ( r.left + (width/2) ) )
        &&
        ( p.y < ( r.top + (height/2) ) )
    )
    {
        // cursor is in the upper-right quadrant of the cell at Col, Row
    }

Quote
}

Gambit
Quote
"David Ray" <da...@timecalc.com> wrote in message news:3a7af509_2@dnews...
> I'm not sure how to determine whether the mouse is resting in the
> upper-right quadrant of a cell, and which cell it is resting in.

Re:StringGrid-Cell/Mouse Location


Thanks a lot for the ideas.....I'll give it a try...

David

Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message

news:95fv67$jk18@bornews.inprise.com...
Quote
> TStringGrid has MouseToCell() and CellRect() methods you would need to use
> for this.  Note that the X and Y parameters of MouseToCell() need to be in
> client coordinates relative to the grid's upper-left corner, not in
absolute
> screen coordinates, so they need to be converted first.

> Try something like this:

> POINT p;
> GetCursorPos(&p); // get the cursor's current screen location
> p = StringGrid1->ScreenToClient(p);  // convert to client coordinates

> int Col, Row;
> StringGrid1->MouseToCell(p.x, p.y, Col, Row); // find the cell
> if(Col > -1 && Row > -1)
> {
>     RECT r = StringGrid1->CellRect(Col, Row);  // get the cell's rect
>     int width = (r.right-r.left);
>     int height = (r.bottom-r.top);

>     // I broke this if() statement up for a little easier reading
>     if(
>         ( p.x > ( r.left + (width/2) ) )
>         &&
>         ( p.y < ( r.top + (height/2) ) )
>     )
>     {
>         // cursor is in the upper-right quadrant of the cell at Col, Row
>     }
> }

> Gambit

> "David Ray" <da...@timecalc.com> wrote in message news:3a7af509_2@dnews...
> > I'm not sure how to determine whether the mouse is resting in the
> > upper-right quadrant of a cell, and which cell it is resting in.

Other Threads