110,499
社区成员
发帖
与我相关
我的任务
分享
#region
public class DataGridViewTextButtonColumn : DataGridViewTextBoxColumn
{
public DataGridViewTextButtonColumn(DataGridView dgv)
{
this.CellTemplate = new DataGridViewTextButtonCell(dgv);
}
}
public class DataGridViewTextButtonCell : DataGridViewTextBoxCell
{
private Bitmap _buttonFace;
private Bitmap _buttonFacePressed;
public event DataGridCellButtonClickEventHandler CellButtonClicked;
DataGridView dgv;
public DataGridViewTextButtonCell()
{
}
public DataGridViewTextButtonCell(DataGridView dgv)
{
try
{
this.dgv = dgv;
System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
_buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
_buttonFacePressed = new Bitmap(strm);
}
catch { }
}
//Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds,
//System.Windows.Forms.CurrencyManager source, int rowNum,
//System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
//graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);
//(int x, int y, int width, int height)
//Rectangle rert = new Rectangle(cellBounds.X + cellBounds.Width - 15, cellBounds.Y - 2, 10, cellBounds.Height - 2);
//graphics.FillRectangle(new SolidBrush(Color.Red), rert);
//base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
if (this.dgv == null)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
if(dgv.CurrentCell.ColumnIndex<=-1) return;
DataGridViewColumn dgvCol = dgv.Columns[dgv.CurrentCell.ColumnIndex];
if (!(dgvCol is DataGridViewTextButtonColumn)) return;
//bool current = parent.IsSelected(rowNum) ||
// (parent.CurrentRowIndex == rowNum &&
// parent.CurrentCell.ColumnNumber == this._columnNum);
//Color BackColor = current ? parent.SelectionBackColor : parent.BackColor;
//Color ForeColor = current ? parent.SelectionForeColor : parent.ForeColor;
Color BackColor = dgv.BackgroundColor;
Color ForeColor = dgv.ForeColor;
//clear the cell
graphics.FillRectangle(new SolidBrush(BackColor), cellBounds);
//draw the value
//string s = this.GetColumnValueAtRow(source, rowNum).ToString();//parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
string s = dgv.CurrentCell.Value.ToString();
//Font font = new Font("Arial", 8.25f);
//g.DrawString(s, font, new SolidBrush(Color.Black), bounds);
graphics.DrawString(s, dgv.Font, new SolidBrush(ForeColor), cellBounds);
//draw the button
//Bitmap bm = _pressedRow == rowNum ? this._buttonFacePressed : this._buttonFace;
Bitmap bm = _buttonFace;
graphics.DrawImage(bm, cellBounds.Right - bm.Width, cellBounds.Y);
//font.Dispose();
}
public void HandleMouseUp(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
//DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
if (hit.Type != DataGridViewHitTestType.Cell) return;
//if(hit.RowIndex<=-1) return;
//DataGridViewColumn col = this.DataGridView.Columns[hit.ColumnIndex];
if (!(this.dgv.Columns[hit.ColumnIndex] is DataGridViewTextButtonColumn)) return;
Rectangle rect = this.dgv.GetCellDisplayRectangle(hit.ColumnIndex, hit.RowIndex, true);
using (Graphics g = Graphics.FromHwnd(this.dgv.Handle))
{
g.DrawImage(this._buttonFace, rect.Right - this._buttonFace.Width, rect.Y);
if (CellButtonClicked != null)
CellButtonClicked(this, new DataGridCellButtonClickEventArgs(hit.RowIndex, hit.ColumnIndex));
}
}
}
#endregion
public class DataGridViewTextButtonColumn : DataGridViewTextBoxColumn
{
public DataGridViewTextButtonColumn(DataGridView dgv)
{
DataGridViewTextButtonCell dgvtb = new DataGridViewTextButtonCell();
DataGridViewTextButtonCell.dgv = dgv;
this.CellTemplate = dgvtb;
}
}
public class DataGridViewTextButtonCell : DataGridViewTextBoxCell
{
private Bitmap _buttonFace;
private Bitmap _buttonFacePressed;
public event DataGridCellButtonClickEventHandler CellButtonClicked;
public static DataGridView dgv;
public DataGridViewTextButtonCell()
{
try
{
System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
_buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
_buttonFacePressed = new Bitmap(strm);
}
catch { }
}
//public DataGridViewTextButtonCell(DataGridView dgv)
//{
// try
// {
// this.dgv = dgv;
// System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
// _buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
// strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
// _buttonFacePressed = new Bitmap(strm);
// }
// catch { }
//}
//Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds,
//System.Windows.Forms.CurrencyManager source, int rowNum,
//System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
//graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);
//(int x, int y, int width, int height)
//Rectangle rert = new Rectangle(cellBounds.X + cellBounds.Width - 15, cellBounds.Y - 2, 10, cellBounds.Height - 2);
//graphics.FillRectangle(new SolidBrush(Color.Red), rert);
//base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
if (dgv == null||dgv.CurrentCell==null)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
if(dgv.CurrentCell.ColumnIndex<=-1) return;
DataGridViewColumn dgvCol = dgv.Columns[dgv.CurrentCell.ColumnIndex];
if (!(dgvCol is DataGridViewTextButtonColumn)) return;
//bool current = parent.IsSelected(rowNum) ||
// (parent.CurrentRowIndex == rowNum &&
// parent.CurrentCell.ColumnNumber == this._columnNum);
//Color BackColor = current ? parent.SelectionBackColor : parent.BackColor;
//Color ForeColor = current ? parent.SelectionForeColor : parent.ForeColor;
Color BackColor = dgv.BackgroundColor;
Color ForeColor = dgv.ForeColor;
//clear the cell
graphics.FillRectangle(new SolidBrush(BackColor), cellBounds);
//draw the value
//string s = this.GetColumnValueAtRow(source, rowNum).ToString();//parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
string s=null;
if (dgv.CurrentCell.Value == null)
s = "";
else
s = dgv.CurrentCell.Value.ToString();
//Font font = new Font("Arial", 8.25f);
//g.DrawString(s, font, new SolidBrush(Color.Black), bounds);
graphics.DrawString(s, dgv.Font, new SolidBrush(ForeColor), cellBounds);
//draw the button
//Bitmap bm = _pressedRow == rowNum ? this._buttonFacePressed : this._buttonFace;
Bitmap bm = _buttonFace;
graphics.DrawImage(bm, cellBounds.Right - bm.Width, cellBounds.Y);
//font.Dispose();
}
public void HandleMouseUp(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hit = dgv.HitTest(e.X, e.Y);
//DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
if (hit.Type != DataGridViewHitTestType.Cell) return;
//if(hit.RowIndex<=-1) return;
//DataGridViewColumn col = this.DataGridView.Columns[hit.ColumnIndex];
if (!(dgv.Columns[hit.ColumnIndex] is DataGridViewTextButtonColumn)) return;
Rectangle rect = DataGridViewTextButtonCell.dgv.GetCellDisplayRectangle(hit.ColumnIndex, hit.RowIndex, true);
using (Graphics g = Graphics.FromHwnd(dgv.Handle))
{
g.DrawImage(this._buttonFace, rect.Right - this._buttonFace.Width, rect.Y);
if (CellButtonClicked != null)
CellButtonClicked(this, new DataGridCellButtonClickEventArgs(hit.RowIndex, hit.ColumnIndex));
}
}
}