# Thread: How do i draw something(a ellipse) when mouse is over a point?

1. ## How do i draw something(a ellipse) when mouse is over a point?

Ex.:I have drawn a line and i want to move in another position.For an easier move, when the mouse is over point line(begin or end,no matter what) I would want draw a ellipse.
Must use BOOL PtInRect( __in const RECT *lprc, __in POINT pt);?
if yes,then how convert a point(structure POINTS) to a point(structure RECT)?and a point(structure POINTS) to a point(structure POINT)?

2. I don't think RECT is a point -- it should be two points (upper left and lower right, if I remember correctly). You can't convert from one to the other.

3. POINT struct is 2 longs, POINTS struct is 2 shorts. RECT is 4 longs.

You can convert between POINT/S with MAKEPOINT, POINTSTOPOINT or POINTTOPOINTS.

You can safely cast from long to short (and back) in this case, as screen coords are unlikely to exceed the size of a short.

ir SetRect(&theRect, (long)startPoints.x, (long)startPoints.y, (long)endPoints.x, (long)endPoints.y);

then use the Ellipse() function to draw the shape.

4. i have made a simple exemple,but does not work:
Code:
case WM_MOUSEMOVE:
{

hdc = GetDC(hwndMain);
SetRect(&rcClient,(long)100,(long)100,(long)120,(long)120);

if (PtInRect(&rcClient,pt))
{
SetROP2(memDC,R2_BLACK);
Ellipse(memDC,100,100,120,120);
}
else
{
SetROP2(memDC,R2_WITHE);
Ellipse(memDC,100,100,120,120);

}
BitBlt(hdc,0,0,cScreenX,cScreenY,memDC,0,0,SRCCOPY);
ReleaseDC(hwndMain, hdc);
}
break;

5. Originally Posted by nutzu2010
i have made a simple exemple,but does not work:
You have a typo... R2_WITHE

Look at this as a pattern for other messages.

I would write functions and put all the code in it.
Pass in any params you need (HWND, LPARAM, WPARAM etc)

This makes the callback easier to follow as it gets bigger.

[NOTE: not written with a compiler, just typed off the top of my head so this is full of bugs and does not work, use at own risk.....]

Code:
case WM_MOUSEMOVE:
DoMouseMove(hwndMain, wParam, lParam, startPoint );
break;

///////////////////////////////////////////////////////
//mouse move handler

// POINT StartPoint is the client coord where the user pressed teh left mouse button

// we may need to use FillRect() or similar to erase the previous ellipse or we will get lots of ellipses on top of one another

void DoMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam, POINT StartPoint )
{
//we only process when the Left button is down
if ( MK_LBUTTON != wParam) //not the mouse msg we are looking for
{
return;
}
POINT currentPoint  = {0,0};
currentPoint.x = LOWORD(lParam);
currentPoint.y = HIWORD(lParam);

//convert mouse coords as required

RECT drawRect = {100,100,120,120};
//create our GDI objects with enough scope
HBRUSH *pOrigBrush = NULL, newBrush;
COLORREF colour = RGB(255,255,255);// this is white
//should we change to a black brush?
if (PtInRect(&drawRect,currentPoint)
{
colour = RGB(0,0,0);//BLACK
}
//create our brush
newBrush = CreateSolidBrush(colour); //create a brush
//select our brush
pOrigBrush = (HBRUSH*) SelectObject(hMemDC, newBrush); //select in, catching the current brush which is pushed out
//set the area we will draw the ellipse in
drawRect.left = min(StartPoint.x, currentPoint.x);
drawRect.right= max(StartPoint.x, currentPoint.x);
drawRect.top= min(StartPoint.y, currentPoint.y);
drawRect.bottom= max(StartPoint.y, currentPoint.y);
//draw to the mem DC
Ellipse(hMemDC, drawRect.left , drawRect.top, drawRect.right, drawRect.bottom);
//clean up
//put memDC back the way we found it
SelectObject(hMemDC, hOrigBrush);
//delete the objects we created
DeleteObject(newBrush);
//call for a paint
InvalidateRect(hWnd, drawRect, TRUE); //only redraw the minimum rect we can so our painting is faster
UpdateWindow(hWnd); //bypass the OS and App msg queus and send this paint msg directly to the callback, so our paint is faster
}

6. Originally Posted by novacain
You have a typo... R2_WITHE
Sorry for mistake(But I had another important mistake,unreported by the compiler).I changed too much code and forgot to convert points to point.

Originally Posted by novacain
Look at this as a pattern for other messages.

I would write functions and put all the code in it.
Pass in any params you need (HWND, LPARAM, WPARAM etc)

This makes the callback easier to follow as it gets bigger.

[NOTE: not written with a compiler, just typed off the top of my head so this is full of bugs and does not work, use at own risk.....]
I thought I about it and will take into account.

This is the code that works:
Code:
case WM_MOUSEMOVE:
{

hdc = GetDC(hwndMain);
RECT rcClient={100,100,120,120};
pts=MAKEPOINTS(lParam);//pts is structure POINTS
POINTSTOPOINT(pt,pts);//pt is structure POINT
if (PtInRect(&rcClient,pt))
{
SetROP2(memDC,R2_BLACK);
Ellipse(memDC,100,100,120,120);
}
else
{
SetROP2(memDC,R2_WHITE);
Ellipse(memDC,100,100,120,120);

}
InvalidateRect(hwndMain,&rcClient,TRUE);
UpdateWindow(hwndMain);
ReleaseDC(hwndMain, hdc);
}
break;
Can you give me a reference?I would like to draw a line across another line without disappearing.I Need change in SetROP2()?(edit)=>What a fool are, logically must exchange.(I'll think about it).

sorry for my English