-1

I am creating an Bezier curve in WinApi with C++ and my problem is that my right Button is not working. This is my code:

if (wParam & MK_LBUTTON)
    {
        HGDIOBJ original = NULL;
        
        //Saving the original object
        original = SelectObject(hdc,GetStockObject(DC_PEN));
        hdc = GetDC (hwnd) ;
        SelectObject(hdc, GetStockObject(DC_PEN));
        SetDCPenColor(hdc, RGB(246, 245, 243));
        myBezier.DrawBezier (hdc, apt) ;
               
        if (wParam & MK_LBUTTON)
            {
                apt[1].x = LOWORD (lParam) ;
                apt[1].y = HIWORD (lParam) ;
            }
               
        else if (wParam & MK_RBUTTON)
            {
                apt[2].x = LOWORD (lParam) ;
                apt[2].y = HIWORD (lParam) ;
            }
               
        SelectObject (hdc, GetStockObject(BLACK_PEN)) ;
        myBezier.DrawBezier (hdc, apt) ;
        ReleaseDC (hwnd, hdc) ;
        DeleteObject(original);
    }

Left Button is perfectly working but I don't know why RightButton and other keys or even MidButton are not working as well. Any idea?

enter image description here

With the left button I am able to adjust the left line, with the right button the right line. But in this case the right button is never got.

Community
  • 1
  • 1
Nicholas
  • 3,529
  • 2
  • 23
  • 31
  • You should make sure you don't paste in tab characters for indenting, otherwise your code looks crazy. – crashmstr Apr 23 '14 at 11:50
  • 3
    Considering your entire code block requires the left button to be pressed, what did you expect to occur? `if (wParam & MK_LBUTTON)` at the top... – Dark Falcon Apr 23 '14 at 11:51
  • Sorry, I wanted to refactor but it told me that someone edited it :D – Nicholas Apr 23 '14 at 11:52
  • How are you supposed to detect right mouse inside the left mouse if statement?? – Marco A. Apr 23 '14 at 11:52
  • Sorry @nico, did I end up wiping out a code change? I think I formatted the original post. – Collin Apr 23 '14 at 11:52
  • @DarkFalcon if my left button will be pressed than by moving the mouse I will can drag the 1 point of my bezier curve. – Nicholas Apr 23 '14 at 11:53
  • So you're expecting to be pressing both buttons simultaneously to trigger this code? – Dark Falcon Apr 23 '14 at 11:53
  • @Collin, no no it is ok. – Nicholas Apr 23 '14 at 11:53
  • No. First if I press the LButton than a bezier curve will appear in my canvas. After that I will go for LButton or RButton to move the points from my curve. – Nicholas Apr 23 '14 at 11:55
  • [link](https://copy.com/2VRk1SEQUpnm) This is my bezier curve. With the left button I am able to control the left Line. With the right button I will be able to control the right Line. – Nicholas Apr 23 '14 at 11:58

1 Answers1

0

if (wParam & MK_LBUTTON) ensures that none of the code you have posted actually runs unless the left button is being held down. You can press the right button all you want and it will never make it inside the if body unless the left button is also pressed. Then your check for the right button is missed because you check for the left button first. I cannot know what you intended, but I expect you were looking for something like this:

if ((wParam & MK_LBUTTON) || (wParam & MK_RBUTTON))
{
    HGDIOBJ original = NULL;

    //Saving the original object
    original = SelectObject(hdc,GetStockObject(DC_PEN));
    hdc = GetDC (hwnd) ;
    SelectObject(hdc, GetStockObject(DC_PEN));
    SetDCPenColor(hdc, RGB(246, 245, 243));
    myBezier.DrawBezier (hdc, apt) ;

    if (wParam & MK_LBUTTON)
        {
            apt[1].x = LOWORD (lParam) ;
            apt[1].y = HIWORD (lParam) ;
        }

    else if (wParam & MK_RBUTTON)
        {
            apt[2].x = LOWORD (lParam) ;
            apt[2].y = HIWORD (lParam) ;
        }

    SelectObject (hdc, GetStockObject(BLACK_PEN)) ;
    myBezier.DrawBezier (hdc, apt) ;
    ReleaseDC (hwnd, hdc) ;
    DeleteObject(original);
}
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Still the right button is not working :(. I tried that before. – Nicholas Apr 23 '14 at 12:02
  • There is insufficient information to determine the issue. Try posting more code from around it. – Dark Falcon Apr 23 '14 at 12:02
  • well this is the code where the action goes. I have the drawBezier function where I Draw my curve and that's it. Also all this code goes in WM_MOUSEMOVE case – Nicholas Apr 23 '14 at 12:10
  • If I substitute RBUTTON with LBUTTON than it is working. That so weird. – Nicholas Apr 23 '14 at 12:11
  • 2
    `if ((wParam & MK_LBUTTON) || (wParam & MK_RBUTTON))` could alternatively be written as `if (wParam & (MK_LBUTTON | MK_RBUTTON))`. Also, [the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms645616.aspx) says to use `GET_X_LPARAM()` and `GET_Y_LPARAM()` instead of using `LOWORD()` and `HIWORD()`, otherwise your code will not work correctly on a multi-monitor system. – Remy Lebeau Apr 23 '14 at 18:25