SpeechSynthesizer
下边代码多次调用,会导致内存溢出outofmemory。
SpeechSynthesizer 需要改为全局静态。
private void button_Click(object sender, EventArgs e)
{
SpeechSynthesizer speak = new SpeechSynthesizer();
speak.SpeakAsync(speakWords);
}
标题datagridview合并单元格
别人的用来做了点改进,解决原代码多次重绘文字,导致文字重影变粗,而且单元格容不下字体时,字体越界。
调用:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView_CellPaintingMergeCell(sender, e,2);
}
代码:
public static void DataGridView_CellPaintingMergeCell(object sender, DataGridViewCellPaintingEventArgs e, int mergeColumnCount)
{
DataGridView myGrid = sender as DataGridView;
Brush gridBrush = new SolidBrush(myGrid.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
int UpRows = 0;
int DownRows = 0;
int rowSpanCount = 0;
int cellwidth = e.CellBounds.Width;
Pen gridLinePen = new Pen(gridBrush);
if (e.ColumnIndex < mergeColumnCount && e.RowIndex >= 0)
{
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
for (int i = e.RowIndex + 1; i < myGrid.Rows.Count; i++)
{
if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null)
{
if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
DownRows++;
}
else
{
break;
}
}
}
for (int i = e.RowIndex - 1; i >= 0; i--)
{
if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null)
{
if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
UpRows++;
}
else
{
break;
}
}
}
rowSpanCount = DownRows + UpRows + 1;
if (rowSpanCount <= 1)
{ return; }
int cellheight = e.CellBounds.Height;
e.Graphics.FillRectangle(backBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y - cellheight * UpRows, cellwidth-1, rowSpanCount * cellheight - 1));
SizeF fontAreaSize = e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font, cellwidth+1);
int fontAreaHeight = (int)fontAreaSize.Height;
int fontAreaWidth = (int)fontAreaSize.Width;
int fontAreaX = e.CellBounds.X + (cellwidth - fontAreaWidth) / 2;
int fontAreaY = e.CellBounds.Y - cellheight * UpRows + (cellheight * rowSpanCount - fontAreaHeight) / 2;
if (cellwidth < fontAreaWidth)
{
fontAreaWidth = cellwidth-1;
}
if (cellheight * rowSpanCount < fontAreaHeight)
{ fontAreaY = e.CellBounds.Y - cellheight * UpRows; fontAreaHeight = cellheight * rowSpanCount; }
e.Graphics.DrawString(curValue, e.CellStyle.Font, fontBrush, new RectangleF(fontAreaX, fontAreaY, fontAreaWidth+1, fontAreaHeight));
// 画下边线
if (DownRows == 0)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom -1, e.CellBounds.Right , e.CellBounds.Bottom -1);
rowSpanCount = 0;
}
// 画右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right-1, e.CellBounds.Top, e.CellBounds.Right-1, e.CellBounds.Bottom);
e.Handled = true;
}
}