本帖最后由 mindrayguowei 于 2017-2-20 09:06 编辑
- //这个方法响应菜单事件, table id 100是我手动设置的, 为了方便测试
- private void tableImageToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- TXTextControl.Table table = this._textControl.Tables.GetItem(100);
-
- int row = (imageList.Count / table.Columns.Count) + 1;
- int column = (imageList.Count % table.Columns.Count) + 1;
- if (row > table.Rows.Count)
- {
- TXTextControl.TableCell lastCell = table.Cells.GetItem(table.Rows.Count, table.Columns.Count);
- _textControl.InputPosition = new TXTextControl.InputPosition(lastCell.Start - 1);
- if (table.Rows.CanAdd)
- {
- bool flag = table.Rows.Add(TXTextControl.TableAddPosition.After, 1);
- if (flag == true)
- {
- TXTextControl.TableRow newRow = table.Rows.GetItem(row);
- newRow.MinimumHeight = 1134;
- }
- else
- {
- return;
- }
- }
- else
- {
- return;
- }
- }
- TXTextControl.TableRow tableRow = table.Rows.GetItem(row);
- if (table != null)
- {
- TXTextControl.TableCell cell = table.Cells.GetItem(row, column);
- if (cell != null)
- {
- TXTextControl.Image imageNew = GetInsertImage(cell.Width, tableRow.MinimumHeight);
- if (imageNew == null) return;
- imageList.Add(imageNew);
- //_textControl.Images.Add(imageNew, TXTextControl.HorizontalAlignment.Left, cell.Start - 1, TXTextControl.ImageInsertionMode.DisplaceCompleteLines);
- _textControl.Images.Add(imageNew, cell.Start - 1);
- }
- }
- }
- catch (Exception exc)
- {
- MessageBox.Show(exc.Message, ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- //这个方法打开一张硬盘上的图片, 长和宽需要自适应table的长宽
- private TXTextControl.Image GetInsertImage(int width, int height)
- {
- Graphics g = _textControl.CreateGraphics();
- int iTwipsPerPixel = (int)(1440 / g.DpiX);
- OpenFileDialog dlgOpen = new OpenFileDialog();
- if (dlgOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- Image tmp = Image.FromFile(dlgOpen.FileName);
- TXTextControl.Image image = new TXTextControl.Image(tmp);
- image.HorizontalScaling = width * 100 / (tmp.Width * iTwipsPerPixel);
- image.VerticalScaling = height * 100 / (tmp.Height * iTwipsPerPixel);
- image.ID = imageList.Count;
- return image;
- }
- return null;
- }
复制代码 |