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

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    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. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by nutzu2010 View Post
    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;
    	}
    	//get ready to draw
    	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
    }
    Last edited by novacain; 07-23-2011 at 09:36 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    Quote Originally Posted by novacain View Post
    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.


    Quote Originally Posted by novacain View Post
    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
    Last edited by nutzu2010; 07-24-2011 at 01:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ellipse problem
    By PR7loco in forum C++ Programming
    Replies: 15
    Last Post: 11-18-2010, 11:37 PM
  2. CDC::Ellipse()
    By manzoor in forum Windows Programming
    Replies: 2
    Last Post: 12-15-2008, 09:28 AM
  3. How to fill an ellipse
    By Ducky in forum Windows Programming
    Replies: 4
    Last Post: 08-30-2008, 09:05 AM
  4. Ellipse error
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2005, 02:57 PM
  5. some ellipse and ExtFloodFill problem
    By monkeymon in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2002, 10:20 AM