How to draw a straight line with a solid arrow in MFC

Source: Internet
Author: User
Tags cos sin

Work encountered in the flow of the project, need to draw a straight line with arrows, after groping, solve;

(1) two points (P1,P2) determine a line, with one end of the line (assuming P2) as the origin, set an angle

(2) the vector P2P1 (p) is obtained at the origin of the P2, the vector p rotates theta angle to get the vector P1, the vector p rotates-theta angle to get the vector P2

(3) Stretch vector to length, translate variable to end of line

(4) Now there are 3 points, draw the line can be

The specific code is as follows:

void Cworkflowdlg::D rawline (CPoint p1, CPoint p2) {CCLIENTDC dc (this);//Get Client window DC CPen pen,pen1,*oldpen; int penlinewidth=2;//to set the size of the arrows according to the line width pen.    CreatePen (Ps_solid, Penlinewidth, RGB (0, 0, 0)); Pen1.    CreatePen (Ps_solid, Penlinewidth, RGB (0, 0, 0)); Oldpen=dc.        SelectObject (&pen);    Double theta=3.1415926/15*penlinewidth;//converted to radians double px,py,p1x,p1y,p2x,p2y;    The vector p2p1 (P) px=p1.x-p2.x is obtained with P2 as the origin.    PY=P1.Y-P2.Y;    Vector p Rotates theta angle to get vector P1 P1x=px*cos (theta)-py*sin (theta);    P1y=px*sin (theta) +py*cos (theta);    Vector p rotates-theta angle to get vector P2 P2x=px*cos (-theta)-py*sin (-theta);    P2y=px*sin (-theta) +py*cos (-theta);    Stretch vector to length double x1,x2;    int length=10;    X1=SQRT (P1X*P1X+P1Y*P1Y);    p1x=p1x*length/x1;    p1y=p1y*length/x1;    X2=SQRT (P2X*P2X+P2Y*P2Y);    p2x=p2x*length/x2;    p2y=p2y*length/x2;    Shift the variable to the end of the line p1x=p1x+p2.x;    P1Y=P1Y+P2.Y;    p2x=p2x+p2.x;        P2Y=P2Y+P2.Y; dc.    MoveTo (P1.X,P1.Y); dc.    LineTo (P2.X,P2.Y); dc. SelectObject (&p en1); dc.    MoveTo (P2.X,P2.Y); dc.    LineTo (P1X,P1Y); dc.    MoveTo (P2.X,P2.Y); dc.    LineTo (P2X,P2Y); dc.    MoveTo (P1X,P1Y); dc.    LineTo (P2X,P2Y);    CPoint Ptvertex[3];    ptvertex[0].x = p2.x;    Ptvertex[0].y = p2.y;    ptvertex[1].x = p1x;    Ptvertex[1].y = p1y;    ptvertex[2].x = P2X;        Ptvertex[2].y = P2Y;      Fill Triangle Area CBrush br (RGB (40,130,170));     CRGN Rgn; Rgn.    CreatePolygonRgn (ptvertex,3,alternate); dc.      Fillrgn (&rgn, &BR); dc.    SelectObject (Oldpen); Br.      DeleteObject (); Rgn.   DeleteObject ();}

In this case, the CREATEPOLYONRGN function is used as follows:

BOOL Crgn::createpolygonrgn (lppoint lpPoints, int ncount, int nmode); [description] Creates an area surrounded by a series of points. Windows automatically connects the last point to the 1th when needed to close the polygon [parameter table]lppoint--------The first POINTAPI structure in Pointapi,ncount POINTAPI structure ncount---------Long, The polygon's number of points Npolyfillmode-Long, which describes the polygon fill pattern. Can be a alternate or winding constant. Npolyfillmode is alternate by default, mode alternate: It draws a ray horizontally from a point in the enclosing area to infinity, and the enclosing area is filled only when the ray crosses an odd number of border lines, which is not populated if even. , Mode winding: method, such as an odd number, fill the area, if the even is the direction of the border line to judge: if the border line through the border lines in different directions of equal numbers, then not filled, such as unequal, is filled. [Return value] Long, execution succeeds for the created zone handle, failure is 0

How to draw a straight line with a solid arrow in MFC

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.