//重载绘图方法 protected override void OnPaint(PaintEventArgs pe) { //使用双缓冲绘图 BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; BufferedGraphics bufg = currentContext.Allocate(pe.Graphics, this.ClientRectangle); Graphics g = bufg.Graphics; g.Clear(Color.White); //开启抗锯齿 g.SmoothingMode = SmoothingMode.AntiAlias; // 计算可现范围,设置滚动条 int frameSizeX = this.Width; int frameSizeY = this.Height; if (listDesk != null && listDesk.Count > 0) { Desk lastDesk = listDesk[listDesk.Count - 1]; if ((lastDesk.X + lastDesk.width) > frameSizeX) { frameSizeX = lastDesk.X + lastDesk.width + 30; } if ((lastDesk.Y + lastDesk.height) > frameSizeY) { frameSizeY = lastDesk.Y + lastDesk.height + 30; } } foreach (Student std in listStudent) { if ((std.X + std.width) > frameSizeX) { frameSizeX = std.X + std.width + 30; } if ((std.Y + std.height) > frameSizeY) { frameSizeY = std.Y + std.height + 30; } } this.AutoScrollMinSize = new Size(frameSizeX, frameSizeY); paintTransX = this.AutoScrollPosition.X; paintTransY = this.AutoScrollPosition.Y; //绘制范围平移 g.TranslateTransform(paintTransX, paintTransY); //开始绘制 int CandidateStartX = 242; //候选区与教室分界线 //绘制候选区与教室区 g.DrawString("候选区", new Font("宋体", 14), new SolidBrush(Color.Black), 1, 1); g.DrawLine(new Pen(Color.Black, 5f), new Point(CandidateStartX, 0), new Point(CandidateStartX, this.Height)); g.DrawString("课室", new Font("宋体", 14), new SolidBrush(Color.Black), CandidateStartX + 3, 1); //绘制学生卡 if (listStudent != null || listStudent.Count > 0) { int stdCount = listStudent.Count; Hashtable hash = new Hashtable(); for (int s = 0; s < stdCount; s++) { g.DrawImage(listStudent[s].Shape, listStudent[s].X, listStudent[s].Y); //重叠学生处理 try{ hash.Add(listStudent[s].X.ToString() + "," + listStudent[s].Y.ToString(), null); } catch (Exception) { g.FillRectangle(new SolidBrush(Color.FromArgb(150, 0, 220, 0)), listStudent[s].X, listStudent[s].Y, listStudent[s].width, listStudent[s].height ); } } } //绘制讲台与课桌 InitDeskAndDais(ref g); //实际描绘到视图 bufg.Render(pe.Graphics); g.Dispose(); bufg.Dispose(); }
//绘制课桌与讲台 private void InitDeskAndDais(ref Graphics gDesk) { string[] deskColGroup = deskColFormat.Split('/'); if (deskColFormat.Trim() == "" || deskColGroup.Length == 0) { return; } int steps = deskColGroup.Length; int[] deskColSteps = new int[steps]; deskCol = 0; try { for (int s = 0; s < steps; s++) { deskColSteps[s] = int.Parse(deskColGroup[s]); deskCol += deskColSteps[s]; } } catch (Exception) { //MessageBox return; } if (deskRow * deskCol == 0) { return; } //根据行列数据初始课桌列表 listDesk.Clear(); for (int i = 0; i < deskRow * deskCol; i++) { Desk desk = new Desk(); listDesk.Add(desk); } //起点位置 int daisStartY = 30; int deskStartX = 290; int deskStartY = daisStartY + dais.Height + 25; //间距 int deskDistX = 15; int deskDistY = 15; int colCenter = 0; //讲台中心 //分别绘制 for (int row = 0; row < deskRow; row++) { int colPosition = 0; for (int col = 0; col < deskCol; col++) { int order = row * deskCol + col; if (!listDesk[order].isValued) { continue; } int px = deskStartX + colPosition; int py = deskStartY + (listDesk[order].height + deskDistY) * row; listDesk[order].X = px; listDesk[order].Y = py; gDesk.DrawImage(listDesk[order].Shape, px, py); //移动X位置 colPosition += listDesk[order].width + deskDistX; //遇到分组时多进一个间距 int rDis = 0; for (int r = 0; r < deskColSteps.Length - 1; r++) { rDis += deskColSteps[r]; if (col == rDis - 1) { colPosition += deskDistX; break; } } } //确定讲桌中心 colCenter = (colPosition - deskDistX) / 2; } //根据课桌中心位置绘制讲台 //int colCenter = deskStartX + (deskDistX * (deskCol-1) + 30) / 2; int daisStartX = colCenter - dais.Width / 2; daisStartX = daisStartX < 0? 0: daisStartX; dais.X = deskStartX + daisStartX; dais.Y = daisStartY; gDesk.DrawImage(dais.Shape, dais.X, dais.Y); //计算打印区域 int printExtend = 20; //扩边范围 int printXmin = listDesk[0].X - printExtend; int printXmax = listDesk[listDesk.Count - 1].X + listDesk[0].width + printExtend; int printYmin = dais.Y - printExtend; int printYmax = listDesk[listDesk.Count - 1].Y + listDesk[0].height + printExtend; printRange = new Rectangle(printXmin, printYmin, printXmax - printXmin, printYmax - printYmin); }