原本的想法是用迴圈去判斷,然後就開始設計算法,瞎搞了幾個小時,結果就是慢到不行,後來發現用這個方式,幾行就搞定了,真不知到昨天在頭痛什麼的-_-|||。
方法很簡單,幾個步驟而已,看下去吧。
Step1:
在OnPaint裡面畫一個三角型先。
CPoint pt[5];
pt[0] = CPoint(20,20);/// move to x=20,y=20
pt[1] = CPoint(100,20);//line at x = 100, y=20
pt[2] = CPoint(20,20);/// move to x=20,y=20
pt[3] = CPoint(20,100);//line at x = 20, y=100
pt[4] = CPoint(100,20);/// ;line to
// Create the region (polygon)
// CRgn region; ---> put in member
region.CreatePolygonRgn(pt, 5, ALTERNATE);
// Create a blue brush
CBrush blueBrush;
blueBrush.CreateSolidBrush(RGB(0, 0, 255));
// Fill the polygon
pDC->FillRgn(®ion, &blueBrush);
這裡要注意,我們要把Region記起來
Step2:
然後我們到OnLButtonDown裡面,一行搞定。
//WINGDIAPI BOOL WINAPI PtInRegion(__in HRGN hrgn, __in int x, __in int y);
//return type is BOOL .
BOOL b = PtInRegion(region,point.x,point.y);
if (b) // Y
else // N
that's all .
----------------------------------------
//A
CPoint pt[5];
pt[0] = CPoint(0,0);/// move to x=20,y=20
pt[1] = CPoint(w,0);//line at x = 100, y=20
pt[2] = CPoint(0,0);/// move to x=20,y=20
pt[3] = CPoint(w/2,h/2);//line at x = 20, y=100
pt[4] = CPoint(w,0);/// ;line to
// Create the region (polygon)
regionA.CreatePolygonRgn(pt, 5, ALTERNATE);
// Create a blue brush
CBrush blueBrush;
blueBrush.CreateSolidBrush(RGB(0, 0, 255));
// Fill the polygon
pDC->FillRgn(®ionA, &blueBrush);
//B
pt[0] = CPoint(0,0);/// move to x=20,y=20
pt[1] = CPoint(0,h);//line at x = 100, y=20
pt[2] = CPoint(0,0);/// move to x=20,y=20
pt[3] = CPoint(w/2,h/2);//line at x = 20, y=100
pt[4] = CPoint(0,h);/// ;line to
// Create the region (polygon)
regionB.CreatePolygonRgn(pt, 5, ALTERNATE);
// Create a blue brush
CBrush RedBrush;
RedBrush.CreateSolidBrush(RGB(255, 0, 0));
// Fill the polygon
pDC->FillRgn(®ionB, &RedBrush);
//C
pt[0] = CPoint(0,h);/// move to x=20,y=20
pt[1] = CPoint(w,h);//line at x = 100, y=20
pt[2] = CPoint(0,h);/// move to x=20,y=20
pt[3] = CPoint(w/2,h/2);//line at x = 20, y=100
pt[4] = CPoint(w,h);/// ;line to
// Create the region (polygon)
regionC.CreatePolygonRgn(pt, 5, ALTERNATE);
// Create a blue brush
CBrush GreenBrush;
GreenBrush.CreateSolidBrush(RGB(0, 255, 0));
// Fill the polygon
pDC->FillRgn(®ionC, &GreenBrush);
//D
pt[0] = CPoint(w,0);/// move to x=20,y=20
pt[1] = CPoint(w,h);//line at x = 100, y=20
pt[2] = CPoint(w,0);/// move to x=20,y=20
pt[3] = CPoint(w/2,h/2);//line at x = 20, y=100
pt[4] = CPoint(w,h);/// ;line to
// Create the region (polygon)
regionD.CreatePolygonRgn(pt, 5, ALTERNATE);
// Create a blue brush
CBrush YellowBrush;
YellowBrush.CreateSolidBrush(RGB(255, 255, 0));
// Fill the polygon
pDC->FillRgn(®ionD, &YellowBrush);