winform记录

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;
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值