MFC中实现的画箭头算法 (Arrow in MFC)
生活随笔
收集整理的這篇文章主要介紹了
MFC中实现的画箭头算法 (Arrow in MFC)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在codeproject中尋找到一個這樣的算法,在這里介紹一下 //使用一個結構體來存儲相關的信息 //Defines the attributes of an arrow. typedef struct tARROWSTRUCT { int nWidth;? ? ?// width (in pixels) of the full base of the arrowhead float fTheta;? ?// angle (in radians) at the arrow tip between the two // ?sides of the arrowhead bool bFill;? ? ?// flag indicating whether or not the arrowhead should be // ?filled } ARROWSTRUCT; /// //函數聲明 // Draws an arrow, using the current pen and brush, from the current position // ?to the passed point using the attributes defined in the ARROWSTRUCT. void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow); void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow); /// //畫箭頭函數實現 void CMyDialog::ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pA) { POINT ptTo = {x, y}; ArrowTo(hDC, &ptTo, pA); } void CMyDialog::ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pA) { POINT pFrom; POINT pBase; POINT aptPoly[3]; float vecLine[2]; float vecLeft[2]; float fLength; float th; float ta; // get from point MoveToEx(hDC, 0, 0, &pFrom); // set to point aptPoly[0].x = lpTo->x; aptPoly[0].y = lpTo->y; // build the line vector vecLine[0] = (float) aptPoly[0].x - pFrom.x; vecLine[1] = (float) aptPoly[0].y - pFrom.y; // build the arrow base vector - normal to the line vecLeft[0] = -vecLine[1]; vecLeft[1] = vecLine[0]; // setup length parameters fLength = (float) sqrt(vecLine[0] * vecLine[0] + vecLine[1] * vecLine[1]); th = pA->nWidth / (2.0f * fLength); ta = pA->nWidth / (2.0f * (tanf(pA->fTheta) / 2.0f) * fLength); // find the base of the arrow pBase.x = (int) (aptPoly[0].x + -ta * vecLine[0]); pBase.y = (int) (aptPoly[0].y + -ta * vecLine[1]); // build the points on the sides of the arrow aptPoly[1].x = (int) (pBase.x + th * vecLeft[0]); aptPoly[1].y = (int) (pBase.y + th * vecLeft[1]); aptPoly[2].x = (int) (pBase.x + -th * vecLeft[0]); aptPoly[2].y = (int) (pBase.y + -th * vecLeft[1]); MoveToEx(hDC, pFrom.x, pFrom.y, NULL); // draw we're fillin'... if(pA->bFill) { LineTo(hDC, aptPoly[0].x, aptPoly[0].y); Polygon(hDC, aptPoly, 3); } // ... or even jes chillin'... else { LineTo(hDC, pBase.x, pBase.y); LineTo(hDC, aptPoly[1].x, aptPoly[1].y); LineTo(hDC, aptPoly[0].x, aptPoly[0].y); LineTo(hDC, aptPoly[2].x, aptPoly[2].y); LineTo(hDC, pBase.x, pBase.y); MoveToEx(hDC, aptPoly[0].x, aptPoly[0].y, NULL); } } / //封裝調用函數實現(其實還是有很大的擴展空間的) void CMyDialog::ArrowTo( CDC *pDC,? ? ? ?? ? ? ?//畫刷 CPoint point, ? ? ? ? ?//終點坐標 int nPenStyle, ?? ? ? ?//線樣式 int nPenWidth, ?? ? ? ?//線寬度 COLORREF color,?//顏色 int nWidth,? ? ?? ? ? ?//三角形底邊寬度 float fTheta,? ?? ? ? ?//三角形頂角角度 bool bFill? ? ? ? ? ? ?//是否填充顏色 ) { ARROWSTRUCT a; a.nWidth = nWidth;? ? ?//三角形底邊寬度 a.fTheta = fTheta;? ? ?//三角形頂角角度 a.bFill = bFill;? ? ? ?//是否填充顏色 CPen* pOldPen; CPen pen(nPenStyle,nPenWidth,color); pOldPen = pDC->SelectObject(&pen); CBrush br,*pbrOld; br.CreateSolidBrush(color); pbrOld = pDC->SelectObject(&br); ArrowTo(*pDC,point.x,point.y,&a);? ? ? ?//調用畫箭頭函數 pDC->SelectObject(pOldPen); pDC->SelectObject(pbrOld); }
可以改變三角形大小,頂點角度,是否填充和填充顏色等
但是畫出的箭頭還是不夠美觀....呵呵,還好吧
其中填充是代表箭頭內是否填充顏色?
先來看聲明和實現
?
?
再來看調用實現(加一層封裝更加適用)
?
?
?
轉載于:https://www.cnblogs.com/wupingzsf/archive/2010/05/16/1736958.html
總結
以上是生活随笔為你收集整理的MFC中实现的画箭头算法 (Arrow in MFC)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常州的照片
- 下一篇: 一起来讨论web2.0吧