Windows Programming (6): Basic Drawing

Source: Internet
Author: User
Tags polyline

There are three ways to draw images in Windows:

(1) You tell the system the coordinates and colors of the points. The system uses setpixel to draw the points. Similarly, get a certain pixel value through getpixel.

(2) Use movetoex and lineto to strip the line. movetoex sets the starting point coordinate, lineto sets the ending point coordinate, or uses the polyline function. This function accepts an array of the point type, use the dot line in the array.

(3) Windows provides some basic graphics rendering functions for us to call directly, such as rectangle, ellipse, and roundrect.

Next let's take a look at how to use them in the program: (or in the Windows Programming)

# 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 ("Drawing"); // 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_vre Draw; // specifies 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; // The registration window if (! Registerclass (& wndclass) {return-1 ;}// create a window hwnd = createwindow (szappname, // name of the window class, it must be a registered text ("My drawing"), // 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,) {translatemessage (& MSG); // send the message to the window dispatchmessage (& MSG);} return MSG. wparam;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {HDC; paintstruct pS; int I, j; static int cxclient, cyclient; point apt [num]; Switch (Message) {Case wm_size: cxclient = loword (lparam); cyclient = hiword (lparam); Return 0; Case wm_paint: HDC = beginpaint (hwnd, & PS); // draw the gradient color for (I = 0; I <500; I ++) {for (j = 0; j <26; j ++) {setpixel (HDC, 200 + I, 200 + J, RGB (I, j * 10, 0 ));}} * // dashes/* // dashes for (I = 0; I <500; I ++) {setpixel (HDC, 0 + I, 200, RGB (, 0);} * // use the function line movetoex (HDC, 0, cyclient/2, null); lineto (HDC, cxclient, cyclient/2); For (INT I = 0; I <num; I ++) {// divide the X axis into 1000 apt [I]. X = I * cxclient/num; Apt [I]. y = (INT) (cyclient/2 * (1-sin (twopi * I/num); // lineto (HDC, APT [I]. x, APT [I]. y);} // polyline rendering, which is faster than linetopolyline (HDC, APT, num) in the for loop; * // draw a rectangular rectangle (HDC, cxclient/8, cyclient/* cxclient/* cyclient/8); // draw the diagonal movetoex (HDC, null); lineto (HDC, cxclient, cyclient); movetoex (HDC, 0, cyclient, null); lineto (HDC, cxclient, 0); // draw the ellipse (HDC, cxclient/8, cyclient/* cxclient/* cyclient/8); // draw roundrect (HDC, cxclient/4, cyclient/* cxclient/* cyclient/4, // The last two parameters are the length and width of the oval corner formed by the rounded corner of the rectangle cxclient/4, cyclient/4); endpaint (hwnd, & PS); Return 0; Case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

There are several notes:

(1) In fact, an ellipse is drawn first by drawing a rectangle, and then calculated by an inner elliptic.

(2) When drawing a rectangle (elliptic), both the upper left and lower right corner of the drawing are specified. This is the same as the drawing software (drawing and Visio) we usually use, the method of drawing should be the same as here.

(3) The result of the program running causes the diagonal line to be blocked by an elliptic. If the order is changed and the diagonal line is drawn, no blocking will occur. This shows that the drawn graph is "solid" and cannot be simply understood as only a contour.

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.