Play Win32 development with me (10): Drawing (c)

Source: Internet
Author: User

Today, let's take a look at the functions used to fill the graph. Of course, I will not introduce them one by one, because we can simply set parameters by referring to msdn.

The setdcbrushcolor function is required. Its declaration is as follows:

 
Colorref setdcbrushcolor (_ in HDC, _ in colorref crcolor );

The second parameter is generated by using the RGB macro colorref and passed in. For example:

 
Setdcbrushcolor (PS. HDC, RGB (211,254, 41 ));

However, if you only call this function, you will find that the color of the paint brush remains unchanged during the drawing, because we have not selected the default image brush dc_brush of hbrush to DC. Therefore, before calling setdcbrushcolor, place the default image brush in the device context. The default image brush can be obtained through getstockobject (dc_brush.

 
SelectObject (PS. HDC, getstockobject (dc_brush ));

 

Next, we can try to fill in several images, such as rectangles, rectangles, and pie charts.

Case wm_paint: {beginpaint (hwnd, & PS); SelectObject (PS. HDC, getstockobject (dc_brush); setdcbrushcolor (PS. HDC, RGB (0, 0, 255); rectangle (PS. HDC, 20,18, 68,50); setdcbrushcolor (PS. HDC, RGB (, 70); rectangle (PS. HDC, 125,100,230,300); setdcbrushcolor (PS. HDC, RGB (30,235, 12); ellipse (PS. HDC, 390,223,); setdcbrushcolor (PS. HDC, RGB (35,160,242); chord (PS. HDC, 185,260,420,480,190,260,405,479); setdcbrushcolor (PS. HDC, RGB (211,254, 41); pie (PS. HDC, 35,320,300,600, 56,470, 60,360); endpaint (hwnd, & PS);} return 0;

Each call to setdcbrushcolor changes the paint brush color. For example, if you want to draw a blue rectangle, you must call setdcbrushcolor to modify the paint brush color before calling rectangle, and then draw a rectangle. Let's take a look at the aboveCode.

 

Next, let's make a simple drawing in the history of mankind.Program.

We provide several optional line styles for the program. You can choose from the menu, such as the truthful line and dotted line. After you press the left mouse button, the left mouse button pops up to draw a straight line. To simplify the process, we set the value of the corresponding menu ID to be consistent with the macro value of the linear type in createpen.

In this way, you can use the menu ID to create a paint brush, saving a lot of code.

 

Create a menu when responding to the wm_create message.

 
Case wm_create: {// create menu hmenu menubar = createmenu (); hmenu menupop = createpopupmenu (); appendmenu (menupop, mf_string, (uint_ptr) ps_solid, l ""); appendmenu (menupop, mf_string, (uint_ptr) ps_dash, l "dotted line"); appendmenu (menupop, mf_string, (uint_ptr) ps_dot, l "dotted line"); appendmenu, (uint_ptr) ps_dashdot, l "dotted line"); appendmenu (menubar, mf_string | mf_popup, (uint_ptr) menupop, l "select linear"); setmenu (hwnd, menubar );} return 0;

 

Now let's take a look at the general idea of creating a straight line.

1. Press the left mouse button to record the starting point of the line.

2. When the left mouse button pops up, record the end of the line and draw the entire line.

3. When the window is re-painted, all the lines in the front are cleared. to retain the lines in the front, you must respond to the wm_paint message and re-draw all lines.

4. Because we will draw multiple lines in the window, the program needs to define a struct to save the start, end, and line of the line.

5. Because you need to save data of multiple lines, you can put the data of each line in a vector.

 

According to the above analysis, the code for completing the program is as follows:

# Include <windows. h> # include <windowsx. h ># include <vector> using namespace STD; typedef struct tagdata {int ptbeginx INX, ptbeginy; // start point int ptendx, ptendy; // end point int penstyle; // paint brush linetype} paintdata; lresult callback windowproc (hwnd, uint MSG, wparam, lparam); int winapi winmain (hinstance hthisapp, hinstance hprevapp, lpstr lpscmd, int nshow) {wndclass WC ={}; WC. hbrbackground = createsolidbrush (RGB (0, 0, 0); WC. hinstance = hthisapp; WC. lpfnwndproc = windowproc; WC. lpszclassname = l "my"; WC. style = cs_hredraw | cs_vredraw; registerclass (& WC); hwnd = createwindow (L "my", l "application", ws_overlappedwindow, 50,20, 600,480, null, null, hthisapp, null); If (hwnd = NULL) Return-1; showwindow (hwnd, nshow); updatewindow (hwnd); MSG; while (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG);} return 0 ;} Lresult callback windowproc (hwnd, uint MSG, wparam, lparam) {static vector <paintdata> datas; static int penstyle = ps_solid; static paintdata * pcurrentdata = NULL; // pointer to current paintdata switch (MSG) {Case wm_create: {// create menu hmenu menubar = createmenu (); hmenu menupop = createpopupmenu (); appendmenu (menupop, mf_string, (uint_ptr) ps_solid, l ""); appendmenu (menupop, mf_string, (uint_ptr) ps_dash, l "Dotted line"); appendmenu (menupop, mf_string, (uint_ptr) ps_dot, l "dotted line"); appendmenu (menupop, mf_string, (uint_ptr) ps_dashdot, l "dotted line "); appendmenu (menubar, mf_string | mf_popup, (uint_ptr) menupop, l "select line type"); setmenu (hwnd, menubar);} return 0; Case wm_command: {// Add the hmenu mnbar = getmenu (hwnd); hmenu hmnpop = getsubmenu (mnbar, 0); checkmenuradioitem (hmnpop, ps_solid, ps_dashdot, loword (wparam), mf_bycommand); // record the user Selected linear penstyle = (INT) loword (wparam);} return 0; Case wm_lbuttondown: {pcurrentdata = new paintdata; // obtain the start point pcurrentdata-> penstyle = penstyle; pcurrentdata-> ptbegplugin = get_x_lparam (lparam); pcurrentdata-> ptbeginy = get_y_lparam (lparam);} return 0; Case wm_lbuttonup: {If (pcurrentdata! = NULL) {// obtain the terminal pcurrentdata-> ptendx = get_x_lparam (lparam); pcurrentdata-> ptendy = get_y_lparam (lparam); // draw the line HDC = getdc (hwnd ); hpen pen = createpen (pcurrentdata-> penstyle, 1, RGB (0,255, 0); Hpen oldpen = (Hpen) SelectObject (HDC, pen); movetoex (HDC, pcurrentdata-> ptbeginx, pcurrentdata-> browse, null); lineto (HDC, pcurrentdata-> ptendx, pcurrentdata-> ptendy); SelectObject (HDC, oldpen); deleteobject (PEN ); release DC (hwnd, HDC); // Add the current data to the vector datas. push_back (* pcurrentdata) ;}} return 0; Case wm_paint: {paintstruct pS; beginpaint (hwnd, & PS); // re-draw all lines by vector <paintdata>:: const_iterator item; For (item = datas. begin (); item! = Datas. end (); item ++) {Hpen pen = createpen (item-> penstyle, 1, RGB (0,255, 0); SelectObject (PS. HDC, pen); movetoex (PS. HDC, item-> ptbeginx, item-> ptbeginy, null); lineto (PS. HDC, item-> ptendx, item-> ptendy); deleteobject (PEN);} endpaint (hwnd, & PS);} return 0; Case wm_destroy: postquitmessage (0 ); return 0; default: Return defwindowproc (hwnd, MSG, wparam, lparam);} return 0 ;}

 

The structure paintdata is used to save the start, end, and line coordinates of each line. To prevent all data from being recycled after windowproc exists, you can use the static keyword to declare variables, so that the lifecycle of these variables is the same as that of the entire application.

After running the program, select a line type from the menu, and draw a line in the window.

 

 

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.