向用户开放向单元格添加图片的功能
问题描述:双击单元格,然后弹出图片选择对话框,然后把图片插入到该单元格。问题解答:可以通过CellDoubleClick事件来捕获双击Spread事件,然后通过ImageCellType插入选择图片。
关键代码:
?
///<summary>
/// 添加 ImageCellType
/// </summary>
private void AddCellType()
{
FarPoint.Win.Spread.CellType.ImageCellType imgct = new FarPoint.Win.Spread.CellType.ImageCellType();
System.Drawing.Image image = System.Drawing.Image.FromFile("Tulips.jpg");
imgct.Style = FarPoint.Win.RenderStyle.Stretch;
imgct.TransparencyColor = Color.Black;
imgct.TransparencyTolerance = 20;
fpSpread1.Sheets.Columns.Width = 100;
fpSpread1.Sheets.Rows.Height = 50;
fpSpread1.Sheets.Cells.CellType = imgct;
fpSpread1.Sheets.Cells.Value = image;
this.fpSpread1.CellDoubleClick += new FarPoint.Win.Spread.CellClickEventHandler(fpSpread1_CellDoubleClick);
}
//双击弹出选择对话框
private void fpSpread1_CellDoubleClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e)
{
if (fpSpread1.ActiveSheet.ActiveCell.CellType is FarPoint.Win.Spread.CellType.ImageCellType)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "*.jpg|*.jpg";
if (dlgOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fpSpread1.ActiveSheet.ActiveCell.Value = System.Drawing.Image.FromFile(dlgOpen.FileName);
}
}
}
示例下载:点击下载
页:
[1]