1、#define Round(d) int(floor(d+0.5)/四舍五入宏定义#ifdef _DEBUG#define new DEBUG_NEW#endif/ CChildViewCChildView:CChildView()CChildView()BEGIN_MESSAGE_MAP(CChildView, CWnd) ON_WM_PAINT() ON_WM_CREATE() ON_COMMAND(ID_DRAW_PIC, &OnDrawPic)END_MESSAGE_MAP()/ CChildView 消息处理程序BOOL CChildView:PreCreateWindow(CREA
2、TESTRUCT& cs) if (!CWnd:PreCreateWindow(cs) return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE; cs.style &= WS_BORDER; cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, :LoadCursor(NULL, IDC_ARROW), reinterpret_cast(COLOR_WINDOW+1), NULL); return TRUE;void CChildView:OnPaint() CPaintD
3、C dc(this); / 用于绘制的设备上下文 / TODO: 在此处添加消息处理程序代码 DrawGraph(); / 不要为绘制消息而调用CWnd:OnPaint()ReadPoint() /点表 P0.x = 50; P0.y = 100; P1.x = -150;P1.y = 300; P2.x = -250;P2.y = 50; P3.x = -150;P3.y = -250; P4.x = 0; P4.y = -50; P5.x = 100; P5.y = -250; P6.x = 300; P6.y = 150;DrawPolygon(CDC *pDC) /绘制多边形边界 CL
4、ine *line = new CLine; CP2 t; for(int i = 0; i MoveTo(pDC, Pi); t = Pi; elseLineTo(pDC, Pi); line-LineTo(pDC, t); /闭合多边形 delete line;DrawGraph() /绘制图形 CRect rect; /定义客户区 GetClientRect(&rect); /获得客户区的大小 CDC *pDC = GetDC(); /定义设备上下文指针 pDC-SetMapMode(MM_ANISOTROPIC); /自定义坐标系SetWindowExt(rect.Width(), r
5、ect.Height(); /设置窗口比例SetViewportExt(rect.Width(), -rect.Height(); /设置视区比例,且x轴水平向右,y轴垂直向上SetViewportOrg(rect.Width() / 2, rect.Height() / 2); /设置客户区中心为坐标系原点 rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2); /矩形与客户区重合 if(!bFill) DrawPolygon(pDC); /绘制多边形 else FillPolygon(pDC); /填充多边形 ReleaseDC(pD
6、C); /释放DCFillPolygon(CDC *pDC) /填充多边形 i+) /转储顶点坐标,y坐标取为整数 P1i.x = Pi.x; P1i.y = Round(Pi.y); P1i.c = CRGB(bRed / 255.0, bGreen / 255.0, bBlue / 255.0); CFill *fill = new CFill; /动态分配内存 fill-SetPoint(P1, 7); /初始化Fill对象CreateBucket(); /建立桶表CreateEdge(); /建立边表Gouraud(pDC); /填充多边形 delete fill; /撤销内存int
7、CChildView:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CWnd:OnCreate(lpCreateStruct) = -1) return -1; 在此添加您专用的创建代码 bFill = FALSE; ReadPoint(); return 0;OnDrawPic() 在此添加命令处理程序代码 COLORREF GetClr = RGB(0, 0, 0); /调色板颜色 CColorDialog ccd(GetClr, CC_SOLIDCOLOR); if(IDOK = ccd.DoModal() /调用颜色对话框选取填充色 GetCl
8、r = ccd.GetColor(); return; bRed = GetRValue(GetClr); /获取红色分量 bGreen = GetGValue(GetClr); /获取绿色分量 bBlue = GetBValue(GetClr); /获取蓝色分量 bFill = TRUE; Invalidate(); 2使用MFC技术实现多边形边缘填充算法,参考界面效果如下:/ demoView.cpp : CdemoView 类的实现demoDoc.hdemoView.h/ CdemoViewIMPLEMENT_DYNCREATE(CdemoView, CView)BEGIN_MESSAG
9、E_MAP(CdemoView, CView) / 标准打印命令 ON_COMMAND(ID_FILE_PRINT, &CView:OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, & ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CdemoView:OnFilePrintPreview)/ CdemoView 构造/析构CdemoView() 在此处添加构造代码CdemoView()BOOL CdemoView: cs) 在此处通过修改 / CREATESTRUCT cs 来修改窗口类或样式 return CView:PreC
10、reateWindow(cs);/ CdemoView 绘制void CdemoView:OnDraw(CDC* /*pDC*/) CdemoDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);pDoc) 在此处为本机数据添加绘制代码/ CdemoView 打印OnFilePrintPreview() AFXPrintPreview(this);OnPreparePrinting(CPrintInfo* pInfo) / 默认准备 return DoPreparePrinting(pInfo);OnBeginPrinting(CDC* /*pDC*/,
11、CPrintInfo* /*pInfo*/) 添加额外的打印前进行的初始化过程OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 添加打印后进行的清理过程OnRButtonUp(UINT nFlags, CPoint point) ClientToScreen(&point); OnContextMenu(this, point);OnContextMenu(CWnd* pWnd, CPoint point) theApp.GetContextMenuManager()-ShowPopupMenu(IDR_POPUP_EDIT, point.x
12、, point.y, this, TRUE);ReadPoint() /点表DrawPolygon(CDC *pDC) i+) /计算多边形边界 if(Pi.x MaxX) MaxX = Pi.x; if(Pi.x MaxY) MaxY = Pi.y; if(Pi.y MinY) MinY = Pi.y; i+) /绘制多边形 /闭合多边形MoveTo(pDC, CP2(MinX, MinY); /绘制包围盒LineTo(pDC, CP2(MinX, MaxY);LineTo(pDC, CP2(MaxX, MaxY);LineTo(pDC, CP2(MaxX, MinY);LineTo(pDC
13、, CP2(MinX, MinY);FillPolygon(CDC *pDC) COLORREF BClr = RGB(255, 255, 255); /背景色 COLORREF FClr = GetClr; /填充色 int ymin, ymax; /边的最小y值与最大y值 double x, y, k; /x,y当前点,k斜率的倒数 i+) /循环多边形所有边 int j = (i + 1) % 7; k = (Pi.x - Pj.x) / (Pi.y - Pj.y); /计算/k Pj.y) /得到每条边y的最大值与最小值 ymin = Round(Pi.y); ymax = Round
14、(Pj.y); x = Pi.x; /得到x|ymin ymin = Round(Pj.y); ymax = Round(Pi.y); x = Pj.x; for(y = ymin; y ymax; y+) /沿每一条边循环扫描线 for(int m = Round(x); m GetPixel(m, Round(y) /如果是填充色 pDC-SetPixelV(m, Round(y), BClr); /置为背景色 elseSetPixelV(m, Round(y), FClr); /置为填充色 x += k; /计算下一条扫描线的x起点坐标DrawGraph() /绘制图形 /定义客户区 /
15、获得客户区的大小 /绘制多边形 /填充多边形 /释放DC/ CdemoView 诊断AssertValid() const CView:AssertValid();Dump(CDumpContext& dc) constDump(dc);CdemoDoc* CdemoView:GetDocument() const / 非调试版本是内联的 ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CdemoDoc); return (CdemoDoc*)m_pDocument;#endif /_DEBUG/ CdemoView 消息处理程序OnInitialUpdate()OnInitialUpdate(); 在此添加专用代码和/或调用基类 GetClr = RGB(0,0,0); MinX = MaxX = P0.x; MinY = MaxY = P0.y; if(IDOK = ccd.DoModal() /调用颜色对话框选取填充色 Invalidate(FALSE); 3.使用MFC技术实现种子填充算法,参考界面效果如下: ON_WM_LBUTTONDOWN() SeedClr = RGB(255, 0, 0);OnContextMenu(CWnd* pWnd, CPoi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1