Go into the world of Windows programming-----drawing related

Source: Internet
Author: User

Windows drawing

1 Drawing Drawing

1.1 How graphics are plotted
Gets the drawing handle-device Description table (DC), using the appropriate drawing API, to draw the graphic on the device.

1.2 Colors
r\g\b three colors, each color 8 bits, a total of 24 bit color.
32-bit color: The number of colors 24-bit color, the extra 8 bits represent grayscale.
16 bits: 16 times the number of colors 2.

Win32, color definition colorref (DWORD), RGB macro definition Color
COLORREF ncolor = RGB (0, 0, 0);
COLORREF Ncolor = RGB (255,255,255);
COLORREF Ncolor = RGB (255, 0, 0);
Get RGB tri-color from one color:
int nblue = Getbvalue (Ncolor);
int nred = Getrvalue (Ncolor);
int ngreen= getgvalue (ncolor);


Here is the specific code

WinDraw.cpp.cpp: Defines the entry point for the application. #include "stdafx.h" #include "windraw.cpp.h" #include <stdio.h> #define max_loadstring 100//Global variables: hinstance                            hinst;//Current instance Tchar sztitle[max_loadstring];//title bar text tchar szwindowclass[max_loadstring];//main window class name int g_ndrawtype = 0;         Drawing command Colorref G_npencolor = RGB (0, 0, 0);            default brush color int g_npenstyle = Ps_solid;                      Default brush type int g_npenwdith = 1;   Default Brush width colorref g_nbrushcolor = RGB (255, 255, 255); Default brush color//forward declaration of functions contained in this code module: Atommyregisterclass (hinstance hinstance); Boolinitinstance (HINSTANCE, int.); LRESULT Callbackwndproc (hwnd, UINT, WPARAM, LPARAM); Int_ptr Callbackabout (hwnd, UINT, WPARAM, LPARAM); INT apientry _                     Twinmain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpCmdLine, int ncmdshow) {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine); TODO: Place the code here. MSG msg; Haccel HACCELTABLe;//initialization of global string LOADSTRING (HInstance, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_windrawcpp, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization: if (! InitInstance (HINSTANCE, nCmdShow)) {return FALSE;} hacceltable = Loadaccelerators (hinstance, Makeintresource (idc_windrawcpp));//main message loop: while (GetMessage (&msg, NULL , 0, 0)) {if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg);D ispatchmessage (&msg);}} return (int) Msg.wparam;} Function: MyRegisterClass ()////Purpose: Registers the window class. Note:////This function and its usage are only required if you want//This code is compatible with the WIN32 system that was added to the "registerclassex"//function in Windows 95. It is important to call this function,//so that the application can get the associated//"well-formed" small icon. ATOM MyRegisterClass (hinstance hinstance) {wndclassex wcex;wcex.cbsize = sizeof (wndclassex); wcex.style= CS_HREDRAW | cs_vredraw;wcex.lpfnwndproc= wndproc;wcex.cbclsextra= 0;wcex.cbwndextra= 0;wcex.hinstance= hInstance;wcex.hIcon= LoadIcon (HInstance, Makeintresource (idi_windrawcpp)); wcex.hcursor= loadcursor (NULL, idc_arrow); wcex.hbrbackground= (Hbrush) (color_window+1);/* Loading menu items can also load menus when creating Windows */wcex.lpszmenuname= Makeintresource (idc_windrawcpp); wcex.lpszclassname= szwindowclass;wcex.hiconsm= LoadIcon (wcex.hInstance, Makeintresource (Idi_small)); return RegisterClassEx (&wcex);} Function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////Note:////in this function, we save the instance handle in the global variable and//create and display the master thread The Sequence window.   BOOL InitInstance (hinstance hinstance, int ncmdshow) {HWND hwnd; HInst = hinstance; Store instance handles in global variables hWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, 0, Cw_usedefault,   0, NULL, NULL, HINSTANCE, NULL);   if (!hwnd) {return FALSE;   } ShowWindow (HWnd, ncmdshow);   UpdateWindow (HWND); return TRUE;} void OnCommand (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent;//command idwmid = LoWord (WPARAM); WmE Vent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {case id_32774://draw point case id_32775:case id_32772:case id_32776://Draw arc caseid_32777://draw a polyline case id_32780://draw a curve case id_32781://draw a multi-style curve case id_32782://draw a rectangle case id_32783://draw a circle case id_32785:// Draw a pie case id_32786://paint a id_32787://draw polygon//save drawing type G_ndrawtype = Wmid;invalidaterect (hwnd,null,true);//Refresh window, Will trigger wm_paintbreak;case id_32790://draw Brush Red//Save drawing type G_npencolor = RGB (255,0,0), InvalidateRect (hwnd,null,true);//Refresh Window , the wm_paintbreak;case id_32792://brush style is triggered G_npenstyle = Ps_solid;invalidaterect (hwnd,null,true);//Refresh window, trigger wm_ Paintbreak;case id_32794://brush Style G_npenstyle = Ps_dash;invalidaterect (hwnd,null,true);//Refresh window, trigger Wm_paintbreak;case id_32796://brush Style G_npenwdith = 1;invalidaterect (hwnd,null,true);//Refresh window, trigger wm_paintbreak;case id_32797://brush Style g_ Npenwdith = 5;invalidaterect (hwnd,null,true);//Refresh window, trigger wm_paintbreak;case id_32799://paint brush Color G_nbrushcolor = RGB ( 255,0,0); InvalidateRect (hwnd,null,true);//refreshes the window, triggering wm_paintbreak;case idm_about:dialogbox (HInst, Makeintresource ( Idd_aboutbox), hwnd, about); Break;case Idm_exit:destroywindow (HWND); Break;default:defwindowproc (hwnd, message, WParam, LParam); Break;}}     Dot draw void Drawpixel (hdc hdc) {COLORREF Ncolor = RGB (255,0,0);/* Draw: COLORREF setpixel (* hdc hdc,//DC handle * int X,//x coordinates * int Y,//y coordinates * COLORREF crcolor); The color of the dots */setpixel (hdc,100,100,ncolor);}            void Getpixelcolor (hdc hdc) {Drawpixel (HDC);/* Get point color * get: COLORREF GetPixel (* hdc hdc,//DC handle * int XPos,//x coordinates * int nypos);    Y coordinate * Returns the color of the point at the specified coordinate position */COLORREF Ncolor = GetPixel (HDC, 100, 100);    Gets the color of the tri-color value int nred = Getrvalue (Ncolor);    int ngreen = Getgvalue (Ncolor);    int nblue = Getbvalue (Ncolor);    CHAR sztext[260] = {0};sprintf_s (sztext, "color=%08x, red=%d green=%d blue=%d", Ncolor, nred, Ngreen, Nblue); MessageBox (NULL, Sztext, "Getpixelcolor", MB_OK);}    Draw a straight line void DrawLine (hdc hdc) {MOVETOEX (hdc, 0, 0, NULL);    LineTo (HDC, 500, 500);    Movetoex (HDC, 0, NULL); LineTo (HDC, 0, 500);} //Draw arc void DrawArc (HDC hdc) {//Counter-clockwise way Setarcdirection (hdc, ad_counterclockwise); Through the bounding rectangle and cutting line/*bool ARC (HDC hdc, * int nleftrect,//bounding rectangle coordinates * int ntoprect,//bounding rectangle coordinates * int nrightrect,//bounding rectangle coordinates * int nbottomrect,//bounding rectangle coordinates * int nxstartarc,//start cut radius x coordinate * int nystartarc,//start cut radius y coordinate * int nxendarc ,//End cut radius x coordinate * int nyendarc); X-coordinate for terminating cutting radius * You can use the Setarcdirection function to set the ARC function * Cutting direction: Clockwise and Counterclockwise * * ARC (HDC, 400, 200, 500, 300,500, 200, 400, 20    0);    Clockwise Way Setarcdirection (HDC, ad_clockwise); ARC (HDC, 500, 200, 600, 300,600, 200, 500, 200);/* There will be 0, 0 to 200,200 a straight line */movetoex (HDC, N, +, NULL);/* Another square that draws a circle method, *bool Anglearc (* HDC hdc,//handle to device context * int x,///center X coordinate * int y,//Y coordinate of center point * DW ORD dwradius,//Circle radius * Float estartangle,//start angle * float esweepangle);//Angle */anglearc (hdc,200,200,100,60,120); L Ineto (HDC, 200, 200);} Draw polyline void DrawPolyline (hdc hdc) {Point Ptpolyline[7] = {0};    ptpolyline[0].x = 100;    PTPOLYLINE[0].Y = 100;    ptpolyline[1].x = 200;    PTPOLYLINE[1].Y = 100;    ptpolyline[2].x = 200;    PTPOLYLINE[2].Y = 200;    ptpolyline[3].x = 300;    PTPOLYLINE[3].Y = 200;    ptpolyline[4].x = 300;    PTPOLYLINE[4].Y = 300;    ptpolyline[5].x = 400;    PTPOLYLINE[5].Y = 300;    ptpolyline[6].x = 400; PTPOLYLINE[6].Y = 400;/* *bool Polyline (* hdc hdc,//DC handle * CONST point *lppt,//polyline Vertex coordinate array * int C Points); The length of the vertex array * PolylineTo is similar to Polyline, just before drawing Polyline *, using lineto from the current point to draw a straight line to the first vertex of Polyline */Polyline (HDC, Ptpolyline,    7);  PolylineTo (HDC, Ptpolyline, 3);/* * Draw multiple sets of polylines Polypolyline * BOOL polypolyline (hdc hdc, * CONST point) *lppt,//array of all points * CONST DWORD *lpdwpolypoints,//number of points per set * DWORD ccount);//number of groupings *///dword Ngro    Up[] = {4, 3}; Polypolyline (HDC, Ptpolyline,ngroup, 2);}    void Drawbizer (hdc hdc) {Point Ptbizer[7] = {0}; ptbizer[0].x = 100;  Endpoint  PTBIZER[0].Y = 100; ptbizer[1].x = 100;  Control point ptbizer[1].y = 50; ptbizer[2].x = 300; Control point ptbizer[2].y = 150; ptbizer[3].x = 300; Endpoint ptbizer[3].y = 100; ptbizer[4].x = 300;    Control point ptbizer[4].y = 400; ptbizer[5].x = 400;    Control point ptbizer[5].y = 200; ptbizer[6].x = 500;    Endpoint PTBIZER[6].Y = 300; /* Bezier curve BOOL polybezier (hdc hdc, CONST Point *lppt,//points Group, Min. 4 points DWORD cpoints);//number of points 4 points: 1   and 4 is the endpoint, 2.3 points is the control point 7 points: 1.4.7 is the endpoint, the rest is the control point */Polybezier (HDC, Ptbizer, 7);   Movetoex (HDC, ptbizer[0].x, Ptbizer[0].y, NULL);   LineTo (HDC, ptbizer[1].x, PTBIZER[1].Y);   Movetoex (HDC, ptbizer[3].x, Ptbizer[3].y, NULL); LineTo (HDC, ptbizer[2].x, ptbizer[2].y);}    Multi-style line void Drawpolydraw (hdc hdc) {Point Ptdraw[4] = {0};    ptdraw[0].x = 100;    PTDRAW[0].Y = 100;    ptdraw[1].x = 200;    PTDRAW[1].Y = 100;    ptdraw[2].x = 200;    PTDRAW[2].Y = 200;    ptdraw[3].x = 300;    PTDRAW[3].Y = 200; BYTE Pttype[4] = {0};    Pttype[0] = Pt_moveto;    PTTYPE[1] = pt_lineto;pttype[2] = Pt_lineto; PTTYPE[3] = pt_lineto;/** BOOL Polydraw (HDC hdc, * const point *lppt,//array of various points * Const BYTE *lpbtypes,// How to draw from a point to the next point pointer to line and curve identifiers * int ccount);    Number of points * * Lpbtypes-pt_moveto move to that point * pt_lineto draw line * Pt_bizerto Biezer curve */Polydraw (HDC, Ptdraw, Pttype, 4);}    void DrawRect (HDC hdc) {//Rectangle Rectangle (hdc, 100, 100, 200, 200); Rectangle with Rounded rectangle///rectangular BOOL roundrect with rounded corners (hdc hdc, int nleftrect,//upper-left x-coordinate int ntoprect,///upper-left y-coordinate int nrig htrect,//Right-bottom x-coordinate int nbottomrect,//right-bottom y-coordinate int nwidth,//The width of the ellipse that generated the fillet int nheight);//The height of the ellipse that generated the fillet */Roun Drect (HDC, 300, 100, 400, 200, 50, 50);}        void DrawEllipse (HDC hdc) {//round/*bool Ellipse (hdc hdc, int nleftrect,//bounding rectangle left upper x coordinate int ntoprect,//bounding rectangle left upper y-coordinate int Nrightrect,//bounding rectangle right bottom x coordinate int nbottomrect); Tangential momentbottom right y coordinate */Ellipse (HDC, 100, 100, 200, 200); Oval Ellipse (HDC, 300, 100, 500, 200);} void Drawpie (hdc hdc) {Pie (hdc, 100, 100, 500, 400, 500, 100, 100, 100);} void Drawchord (HDC hdc) {/* String * BOOL Chord (HDC hdc, * int nleftrect,//bounding rectangle left upper x coordinate * int ntoprect,/ /bounding rectangle left upper y coordinate * int nrightrect,//bounding rectangle right bottom x coordinate * int nbottomrect,//bounding rectangle right lower y coordinate * int nxradial1,//cut start radius x coordinates * int nyradial1,//cut start radius y coordinate * int nxradial2,//cut END radius x coordinate * int nYRadial2)//cut end radius y coordinate */Cho RD (HDC, 100, 100, 500, 400, 500, 100, 100, 100);}    void Drawploygon (hdc hdc) {Point Ptploygon[4] = {0};    ptploygon[0].x = 100;    PTPLOYGON[0].Y = 100;    ptploygon[1].x = 200;    PTPLOYGON[1].Y = 100;    ptploygon[3].x = 600;    PTPLOYGON[3].Y = 300;    ptploygon[2].x = 500; PTPLOYGON[2].Y = 300;/* Polygon * BOOL Polygon (hdc hdc, * CONST point *lppoints,//vertex of polygon * int ncount); Number of vertices * Polypolygon can draw multiple sets of polygons */Polygon (HDC, Ptploygon, 4);} void OnPaint (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc; Paintstruct PS;HDC = BeginPaint (HWND,&AMP;PS);/* Create brush * hpen CreatePen (* int fnpenstyle,//Brush style * int nwidth,// Brush Width * COLORREF crcolor);//Brush color */hpen hpen = CreatePen (G_npenstyle,g_npenwdith,g_npencolor);/* Set Brush to current DC * Hgdiobj S Electobject (* HDC hdc,//Handle of current DC * hgdiobj hgdiobj);//The GDI object handle to use * Returns the same type of GDI object handle that the current DC used originally. */hpen hOldPen = (hpen) SelectObject (Hdc,hpen);//Create brush, brush only effect on filler, circle, polygon, rectangle, etc. hbrush hbrush = CreateSolidBrush (g_ Nbrushcolor);//Set the brush to the current Dchbrush hOldBrush = (hbrush) SelectObject (Hdc,hbrush);//Depending on the drawing type, make a different command switch (g_ndrawtype {case id_32774://Draw point Drawpixel (HDC); break;case id_32775://get point color Getpixelcolor (HDC); Break;case id_32772:// Draw Line DrawLine (hdc); break;case id_32776://Draw Arc DrawArc (HDC); Break;case id_32777://draw polyline DrawPolyline (hdc); break;case id_32780://Draw Curve Drawbizer (hdc); Break;case id_32781://draw multi-style curve Drawpolydraw (hdc); Break;casE id_32782://Draw Rectangle DrawRect (hdc); Break;case id_32783://Draw Circle and Ellipse DrawEllipse (hdc); Break;case id_32785://Draw Pie Drawpie (hdc ); Break;case id_32786://draw Drawchord (HDC); Break;case id_32787://Draw Polygon Drawploygon (hdc); break;default:break;}    Remove Paint brush SelectObject (HDC, hOldBrush);    Remove Paint brush DeleteObject (hbrush);    Remove the brush, put the old brush in, and the new brush will release SelectObject (HDC, hOldPen); Destroy Brush DeleteObject (hpen); EndPaint (HWND,&AMP;PS);} Functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Handles the message of the main window. wm_command-processing Application Menu//wm_paint-Draw main window//wm_destroy-send exit message and return////lresult CALLBACK WndProc (HWND hwnd, UINT message , WPARAM WPARAM, LPARAM LPARAM) {switch (message) {Case wm_command:/* menu information Processing */oncommand (Hwnd,message,wparam,lparam); If 0wmId = LoWord (wParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {case Idm_about:dialogbox (HInst, Makeintresou RCE (Idd_aboutbox), hwnd, about); Break;case Idm_exit:destroywindow (hwnd); Break;default:return DefWindowProc (hwnd, Message, WParam, LParam);} #endifbreak; CASe wm_paint:/* drawing instruction */onpaint (hWnd, message, WParam, LParam); Break;case wm_destroy:postquitmessage (0); Break;default: Return DefWindowProc (hWnd, message, WParam, LParam);} return 0;} The message handler for the About box. INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {case Wm_initdialog:return (INT_PTR) true;case wm_command:if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL) {EndDialog (hdlg, LoWord (WParam)); return (INT_PTR) TRUE;} break;} Return (INT_PTR) FALSE;}


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.