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 Customer window Dccpen pen,pen1,*oldpen;int Penlinewi dth=2;//to set the size of the arrows according to the line width pen. CreatePen (Ps_solid, Penlinewidth, RGB (0, 0, 0));p en1. CreatePen (Ps_solid, Penlinewidth, RGB (0, 0, 0)); Oldpen=dc. SelectObject (&pen);d ouble theta=3.1415926/15*penlinewidth;//convert 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 p1p1x=px*cos (theta)-py*sin (theta); P1y=px*sin (theta) +py*cos (theta);//vector p rotation-theta angle get vector p2p2x=px*cos (-theta)-py*sin (-theta); P2y=px*sin (-theta) +py*cos (-theta);//scaling 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;//the translation 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);d C. LineTo (P2.X,P2.Y);d C. SelectObject (&PEN1);d C. MoveTo (P2.X,P2.Y);d C. LineTo (p1x,p1y);d C. MoveTo (P2.X,P2.Y);d C. LineTo (P2X,P2Y);d C. MoveTo (p1x,p1y);d C. LineTo (P2X,P2Y); CPoint Ptvertex[3];p tvertex[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);d C. 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
The specific results are as follows:
How to draw a straight line with a solid arrow in MFC