huashi2017 最近的时间轴更新
huashi2017

huashi2017

V2EX 第 234234 号会员,加入于 2017-06-06 17:16:38 +08:00
huashi2017 最近回复了
继承 DataGridViewCell 自己写一个 cell 类型出来,在里边画··· 这么做真的很麻烦很复杂···,一定要两个按钮完全可以用自带的 buttoncell 类 然后通过合并单元格的形式实现两个按钮的效果

class twobuttoncell : DataGridViewCell
{
public twobuttoncell():base()
{

}
public override Type FormattedValueType
{
get
{
Type valueType = base.ValueType;
if (valueType != null)
{
return valueType;
}
return typeof(String);
}
}
Rectangle lb;
Rectangle rb;
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);

lb = new Rectangle(new Point(cellBounds.X+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
rb = new Rectangle(new Point(cellBounds.X+cellBounds.Width/2+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
graphics.FillRectangle(Brushes.White, cellBounds);
graphics.DrawRectangle(new Pen(cellStyle.ForeColor), lb);
graphics.DrawString("left", cellStyle.Font, Brushes.Red, lb);
graphics.DrawRectangle(new Pen(cellStyle.ForeColor), rb);
graphics.DrawString("right", cellStyle.Font, Brushes.Red, rb);

}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//base.OnMouseClick(e);
var rec = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Point pt = new Point(rec.Location.X+e.X,rec.Y+e.Y);

if (lb.Contains(pt))
{
MessageBox.Show("left");
}
if (rb.Contains(pt))
{
MessageBox.Show("right");
}
}
}
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   883 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 20:07 · PVG 04:07 · LAX 12:07 · JFK 15:07
Developed with CodeLauncher
♥ Do have faith in what you're doing.