internal WriteableBitmap GetColumSnapshot(int iColumn) { GB_GridView.CellPaintEventArgs e = new GB_GridView.CellPaintEventArgs(); e.Bounds = new GDI.Rectangle(0, 0, (int)Columns[iColumn].Width, (int)ActualHeight); e.ColumnIndex = iColumn; WriteableBitmap temp = new WriteableBitmap(e.Bounds.Width, e.Bounds.Height, 96.0, 96.0, PixelFormats.Pbgra32, null); GDI.Bitmap tempbmp = new GDI.Bitmap(temp.PixelWidth, temp.PixelHeight, temp.BackBufferStride, GDI.Imaging.PixelFormat.Format32bppArgb, temp.BackBuffer); using (GDI.Graphics gg = GDI.Graphics.FromImage(tempbmp)) { e.Graphics = gg; e.RowIndex = -1; e.States = GB_GridView.GridViewElementStates.Selected; e.Value = Columns[iColumn].Header; DefaultPaintCell(e); } tempbmp.Dispose(); temp.Lock(); temp.AddDirtyRect(new Int32Rect(0, 0, temp.PixelWidth, temp.PixelHeight)); temp.Unlock(); return(temp); }
void DefaultPaintCell(GB_GridView.CellPaintEventArgs e) { if (m_cellPaint != null) { m_cellPaint(this, e); if (e.Handled) { return; } } GDI.Brush brush = m_background; if (e.ColumnIndex == m_mouseColumn) { brush = m_hoverbackground; } e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 1); //e.Graphics.DrawRectangle(System.Drawing.Pens.Black, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 1); // 判断下有没有SortGraph System.Windows.Forms.SortOrder so = GetColumnSortGraph(e.ColumnIndex); if (so != System.Windows.Forms.SortOrder.None) { GDI.Drawing2D.GraphicsPath path = new GDI.Drawing2D.GraphicsPath(); // 创建一个5*9的三角形 GDI.Point[] ptList = null; if (so == System.Windows.Forms.SortOrder.Ascending) // 箭头朝上 { ptList = new GDI.Point[] { new GDI.Point(5, 0), new GDI.Point(0, 5), new GDI.Point(9, 5) }; } else { ptList = new GDI.Point[] { new GDI.Point(4, 5), new GDI.Point(0, 0), new GDI.Point(9, 0) }; } GDI.Size offset = new GDI.Size(e.Bounds.Right - e.Bounds.Width / 2 - 5, e.Bounds.Top); path.AddLines(new GDI.Point[] { GDI.Point.Add(ptList[0], offset), GDI.Point.Add(ptList[1], offset), GDI.Point.Add(ptList[2], offset) }); e.Graphics.FillPath(m_foreground, path); path.Dispose(); } if (e.Value != null) { GDI.StringFormat sf = new System.Drawing.StringFormat(); sf.Alignment = System.Drawing.StringAlignment.Center; sf.FormatFlags = System.Drawing.StringFormatFlags.NoWrap; sf.LineAlignment = System.Drawing.StringAlignment.Center; sf.Trimming = System.Drawing.StringTrimming.EllipsisCharacter; System.Drawing.Text.TextRenderingHint hint = e.Graphics.TextRenderingHint; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; e.Graphics.DrawString(e.Value.ToString(), Font, m_foreground, e.Bounds, sf); e.Graphics.TextRenderingHint = hint; } }
/// <summary> /// 重新绘制本区域内的内容 /// </summary> /// <param name="rect">相对当前屏幕的坐标矩形</param> public void paintArea(GDI.Rectangle rect) { if (rect.Height == 0 || rect.Width == 0) { return; } GDI.Drawing2D.GraphicsState state = g.Save(); g.SetClip(rect); // 开始画图吧 double iOffsetX = rect.X; int iColumn = GetCellColumn(ref iOffsetX); if (iColumn == -1) { g.Restore(state); return; } GB_GridView.CellPaintEventArgs e = new GB_GridView.CellPaintEventArgs(); g.FillRectangle(m_background, rect); double iColumnWidth = rect.X - iOffsetX; for (int j = iColumn; j < Columns.Count; ++j) { if (iColumnWidth > ActualWidth) { break; // 说明这一行画完了,开始画下一行吧 } e.Bounds = new GDI.Rectangle((int)iColumnWidth, 0, (int)Columns[j].Width, m_bufferedBmp.PixelHeight); iColumnWidth += Columns[j].Width; e.ColumnIndex = j; e.RowIndex = -1; e.States = GB_GridView.GridViewElementStates.Visible; e.Graphics = g; e.Value = Columns[j].Header.ToString(); DefaultPaintCell(e); } g.Restore(state); }
protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); if (m_mouseColumn != -1) { int m_prev_mouseColumn = m_mouseColumn; m_mouseColumn = -1; GB_GridView.CellPaintEventArgs ce = new GB_GridView.CellPaintEventArgs(); int iPos = GetCellPosition(m_prev_mouseColumn); ce.Bounds = new GDI.Rectangle(iPos, 0, (int)(Columns[m_prev_mouseColumn].Width), m_bufferedBmp.PixelHeight); ce.ColumnIndex = m_prev_mouseColumn; ce.Graphics = g; ce.RowIndex = -1; ce.States = GB_GridView.GridViewElementStates.Visible; ce.Value = Columns[m_prev_mouseColumn].Header.ToString(); DefaultPaintCell(ce); Invalidate(new Int32Rect(ce.Bounds.X, ce.Bounds.Y, ce.Bounds.Width, ce.Bounds.Height)); } }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); Point ptOwn = e.GetPosition(this); double iOffsetX = ptOwn.X; int iCol = GetCellColumn(ref iOffsetX); //正在拖动列头 if (m_bdragingColumn) { // 改变列的宽度吧 int iWidthOffset = (int)(ptOwn.X - m_ptMouseDown.X); if (m_tempWidth == 0) { m_tempWidth = Columns[m_needadjustcolumn].Width; } double iNewWidth = m_tempWidth + iWidthOffset; if (iNewWidth <= 0) { iNewWidth = 3; } Columns[m_needadjustcolumn].Width = iNewWidth; paintArea(new GDI.Rectangle((int)(ptOwn.X - iNewWidth), 0, (int)(m_bufferedBmp.PixelWidth - (int)ptOwn.X + iNewWidth), m_bufferedBmp.PixelHeight)); Invalidate(new Int32Rect((int)(ptOwn.X - iNewWidth), 0, (int)(m_bufferedBmp.PixelWidth - (int)ptOwn.X + iNewWidth), m_bufferedBmp.PixelHeight)); // 这里需要暴露出来,列宽已经改变了,需要更新ExtentWidth if (OnExtentViewChanged != null) { OnExtentViewChanged(); } if (GridView != null) { GridView.paintArea(new GDI.Rectangle(0, 0, (int)GridView.ActualWidth, (int)GridView.ActualHeight)); GridView.Invalidate(); } return; } if (iCol != m_mouseColumn) { // 说明图形位置更新了,那就需要来替换了啊 // 高亮显示列吧 int m_prev_mouseColumn = m_mouseColumn; m_mouseColumn = iCol; // 先清除老的行 if (m_prev_mouseColumn != -1) { // 这里还要判断是不是上次的已经滚动走了 int iOldColPos = GetCellPosition(m_prev_mouseColumn); GB_GridView.CellPaintEventArgs e1 = new GB_GridView.CellPaintEventArgs(); e1.Bounds = new GDI.Rectangle(iOldColPos, 0, (int)Columns[m_prev_mouseColumn].Width, m_bufferedBmp.PixelHeight); e1.ColumnIndex = m_prev_mouseColumn; e1.RowIndex = -1; e1.Graphics = g; e1.Value = Columns[m_prev_mouseColumn].Header.ToString(); DefaultPaintCell(e1); Invalidate(new Int32Rect(e1.Bounds.X, e1.Bounds.Y, e1.Bounds.Width, e1.Bounds.Height)); } // 如果鼠标已经换到别的列,则再画新列 if (m_mouseColumn != m_prev_mouseColumn && m_mouseColumn != -1) { GB_GridView.CellPaintEventArgs e1 = new GB_GridView.CellPaintEventArgs(); e1.Bounds = new GDI.Rectangle((int)(ptOwn.X - iOffsetX), 0, (int)Columns[m_mouseColumn].Width, m_bufferedBmp.PixelHeight); e1.ColumnIndex = m_mouseColumn; e1.RowIndex = -1; e1.Value = Columns[m_mouseColumn].Header.ToString(); e1.Graphics = g; DefaultPaintCell(e1); Invalidate(new Int32Rect(e1.Bounds.X, e1.Bounds.Y, e1.Bounds.Width, e1.Bounds.Height)); } } // 这里在列上面,所以得判断光标是不是在两个列之间边缘,这种情况下要允许拖动列大小 if (iCol != -1) { if (iOffsetX < 3 || Columns[iCol].Width - iOffsetX < 3) { //这里光标要变化 Cursor = Cursors.SizeWE; } else { Cursor = Cursors.Arrow; } } else { Cursor = Cursors.Arrow; } }