Windows Programming (7): drawing the besell Curve

Source: Internet
Author: User
# Include <windows. h> # include <math. h> # define num 1000 # define twopi (2*3.14159) lresult callback wndproc (hwnd, uint, wparam, lparam); int winapi winmain (hinstance, // The current instance handle hinstance hprevinstance, // The previous instance handle lpstr lpcmdline, // command line int icmdshow) // display status {static tchar szappname [] = text ("bezr"); // window handle hwnd; // message MSG; // window class wndclass; // window style: when the window is moved or the size of the window is changed, the wndclass is repainted. style = cs_hredraw | Cs _ Vredraw; // specify the callback function wndclass. lpfnwndproc = wndproc; // The extra bit is used to determine the position of the next window class. wndclass is not used for the moment. cbclsextra = 0; // The extra bit is used to confirm the location of the next window instance. wndclass is not used for the moment. cbwndextra = 0; // instance handle wndclass. hinstance = hinstance; // The Mount icon wndclass. hicon = loadicon (null, idi_application); // load the cursor wndclass. hcursor = loadcursor (null, idc_arrow); // The background is white wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // menu: No wndclass. lpszmenuname = NULL ;/ /Window class name wndclass. lpszclassname = szappname; // register the window if (! Registerclass (& wndclass) {return-1 ;}// create a window hwnd = createwindow (szappname, // name of the window class, it must be a registered text ("Bessert function"), // window title ws_overlappedwindow, // window style cw_usedefault, // X coordinate cw_usedefault, // y coordinate cw_usedefault, // width cw_usedefault, // height null, // parent window handle null, // menu window handle hinstance, // WINDOS of the advanced version ignores NULL ); // display window // showwindow (hwnd, sw_showna); showwindow (hwnd, icmdshow); // update window updatewindow (hwnd); // message loop while (getmessage (& MSG, null, 0, 0) {translat Emessage (& MSG); // send the message to the window dispatchmessage (& MSG);} return MSG. wparam;} void drawbetiller (HDC, point apt []) {// call the system's drawing guide function (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, uint message, wparam, lparam) {HDC; pastruintct pS; int I, j; static int cxcl Ient, cyclient; static point apt [4]; Switch (Message) {Case wm_size: cxclient = loword (lparam); cyclient = hiword (lparam ); // The four points are the start point, the first control point, and the second control point. Terminal 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; return 0; Case wm_lbuttondown: Case wm_rbuttonup: Case wm_mousemove: // left-click or right-click can be drawn if (wparam & mk_lbutton | wparam & mk_rbutton) {HDC = getdc (hwnd); // use a white pen SelectObject (HDC, getstockobject (white_pen); // The user-defined function used to draw Bayesian curves ); // left-click the first control point if (wparam & mk_lbutton) {apt [1]. X = loword (lparam); Apt [1]. y = hiword (lparam);} // right-click to control the second control point if (wparam & mk_rbutton) {apt [2]. X = loword (lparam); Apt [2]. y = hiword (lparam);} // replace it with a custom paint brush SelectObject (HDC, createpen (ps_dash, 0, RGB (255, 0, 0 ))); // draw curve drawbezr (HDC, APT); releasedc (hwnd, HDC);} return 0; Case wm_paint: invalidaterect (hwnd, null, true); HDC = beginpaint (hwnd, true, & PS); // The purpose of this sentence is to have a curve drawbetiller (HDC, APT) at the beginning; endpaint (hwnd, & PS); Return 0; case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

In Windows, you can use the polybezr function to plot the besell curve.

Note: The Message Processing Method under wm_mousemove: first draw a curve with a white paint brush and then draw a curve with a black paint brush, so that the same effect is achieved: the first curve is actually the trajectory produced by moving the mouse, not the desired result. What we want is the curve when we finally move to the position, so we finally used our custom pen (here defined as red, dotted line) to draw a picture.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.