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