Windows program design (9): simple GDI object

Source: Internet
Author: User
Tags drawtext

The first thing to introduce is the paint brush.

Generally, the dashes we understand should be specified in the dashes function. How wide should I draw this line, and what is the type of the line (such as the dotted line, dotted line, and implementation. This is also done in many places, such as the open-source computer vision library opencv and Matlab. But windows programming is another way of thinking: I first design a paint brush, this pen determines the color, width, line type, and so on. Select the pen and draw a line. There are actually a lot of good points. If you are used to drawing in word or Visio, it is not difficult to understand the convenience of doing so.

For example:

# Include <windows. h> # include <math. h> # define num 1000 # define twopi (2*3.14159) # define logwide 4000 # define loghigh 3000 lresult callback wndproc (hwnd, uint, wparam, lparam ); int winapi winmain (hinstance, // current instance handle hinstance hprevinstance, // 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 changed in size Redraw window wndclass. style = cs_hredraw | cs_vredraw; // 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); // dish Single: wndclass. lpszmenuname = NULL; // window class name wndclass. lpszclassname = szappname; // register 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; Hpen; paintstruct pS; int I; 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); for (I = 0; I <256; I ++) {/* // creates a paint brush. The parameter is: paint style, paint width, paint Color Hpen = createpen (ps_solid, I, RGB (I, 300,200); SelectObject (HDC, Hpen); movetoex (HDC, null); lineto (HDC, (300,500); deleteobject (Hpen); sleep (10); */movetoex (HDC, 0, cyclient/2, null); lineto (HDC, cxclient, cyclient/2 ); movetoex (HDC, 0, cyclient/2, null); // draw a sine curve for (I = 0; I <num; I ++) {Hpen = createpen (ps_solid, (INT) (I/10), RGB (INT) (I/4), 0, 0); SelectObject (HDC, Hpen); 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); deleteobject (Hpen); // sleep (10) ;}} endpaint (hwnd, & PS); Return 0; Case wm_destroy: postquitmessage (0); Return 0 ;} return defwindowproc (hwnd, message, wparam, lparam );}

 

I still draw a sine curve, but I need to change the width and color of each line, including (I/10) and (I/4) only to make the width and color change not too fast.

Next, we will introduce the writing content drawtext.

First, you must understand that when writing drawtext in a window, pay attention to the color in 3: the first is the background color, which passes wndclass. hbrbackground = (hbrush) getstockobject (gray_brush); changed; second, the color of the words you wrote, changed by settextcolor (HDC, RGB (255, 0, 0; the third is the background color of the words you write, that is, the words you write occupy a square, and the background color of these grids. This is done through setbkcolor (HDC, RGB (128,128,128); to change. But in general, we set the background of the word to transparent setbkmode (HDC, transparent);, so that it changes with the color of the window. Let's take a look at this simple program:

# Include <windows. h> # include <math. h> 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 wndclass; // window style: when the window is moved or changed, the wndclass window is repainted. style = cs_hredraw | cs_vredraw; // specifies the callback function wndclass. lpfnwndproc = wndpr OC; // The extra bit is used to determine the location 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); // The background is gray wndclass. hbrbackground = (hbrush) getstockobject (gray_brush); // menu: No wndc Lass. lpszmenuname = NULL; // window class name wndclass. lpszclassname = szappname; // 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, 0, 0) {translate Message (& MSG); // send the message to the window dispatchmessage (& MSG);} return MSG. wparam;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {HDC; Hpen; paintstruct pS; int I; static int cxclient, cyclient; rect; Switch (Message) {Case wm_size: cxclient = loword (lparam); cyclient = hiword (lparam); Return 0; Case wm_paint: HDC = beginpaint (hwnd, & PS); // set the background color // setbkcolor (HDC, RGB (128,128,128); // set Background mode: transparent setbkmode (HDC, transparent); // sets the background mode: Opaque // setbkmode (HDC, opaque); // sets the color of the word settextcolor (HDC, RGB (255, 0, 0); // obtain the client region coordinate getclientrect (hwnd, & rect); drawtext (HDC, text ("Hello, windows7! "),-1, & rect, dt_singleline | dt_center | dt_vcenter); endpaint (hwnd, & PS); Return 0; Case wm_destroy: postquitmessage (0); Return 0 ;} return defwindowproc (hwnd, message, wparam, lparam );}

 

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.