views:

74

answers:

4

ok i know how to do the left mouse button down evet(WM_LBUTTONDOWN). but im having some troubles with it. when use it with vectors it seems to add 101 elemnts everytime the left mouse button is down. i think that every time the mouse button is down, it sends 101 messages to WM_LBUTTONDOWN that causes 101 elements to be added. here is the code for the event

case WM_LBUTTONDOWN:
    iRegularShots=0;
    pt.x = GET_X_LPARAM(lParam); 
    pt.y = GET_Y_LPARAM(lParam); 
    pRegularShots.push_back(pt); 
    InvalidateRect(hWnd, rect, false); 
    break;

any ideas ?


im not missing a break;
i used teh size() function to tell me how many elemnts were assigned.
i set two break points one one pRegularShots.push_back(pt); and the other one on different function that will use what is inside the vector to display the image. and i got 101 calls over there but only one call on the pRegularShots.push_back(pt);.
this is the function code

VOID fRegularShot(HDC hdc, HWND hWnd) 
{ 
    Graphics graphics(hdc); 
    Image shot(L"RegularShots.png"); 
    long index=0;
    long s=pRegularShots.size();
    while(index < (long)pRegularShots.size()) 
    { 
        graphics.DrawImage(&shot, pRegularShots[index].x, pRegularShots[index].y); 
        ++index;
    } 
} 

windows prudocer

switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        OnPaint(hdc, hWnd, 1);
        if(iRegularShots==0)
        {
            fRegularShot(hdc, hWnd);
        }
        EndPaint(hWnd, &ps);
        break;
    case WM_LBUTTONDOWN:
        iRegularShots=0;
        pt.x = GET_X_LPARAM(lParam); 
        pt.y = GET_Y_LPARAM(lParam); 
        pRegularShots.push_back(pt); 
        InvalidateRect(hWnd, rect, false); 
        return 0;
        break; 
    case WM_LBUTTONUP:
            iRegularShots=1;
            break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
+2  A: 

There isn't any reason why you would normally get a lot of WM_LBUTTONDOWN events when you press the button once.

Perhaps your previous case section (that handles some other message) is missing a break?

Greg Hewgill
Beat me to it. :)
Will A
@Greg it is not missing anything
Ramiz Toma
A: 

Are you sure it's this code that's generating the 101 entries in the vector - the preceding case (if there is one) isn't missing a break; is it?

Will A
nothing is missing, no breaks are missing
Ramiz Toma
A: 

What do you return from the window procedure? WM_LBUTTONDOWN should return 0 to indicate the message was handled (if you don't, you'll continue to receive the message until it's handled).

Matthew Iselin
it compiled but still havn't solved the problem
Ramiz Toma
A: 

omg it was my fault i set the vetor to 100 elemnts sorry guys

Ramiz Toma