Thread: Bezier curve

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Bezier curve

    Hello to all! This is my first time on this board, and I have one question.
    I'm new to Windows programming.
    I'm trying to create the Bezier curves with endpoints and control points within the four squares.
    I created the core, but I have the problem. Once I click on square surrounding any of four points,
    it became black, and that indicates that the point (square) is selected.
    BUT, when I click on some other square, it also became black and selected. My goal is to make the
    first clicked square deselected, when user clicks anywhere inside the client area,
    including other squares.

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
            PSTR szCmdLine, int iCmdShow)
    {
        HWND         hwnd ;
        MSG          msg ;
        WNDCLASS     wc ;
    
        wc.style         = CS_HREDRAW | CS_VREDRAW ;
        wc.lpfnWndProc   = WndProc ;
        wc.cbClsExtra    = 0 ;
        wc.cbWndExtra    = sizeof(long) ;
        wc.hInstance     = hInstance ;
        wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wc.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
        wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wc.lpszMenuName  = NULL ;
        wc.lpszClassName = "My class";
    
        if (!RegisterClass (&wc))
        {
            MessageBox (NULL, "Error in registration", 
                    "Error", MB_ICONERROR) ;
            return 0 ;
        }
        hwnd = CreateWindow ("My class",  
                "Problem 1", 
                WS_OVERLAPPEDWINDOW, 
                100,             
                100,              
                300,             
                300,              
                NULL,            
                NULL,            
                hInstance,     
                NULL) ;        
        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;
    
        while (GetMessage (&msg, NULL, 0, 0))
        {
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
        }
        return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    { 
    	HDC hDC;
        PAINTSTRUCT ps;
        POINT Pt[4] = { { 0, 130 }, { 30, 130 }, { 260, 130 }, { 290, 130} };
        int side = 8;
    	int k;
    	int x;
    	int y;
    
    	RECT square[4];
    		
    	
        switch(Msg)
        {
        	case WM_CREATE:
    				
    				SetWindowLong(hWnd,0,0);
    			break;
    	case WM_LBUTTONDOWN:
    				
                    x = LOWORD(lParam); 
                    y = HIWORD(lParam);
                    for(k = 0; k < 4; k++)
                    {
                        square[k].left = Pt[k].x - side/2;
                        square[k].top = Pt[k].y - side/2;
                        square[k].right = Pt[k].x + side/2;
                        square[k].bottom = Pt[k].y + side/2;
                    }
                    for(k = 0; k < 4; k++)
                    {   
                        if((square[k].left <= x) && (x <= square[k].right))
                            if((square[k].top <= y) && (y <= square[k].bottom))
                                {
    								SetWindowLong(hWnd,0,!GetWindowLong(hWnd,0));
                                    InvalidateRect(hWnd, &square[k], TRUE);
    								break;
                                }
                    }
                    break;
                case WM_PAINT:
                    hDC = BeginPaint(hWnd, &ps);
                    for(k = 0; k < 4; k++)
                    {
                        Rectangle(hDC, Pt[k].x - side/2,Pt[k].y - side/2 , Pt[k].x + side/2 , Pt[k].y + side/2);
                        PolyBezier(hDC, Pt, 4);
                    }
    				
                    for(k = 0; k < 4; k++)
                    {
                        square[k].left = Pt[k].x - side/2;
                        square[k].top = Pt[k].y - side/2;
                        square[k].right = Pt[k].x + side/2;
                        square[k].bottom = Pt[k].y + side/2;
                        if(GetWindowLong(hWnd, 0))
                            FillRect(hDC,&square[k],(HBRUSH)GetStockObject(BLACK_BRUSH));
                    }
    			
                    EndPaint(hWnd, &ps);
                    break;
                case WM_DESTROY:
                    PostQuitMessage(WM_QUIT);
                    break;
        }
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    Last edited by simo; 12-10-2005 at 05:03 AM.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    get_click(...param..)
    {
      if click square
      ... then colour square black ->hold value in temp
      if click square again
      ... then decolour square held in temp
          and colour existing square...
     
      yada,yada,yada
    }

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    Well, in general I know that would be algorithm, but since I'm beginer I don't know how to fit that procedure in my existing code.
    Can you give me a little code snippet please?
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using basic library to write curve to bmp
    By CIO in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 02:25 PM
  2. Bezier Curves where y = f(x)
    By thetinman in forum Game Programming
    Replies: 11
    Last Post: 12-27-2008, 10:21 AM
  3. Collision detection between bezier and ball.
    By CornedBee in forum Game Programming
    Replies: 1
    Last Post: 04-27-2006, 12:49 PM
  4. Help with bezier functions
    By rich999 in forum C++ Programming
    Replies: 10
    Last Post: 03-18-2006, 01:57 PM
  5. Attaching Cylinder to Bezier curve...
    By MipZhaP in forum Game Programming
    Replies: 5
    Last Post: 09-02-2004, 10:09 AM