Simple use of the Windows API to implement the Bezier curve. The two endpoints are fixed, the vertical position is half the height of the display area, and the horizontal direction is at 1/4 and 3/4 of the display width, respectively.
Two control points can be changed, hold down the left mouse button and drag the mouse can change the first handle, hold right button and drag the mouse can change the second handle.
The code is as follows:
#include <windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline,inticmdshow) { StaticTCHAR szappname[] = TEXT ("Bezier Curve"); HWND hwnd; MSG msg; Wndclass Wndclass; Wndclass.style= Cs_hredraw |Cs_vredraw; Wndclass.lpfnwndproc=WndProc; Wndclass.cbclsextra=0; Wndclass.cbwndextra=0; Wndclass.hinstance=hinstance; Wndclass.hicon=LoadIcon (NULL, idi_application); Wndclass.hcursor=loadcursor (NULL, Idc_arrow); Wndclass.hbrbackground=(Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname=NULL; Wndclass.lpszclassname=Szappname; if(! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program needs to run under Windows NT! "), Szappname, Mb_iconerror); return 0; } hwnd=CreateWindow (Szappname, TEXT ("Bezier Curve! "), Ws_overlappedwindow, Cw_usedefault, Cw_usedefault, Cw_usedefault, Cw_usedefault, NULL, NULL, HINSTANCE, NULL); ShowWindow (hwnd, icmdshow); UpdateWindow (HWND); while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); } returnMsg.wparam;}voidDrawBezier (hdc hdc, point apt[]) {polybezier (hdc, APT,4); Movetoex (hdc, apt[0].x, apt[0].y, NULL); LineTo (hdc, apt[1].x, apt[1].y); Movetoex (hdc, apt[2].x, apt[2].y, NULL); LineTo (hdc, apt[3].x, apt[3].y);} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {StaticPoint apt[4]; HDC hdc; intcxclient, cyclient; Paintstruct PS; Switch(message) { Casewm_size:cxclient=LoWord (LParam); Cyclient=HiWord (LParam); apt[0].x = cxclient/4; apt[0].Y = cyclient/2; apt[1].x = cxclient/2; apt[1].Y = cyclient/4; apt[2].x = cxclient/2; apt[2].Y =3* Cyclient/4; apt[3].x =3* Cxclient/4; apt[3].Y = cyclient/2; CaseWm_lbuttondown: CaseWm_rbuttondown: CaseWm_mousemove:if(WParam & Mk_lbutton | | WParam &&Mk_rbutton) {HDC=GetDC (HWND); SelectObject (hdc, Getstockobject (White_pen)); DrawBezier (hdc, APT); if(WParam &Mk_lbutton) {apt[1].x =LoWord (LParam); apt[1].Y =HiWord (LParam); } if(WParam &Mk_rbutton) {apt[2].x =LoWord (LParam); apt[2].Y =HiWord (LParam); } SelectObject (hdc, Getstockobject (Black_pen)); DrawBezier (hdc, APT); ReleaseDC (hwnd, HDC); } return 0; CaseWm_paint:invalidaterect (hwnd, NULL, TRUE); HDC= BeginPaint (hwnd, &PS); DrawBezier (hdc, APT); EndPaint (hwnd,&PS); return 0; CaseWm_destroy:postquitmessage (0); return 0; } returnDefWindowProc (hwnd, message, WParam, LParam);}
Bezier curves under the Win32