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;
}
//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
}