两个C#开发实例

让DataGridView显示行号
DataGridView默认的属性中,不可设置显示行号。
效果如下:
单击显示全图,Ctrl+滚轮缩放图片


为了表示行号,我们可以在DataGridView的RowPostPaint事件中进行绘制。
RowPostPaint事件,具体可以参照MSDN。
下面是实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace DrawRowIndex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = ImageCodecInfo.GetImageDecoders();
}

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 4,
e.RowBounds.Height);

TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
}
}


C#高精度计时器

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
namespace Test.TechTao.Dlis
{
     class time
     {
         #region using dll
         [DllImport("Kernel32.dll")]
         private static extern bool QueryPerformanceCounter(out   long lpPerformanceCount);
         [DllImport("Kernel32.dll")]
         private static extern bool QueryPerformanceFrequency(out   long lpFrequency);
         #endregion
         private long startTime, stopTime;
         private long freq;
         public time()
         {
             if (QueryPerformanceFrequency(out freq) == false)
             {
                 //   不支持高精度计时  
                 throw new Win32Exception();
             }
         }
         //   计时器开始  
         public void Start()
         {
             Thread.Sleep(0);
             QueryPerformanceCounter(out startTime);
         }
         //   计时器结束  
         public void Stop()
         {
             QueryPerformanceCounter(out stopTime);
         }
         //   计时器清零
         public void clear()
         {
             startTime = 0;
             stopTime = 0;
         }
         //   持续时间,单位秒  
         public double Duration
         {
             get
             {
                 return (stopTime - startTime) / (double)freq;
             }
         }
     }
}

你可能感兴趣的:(职场,C#,实例,datagridview,休闲)