DataGridView自定义控件类

   1 public  class clsWindowGird
   2     {
   3         public static void Editcol(int iColumnCount, DataGridView Grid, string sCaption, string sName, int iWidth, bool bVisible, bool bEdit, DataGridViewColumnSortMode SortMode, GridColumnType ColumnType)
   4         {
   5             DataGridViewColumn column = new DataGridViewColumn();
   6             switch (ColumnType)
   7             {
   8                 case GridColumnType.enCheckBox:
   9                     column = new DataGridViewCheckBoxColumn();
  10                     column.HeaderCell = new DataGridViewCheckBoxHeaderCell();
  11                     break;
  12                 case GridColumnType.enTextBox:
  13                     column = new DataGridViewTextBoxColumn();//new DataGridViewTextBoxColumn();
  14                     break;
  15                 case GridColumnType.enComboBox:
  16                     column = new DataGridViewComboBoxColumn();
  17                     break;
  18                 case GridColumnType.enButton:
  19                     column = new DataGridViewDisableButtonColumn();
  20                     break;
  21                 case GridColumnType.enProgressBar:
  22                     column = new DataGridViewProgressBarColumn();
  23                     break;
  24                 case GridColumnType.enDateTimePicker:
  25                     column = new CalendarColumn();
  26                     break;
  27                 case GridColumnType.enWorkStatus:
  28                     column = new WorkStatusColumn();
  29                     break;
  30                 case GridColumnType.enImage:
  31                     column = new DataGridViewImageColumn();
  32                     break;
  33                 case GridColumnType.enBrighter:
  34                     column = new BrighterColumn();
  35                     break;
  36                 case GridColumnType.enVolume:
  37                     column = new VolumeColumn();
  38                     break;
  39                 case GridColumnType.enTrackBar:
  40                     column = new TrackBarColumn();
  41                     break;
  42                 case GridColumnType.enTimeZone:
  43                     column = new TimeZoneColumn();
  44                     break;
  45                 case GridColumnType.enVolumeTrackBar:
  46                     column = new VolumeTrackBarColumn();
  47                     break;
  48                 case GridColumnType.enBrighterTrackBar:
  49                     column = new BrighterTrackBarColumn();
  50                     break;
  51             }
  52             switch (sCaption.Substring(0, 1))
  53             {
  54                 case "^":
  55                     column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  56                     break;
  57                 case "<":
  58                     column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  59                     break;
  60                 case ">":
  61                     column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  62                     break;
  63             }
  64             column.HeaderText = sCaption.Remove(0, 1);
  65             column.SortMode = SortMode;
  66             Grid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  67 
  68             column.Name = sName;
  69             column.Visible = bVisible;
  70             column.ReadOnly = bEdit;
  71             //column.Width = iWidth * int.Parse(font.Size.ToString());
  72             column.Width = iWidth;
  73             Grid.Columns.Add(column);
  74         }
  75         public enum GridColumnType
  76         {
  77             enTextBox = 0, //文本框
  78             enComboBox = 1,//下拉框
  79             enCheckBox = 2,//复选框
  80             enButton = 3, //按钮
  81             enProgressBar = 4,//进度条
  82             enDateTimePicker = 5, //时间框
  83             enWorkStatus = 6, //工作状态图标文字
  84             enImage = 7, //图片框
  85             enBrighter = 8,//亮度图标文字
  86             enVolume = 9, //音量图标文字
  87             enVolumeTrackBar = 10, //调节音量带滑块
  88             enTimeZone = 11, //自定义控件
  89             enTrackBar = 12, //滑块
  90             enBrighterTrackBar = 13, //滑块
  91         }
  92         #region 可以启用(禁用)按钮的自定义Grid按钮列和单元格
  93         public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
  94         {
  95             public DataGridViewDisableButtonColumn()
  96             {
  97                 this.CellTemplate = new DataGridViewDisableButtonCell();
  98             }
  99         }
 100         public class DataGridViewDisableButtonCell : DataGridViewButtonCell
 101         {
 102             private bool enabledValue;
 103             public bool Enabled
 104             {
 105                 get
 106                 {
 107                     return enabledValue;
 108                 }
 109                 set
 110                 {
 111                     enabledValue = value;
 112                 }
 113             }
 114 
 115             // 重写
 116             public override object Clone()
 117             {
 118                 DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell)base.Clone();
 119                 cell.Enabled = this.Enabled;
 120                 return cell;
 121             }
 122 
 123             // 默认情况下,按钮可用
 124             public DataGridViewDisableButtonCell()
 125             {
 126                 this.enabledValue = true;
 127             }
 128 
 129             protected override void Paint(Graphics graphics,
 130                 Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
 131                 DataGridViewElementStates elementState, object value,
 132                 object formattedValue, string errorText,
 133                 DataGridViewCellStyle cellStyle,
 134                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
 135                 DataGridViewPaintParts paintParts)
 136             {
 137                 // 判断是否被禁用,如果被禁用,这绘制不可用的按钮样式
 138                 if (!this.enabledValue)
 139                 {
 140                     //绘制背景
 141                     if ((paintParts & DataGridViewPaintParts.Background) ==
 142                         DataGridViewPaintParts.Background)
 143                     {
 144                         SolidBrush cellBackground =
 145                             new SolidBrush(cellStyle.BackColor);
 146                         graphics.FillRectangle(cellBackground, cellBounds);
 147                         cellBackground.Dispose();
 148                     }
 149 
 150                     // 绘制边框
 151                     if ((paintParts & DataGridViewPaintParts.Border) ==
 152                         DataGridViewPaintParts.Border)
 153                     {
 154                         PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
 155                             advancedBorderStyle);
 156                     }
 157 
 158                     // 计算按钮的绘制区域,大小等
 159                     Rectangle buttonArea = cellBounds;
 160                     Rectangle buttonAdjustment =
 161                         this.BorderWidths(advancedBorderStyle);
 162                     buttonArea.X += buttonAdjustment.X;
 163                     buttonArea.Y += buttonAdjustment.Y;
 164                     buttonArea.Height -= buttonAdjustment.Height;
 165                     buttonArea.Width -= buttonAdjustment.Width;
 166 
 167                     // 绘制不可用情况下的按钮              
 168                     ButtonRenderer.DrawButton(graphics, buttonArea,
 169                         PushButtonState.Disabled);
 170 
 171                     // 绘制按钮文本 
 172                     if (this.FormattedValue is String)
 173                     {
 174                         TextRenderer.DrawText(graphics,
 175                             (string)this.FormattedValue,
 176                             this.DataGridView.Font,
 177                             buttonArea, SystemColors.GrayText);
 178                     }
 179                 }
 180                 else
 181                 {
 182                     // 绘制可用情况下的按钮
 183                     base.Paint(graphics, clipBounds, cellBounds, rowIndex,
 184                         elementState, value, formattedValue, errorText,
 185                         cellStyle, advancedBorderStyle, paintParts);
 186                 }
 187             }
 188         }
 189         #endregion;
 190 
 191         #region Grid添加进度条
 192         public class DataGridViewProgressBarColumn : DataGridViewTextBoxColumn
 193         {
 194             public DataGridViewProgressBarColumn()
 195             {
 196                 this.CellTemplate = new DataGridViewProgressBarCell();
 197             }
 198             public override DataGridViewCell CellTemplate
 199             {
 200                 get
 201                 {
 202                     return base.CellTemplate;
 203                 }
 204                 set
 205                 {
 206                     if (!(value is DataGridViewProgressBarCell))
 207                     {
 208                         throw new InvalidCastException("DataGridViewProgressBarCell");
 209                     }
 210                     base.CellTemplate = value;
 211                 }
 212             }
 213             /// <summary>
 214             /// ProgressBar最大値
 215             /// </summary>
 216             public int Maximum
 217             {
 218                 get
 219                 {
 220                     return ((DataGridViewProgressBarCell)this.CellTemplate).Maximum;
 221                 }
 222                 set
 223                 {
 224                     if (this.Maximum == value)
 225                         return;
 226                     ((DataGridViewProgressBarCell)this.CellTemplate).Maximum = value;
 227 
 228                     if (this.DataGridView == null)
 229                         return;
 230                     int rowCount = this.DataGridView.RowCount;
 231                     for (int i = 0; i < rowCount; i++)
 232                     {
 233                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 234                         ((DataGridViewProgressBarCell)r.Cells[this.Index]).Maximum = value;
 235                     }
 236                 }
 237             }
 238             /// <summary>
 239             /// ProgressBar最小値
 240             /// </summary>
 241             public int Mimimum
 242             {
 243                 get
 244                 {
 245                     return ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum;
 246                 }
 247                 set
 248                 {
 249                     if (this.Mimimum == value)
 250                         return;
 251                     ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum = value;
 252                     if (this.DataGridView == null)
 253                         return;
 254                     int rowCount = this.DataGridView.RowCount;
 255                     for (int i = 0; i < rowCount; i++)
 256                     {
 257                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 258                         ((DataGridViewProgressBarCell)r.Cells[this.Index]).Mimimum = value;
 259                     }
 260                 }
 261             }
 262             public int BarValue
 263             {
 264                 get
 265                 {
 266                     return ((DataGridViewProgressBarCell)this.CellTemplate).BarValue;
 267                 }
 268                 set
 269                 {
 270                     if (this.BarValue == value)
 271                         return;
 272                     ((DataGridViewProgressBarCell)this.CellTemplate).BarValue = value;
 273                     if (this.DataGridView == null)
 274                         return;
 275                     int rowCount = this.DataGridView.RowCount;
 276                     for (int i = 0; i < rowCount; i++)
 277                     {
 278                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 279                         ((DataGridViewProgressBarCell)r.Cells[this.Index]).BarValue = value;
 280                     }
 281                 }
 282             }
 283         }
 284         /// <summary>
 285         /// ProgressBarDataGridView
 286         /// </summary>
 287         public class DataGridViewProgressBarCell : DataGridViewTextBoxCell  
 288         {
 289             public DataGridViewProgressBarCell()
 290             {
 291                 this.maximumValue = 100;
 292                 this.mimimumValue = 0;
 293             }
 294 
 295 
 296             private int mValue = 0;
 297             public int BarValue
 298             {
 299                 get
 300                 {
 301                     return this.mValue;
 302                 }
 303                 set
 304                 {
 305                     this.mValue = value;
 306                 }
 307             }
 308 
 309             private int maximumValue;
 310             public int Maximum
 311             {
 312                 get
 313                 {
 314                     return this.maximumValue;
 315                 }
 316                 set
 317                 {
 318                     this.maximumValue = value;
 319                 }
 320             }
 321 
 322             private int mimimumValue;
 323             public int Mimimum
 324             {
 325                 get
 326                 {
 327                     return this.mimimumValue;
 328                 }
 329                 set
 330                 {
 331                     this.mimimumValue = value;
 332                 }
 333             }
 334             public override Type ValueType
 335             {
 336                 get
 337                 {
 338                     return typeof(int);
 339                 }
 340             }
 341             public override object DefaultNewRowValue
 342             {
 343                 get
 344                 {
 345                     return 0;
 346                 }
 347             }
 348             public override object Clone()
 349             {
 350                 DataGridViewProgressBarCell cell = (DataGridViewProgressBarCell)base.Clone();
 351                 cell.Maximum = this.Maximum;
 352                 cell.Mimimum = this.Mimimum;
 353                 return cell;
 354             }
 355 
 356             protected override void Paint(Graphics graphics,
 357                 Rectangle clipBounds, Rectangle cellBounds,
 358                 int rowIndex, DataGridViewElementStates cellState,
 359                 object value, object formattedValue, string errorText,
 360                 DataGridViewCellStyle cellStyle,
 361                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
 362                 DataGridViewPaintParts paintParts)
 363             {
 364                 int intValue = 0;
 365                 if (value is int)
 366                     intValue = (int)value;
 367                 if (intValue < this.mimimumValue)
 368                     intValue = this.mimimumValue;
 369                 if (intValue > this.maximumValue)
 370                     intValue = this.maximumValue;
 371                 if (mValue != 0)
 372                 {
 373                     intValue = mValue;
 374                 }
 375                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
 376                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
 377                 {
 378                     this.PaintBorder(graphics, clipBounds, cellBounds,
 379                         cellStyle, advancedBorderStyle);
 380                 }
 381                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
 382                 Rectangle paintRect = new Rectangle(
 383                     cellBounds.Left + borderRect.Left,
 384                     cellBounds.Top + borderRect.Top,
 385                     cellBounds.Width - borderRect.Right,
 386                     cellBounds.Height - borderRect.Bottom);
 387                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
 388                 System.Drawing.Color bkColor;
 389                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
 390                 {
 391                     bkColor = cellStyle.SelectionBackColor;
 392                 }
 393                 else
 394                 {
 395                     bkColor = cellStyle.BackColor;
 396                 }
 397                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
 398                 {
 399                     using (SolidBrush backBrush = new SolidBrush(bkColor))
 400                     {
 401                         graphics.FillRectangle(backBrush, paintRect);
 402                     }
 403                 }
 404                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
 405                 paintRect.Width -= cellStyle.Padding.Horizontal;
 406                 paintRect.Height -= cellStyle.Padding.Vertical;
 407                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
 408                 {
 409                     Rectangle barBounds = new Rectangle(
 410                             paintRect.Left, paintRect.Bottom - 3*cellBounds.Height/4,
 411                             paintRect.Width, cellBounds.Height/2);
 412                     barBounds.Width = (int)Math.Round(barBounds.Width * rate);
 413                     //ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds);
 414 
 415                     graphics.FillRectangle(System.Drawing.Brushes.Green, barBounds);
 416                     barBounds = new Rectangle(
 417                         paintRect.Left, paintRect.Bottom - cellBounds.Height,
 418                         cellBounds.Width, cellBounds.Height);
 419                     graphics.DrawRectangle(Pens.Wheat, barBounds);
 420                     //if (ProgressBarRenderer.IsSupported)
 421                     //{
 422                     //    //是否完全采用进度条的样式,此处主要修改了进度条的边框样式
 423                     //    //ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect);
 424                     //    Rectangle barBounds = new Rectangle(
 425                     //        paintRect.Left, paintRect.Bottom - 9,
 426                     //        paintRect.Width, 9);
 427                     //    barBounds.Width = (int)Math.Round(barBounds.Width * rate);
 428                     //    //ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds);
 429 
 430                     //    graphics.FillRectangle(System.Drawing.Brushes.Green, barBounds);
 431                     //    barBounds = new Rectangle(
 432                     //        paintRect.Left, paintRect.Bottom - 9,
 433                     //        cellBounds.Width, 9);
 434                     //    graphics.DrawRectangle(Pens.Wheat, barBounds);
 435                     //}
 436                     //else
 437                     //{
 438                     //    //采用控件的背景色
 439                     //    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(bkColor);
 440                     //    graphics.FillRectangle(brush, paintRect);
 441                     //    //graphics.FillRectangle(Brushes.White, paintRect);
 442                     //    //graphics.DrawRectangle(Pens.Black, paintRect);
 443                     //    Rectangle barBounds = new Rectangle(
 444                     //        paintRect.Left + 1, paintRect.Top + 1,
 445                     //        paintRect.Width - 1, paintRect.Height - 1);
 446                     //    barBounds.Width = (int)Math.Round(barBounds.Width * rate);
 447                     //    graphics.FillRectangle(System.Drawing.Brushes.BurlyWood, barBounds);
 448                     //}
 449                 }
 450                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
 451                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
 452                      (paintParts & DataGridViewPaintParts.Focus) ==
 453                          DataGridViewPaintParts.Focus &&
 454                      this.DataGridView.Focused)
 455                 {
 456 
 457                     Rectangle focusRect = paintRect;
 458                     focusRect.Inflate(-3, -3);
 459                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
 460                 }
 461                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
 462                 {
 463                     if (value != null)
 464                     {
 465                         //if (Math.Round(rate * 100) > 0 && Math.Round(rate * 100) <= 100)
 466                         //{
 467                         //string txt = string.Format("{0}%", Math.Round(rate * 100));
 468                         string txt = value.ToString();
 469                         TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
 470                         System.Drawing.Color fColor = cellStyle.ForeColor;
 471                         paintRect.Inflate(-2, -2);
 472                         TextRenderer.DrawText(graphics, txt, cellStyle.Font,
 473                             paintRect, fColor, flags);
 474                         //}
 475                     }
 476                 }
 477                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
 478                     DataGridViewPaintParts.ErrorIcon &&
 479                     this.DataGridView.ShowCellErrors &&
 480                     !string.IsNullOrEmpty(errorText))
 481                 {
 482                     Rectangle iconBounds = this.GetErrorIconBounds(
 483                         graphics, cellStyle, rowIndex);
 484                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
 485                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
 486                 }
 487             }
 488         }
 489         #endregion
 490 
 491         #region Grid添加工作状态控件
 492         public class WorkStatusColumn : DataGridViewTextBoxColumn
 493         {
 494             public WorkStatusColumn()
 495             {
 496                 this.CellTemplate = new WorkStatusCell();
 497             }
 498             public override DataGridViewCell CellTemplate
 499             {
 500                 get
 501                 {
 502                     return base.CellTemplate;
 503                 }
 504                 set
 505                 {
 506                     if (!(value is WorkStatusCell))
 507                     {
 508                         throw new InvalidCastException("DataGridViewVolumeCell");
 509                     }
 510                     base.CellTemplate = value;
 511                 }
 512             }
 513             /// <summary>
 514             /// ProgressBar最大値
 515             /// </summary>
 516             public int Maximum
 517             {
 518                 get
 519                 {
 520                     return ((WorkStatusCell)this.CellTemplate).Maximum;
 521                 }
 522                 set
 523                 {
 524                     if (this.Maximum == value)
 525                         return;
 526                     ((WorkStatusCell)this.CellTemplate).Maximum = value;
 527 
 528                     if (this.DataGridView == null)
 529                         return;
 530                     int rowCount = this.DataGridView.RowCount;
 531                     for (int i = 0; i < rowCount; i++)
 532                     {
 533                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 534                         ((WorkStatusCell)r.Cells[this.Index]).Maximum = value;
 535                     }
 536                 }
 537             }
 538             /// <summary>
 539             /// ProgressBar最小値
 540             /// </summary>
 541             public int Mimimum
 542             {
 543                 get
 544                 {
 545                     return ((WorkStatusCell)this.CellTemplate).Mimimum;
 546                 }
 547                 set
 548                 {
 549                     if (this.Mimimum == value)
 550                         return;
 551                     ((WorkStatusCell)this.CellTemplate).Mimimum = value;
 552                     if (this.DataGridView == null)
 553                         return;
 554                     int rowCount = this.DataGridView.RowCount;
 555                     for (int i = 0; i < rowCount; i++)
 556                     {
 557                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 558                         ((WorkStatusCell)r.Cells[this.Index]).Mimimum = value;
 559                     }
 560                 }
 561             }
 562             public int BarValue
 563             {
 564                 get
 565                 {
 566                     return ((WorkStatusCell)this.CellTemplate).BarValue;
 567                 }
 568                 set
 569                 {
 570                     if (this.BarValue == value)
 571                         return;
 572                     ((WorkStatusCell)this.CellTemplate).BarValue = value;
 573                     if (this.DataGridView == null)
 574                         return;
 575                     int rowCount = this.DataGridView.RowCount;
 576                     for (int i = 0; i < rowCount; i++)
 577                     {
 578                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
 579                         ((WorkStatusCell)r.Cells[this.Index]).BarValue = value;
 580                     }
 581                 }
 582             }
 583         }
 584         /// <summary>
 585         ///WorkStatusCell
 586         /// </summary>
 587         public class WorkStatusCell : DataGridViewTextBoxCell
 588         {
 589             public WorkStatusCell()
 590             {
 591                 this.maximumValue = 1;
 592                 this.mimimumValue = 0;
 593             }
 594 
 595 
 596             private int mValue = 0;
 597             public int BarValue
 598             {
 599                 get
 600                 {
 601                     return this.mValue;
 602                 }
 603                 set
 604                 {
 605                     this.mValue = value;
 606                 }
 607             }
 608 
 609             private int maximumValue;
 610             public int Maximum
 611             {
 612                 get
 613                 {
 614                     return this.maximumValue;
 615                 }
 616                 set
 617                 {
 618                     this.maximumValue = value;
 619                 }
 620             }
 621 
 622             private int mimimumValue;
 623             public int Mimimum
 624             {
 625                 get
 626                 {
 627                     return this.mimimumValue;
 628                 }
 629                 set
 630                 {
 631                     this.mimimumValue = value;
 632                 }
 633             }
 634             public override Type ValueType
 635             {
 636                 get
 637                 {
 638                     return typeof(int);
 639                 }
 640             }
 641             public override object DefaultNewRowValue
 642             {
 643                 get
 644                 {
 645                     return 0;
 646                 }
 647             }
 648             public override object Clone()
 649             {
 650                 WorkStatusCell cell = (WorkStatusCell)base.Clone();
 651                 cell.Maximum = this.Maximum;
 652                 cell.Mimimum = this.Mimimum;
 653                 return cell;
 654             }
 655 
 656             protected override void Paint(Graphics graphics,
 657                 Rectangle clipBounds, Rectangle cellBounds,
 658                 int rowIndex, DataGridViewElementStates cellState,
 659                 object value, object formattedValue, string errorText,
 660                 DataGridViewCellStyle cellStyle,
 661                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
 662                 DataGridViewPaintParts paintParts)
 663             {
 664                 int intValue = 0;
 665                 if (value is int)
 666                     intValue = (int)value;
 667                 //if (intValue < this.mimimumValue)
 668                 //    intValue = this.mimimumValue;
 669                 //if (intValue > this.maximumValue)
 670                 //    intValue = this.maximumValue;
 671                 if (mValue != 0)
 672                 {
 673                     intValue = mValue;
 674                 }
 675                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
 676                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
 677                 {
 678                     this.PaintBorder(graphics, clipBounds, cellBounds,
 679                         cellStyle, advancedBorderStyle);
 680                 }
 681                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
 682                 Rectangle paintRect = new Rectangle(
 683                     cellBounds.Left + borderRect.Left,
 684                     cellBounds.Top + borderRect.Top,
 685                     cellBounds.Width - borderRect.Right,
 686                     cellBounds.Height - borderRect.Bottom);
 687                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
 688                 System.Drawing.Color bkColor;
 689                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
 690                 {
 691                     bkColor = cellStyle.SelectionBackColor;
 692                 }
 693                 else
 694                 {
 695                     bkColor = cellStyle.BackColor;
 696                 }
 697                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
 698                 {
 699                     using (SolidBrush backBrush = new SolidBrush(bkColor))
 700                     {
 701                         graphics.FillRectangle(backBrush, paintRect);
 702                     }
 703                 }
 704                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
 705                 paintRect.Width -= cellStyle.Padding.Horizontal;
 706                 paintRect.Height -= cellStyle.Padding.Vertical;
 707                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
 708                 {
 709                     //Rectangle barBounds = new Rectangle(
 710                     //        paintRect.Left, paintRect.Bottom - cellBounds.Height,
 711                     //       40, cellBounds.Height);
 712                     if (intValue == 0)
 713                     {
 714                         //graphics.DrawImage(Properties.Resources.btncheckoff, paintRect.X+cellBounds.Size.Width/4, paintRect.Bottom - 3*cellBounds.Height / 4 , 60,15);
 715                         graphics.DrawImage(Properties.Resources.btncheckoff, paintRect.X + cellBounds.Size.Width / 4, paintRect.Bottom - cellBounds.Height, 60, cellBounds.Size.Height);
 716 
 717                     }
 718                     else
 719                     {
 720                         //graphics.DrawImage(Properties.Resources.btncheckon, paintRect.X + cellBounds.Size.Width / 4, paintRect.Bottom - 3 * cellBounds.Height / 4, 60, 15);
 721                         graphics.DrawImage(Properties.Resources.btncheckon, paintRect.X + cellBounds.Size.Width / 4, paintRect.Bottom - cellBounds.Height, 60, cellBounds.Size.Height);
 722                     }
 723                 }
 724                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
 725                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
 726                      (paintParts & DataGridViewPaintParts.Focus) ==
 727                          DataGridViewPaintParts.Focus &&
 728                      this.DataGridView.Focused)
 729                 {
 730 
 731                     Rectangle focusRect = paintRect;
 732                     focusRect.Inflate(-3, -3);
 733                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
 734                 }
 735                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
 736                 {
 737                     if (value != null)
 738                     {
 739 
 740                         string txt = value.ToString();
 741                         //TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
 742                         System.Drawing.Color fColor = cellStyle.ForeColor;
 743                         paintRect.Inflate(-2, -2);
 744                         //TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags);
 745                     }
 746                 }
 747                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
 748                     DataGridViewPaintParts.ErrorIcon &&
 749                     this.DataGridView.ShowCellErrors &&
 750                     !string.IsNullOrEmpty(errorText))
 751                 {
 752                     Rectangle iconBounds = this.GetErrorIconBounds(
 753                         graphics, cellStyle, rowIndex);
 754                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
 755                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
 756                 }
 757             }
 758         }
 759         #endregion
 760 
 761         #region Grid添加日期控件
 762         public class CalendarCell : DataGridViewTextBoxCell
 763         {
 764             public CalendarCell()
 765                 : base()
 766             {
 767                 //使用简短日期格式.
 768                 this.Style.Format = "HH:mm:ss";
 769             }
 770             public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
 771             {
 772                 //将编辑控件的值设置成当前单元格的值.
 773                 base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
 774                 //
 775                 CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
 776                 try
 777                 {
 778                     ctl.Value = (DateTime)this.Value;
 779                 }
 780                 catch (Exception)
 781                 {
 782                     /*当数据库中对应日期为空(比如在添加数据时),在这里设置日期的默认值。*/
 783 
 784                     ctl.Value = Convert.ToDateTime("1990-01-01 " + this.Value);
 785                 }
 786             }
 787             public override Type EditType
 788             {
 789                 get
 790                 {
 791                     return typeof(CalendarEditingControl);
 792                 }
 793             }
 794             public override Type ValueType
 795             {
 796                 get
 797                 {
 798                     return typeof(DateTime);
 799                 }
 800             }
 801             public override object DefaultNewRowValue
 802             {
 803                 get
 804                 {
 805                     return DateTime.Now;
 806                 }
 807             }
 808         }
 809         public class CalendarColumn : DataGridViewColumn
 810         {
 811             public CalendarColumn()
 812                 : base(new CalendarCell())
 813             {
 814                 //this.CellTemplate = new CalendarCell();
 815             }
 816             public override DataGridViewCell CellTemplate
 817             {
 818                 get
 819                 {
 820                     return base.CellTemplate;
 821                 }
 822                 set
 823                 {
 824                     if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
 825                     {
 826                         throw new InvalidCastException("单元格类型必须为CalendarCell类型");
 827                     }
 828                     base.CellTemplate = value;
 829                 }
 830             }
 831         }
 832         public class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
 833         {
 834             DataGridView dataGridView;
 835             private bool valueChanged = false;
 836             int rowIndex;
 837 
 838             public CalendarEditingControl()
 839             {
 840                 this.Format = DateTimePickerFormat.Time;
 841                  this.ShowUpDown = true;
 842             }
 843 
 844             // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
 845             // property.
 846             public object EditingControlFormattedValue
 847             {
 848                 get
 849                 {
 850                     //this.Value = this.Value.ToString("HH:mm:ss");
 851                     return this.Value.ToString("HH:mm:ss");
 852                 }
 853                 set
 854                 {
 855                     String newValue = value as String;
 856                     if (newValue != null)
 857                     {
 858                         this.Value = DateTime.Parse(newValue);
 859                     }
 860                 }
 861             }
 862 
 863             // Implements the 
 864             // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
 865             public object GetEditingControlFormattedValue(
 866                 DataGridViewDataErrorContexts context)
 867             {
 868                 return EditingControlFormattedValue;
 869             }
 870 
 871             // Implements the 
 872             // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
 873             public void ApplyCellStyleToEditingControl(
 874                 DataGridViewCellStyle dataGridViewCellStyle)
 875             {
 876                 this.Font = dataGridViewCellStyle.Font;
 877                 this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
 878                 this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
 879             }
 880 
 881             // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
 882             // property.
 883             public int EditingControlRowIndex
 884             {
 885                 get
 886                 {
 887                     return rowIndex;
 888                 }
 889                 set
 890                 {
 891                     rowIndex = value;
 892                 }
 893             }
 894 
 895             // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
 896             // method.
 897             public bool EditingControlWantsInputKey(
 898                 Keys key, bool dataGridViewWantsInputKey)
 899             {
 900                 // Let the DateTimePicker handle the keys listed.
 901                 switch (key & Keys.KeyCode)
 902                 {
 903                     case Keys.Left:
 904                     case Keys.Up:
 905                     case Keys.Down:
 906                     case Keys.Right:
 907                     case Keys.Home:
 908                     case Keys.End:
 909                     case Keys.PageDown:
 910                     case Keys.PageUp:
 911                         return true;
 912                     default:
 913                         return false;
 914                 }
 915             }
 916 
 917             // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
 918             // method.
 919             public void PrepareEditingControlForEdit(bool selectAll)
 920             {
 921                 // No preparation needs to be done.
 922             }
 923 
 924             // Implements the IDataGridViewEditingControl
 925             // .RepositionEditingControlOnValueChange property.
 926             public bool RepositionEditingControlOnValueChange
 927             {
 928                 get
 929                 {
 930                     return false;
 931                 }
 932             }
 933 
 934             // Implements the IDataGridViewEditingControl
 935             // .EditingControlDataGridView property.
 936             public DataGridView EditingControlDataGridView
 937             {
 938                 get
 939                 {
 940                     return dataGridView;
 941                 }
 942                 set
 943                 {
 944                     dataGridView = value;
 945                 }
 946             }
 947 
 948             // Implements the IDataGridViewEditingControl
 949             // .EditingControlValueChanged property.
 950             public bool EditingControlValueChanged
 951             {
 952                 get
 953                 {
 954                     return valueChanged;
 955                 }
 956                 set
 957                 {
 958                     valueChanged = value;
 959                 }
 960             }
 961 
 962             // Implements the IDataGridViewEditingControl
 963             // .EditingPanelCursor property.
 964             public Cursor EditingPanelCursor
 965             {
 966                 get
 967                 {
 968                     return base.Cursor;
 969                 }
 970             }
 971 
 972             protected override void OnValueChanged(EventArgs eventargs)
 973             {
 974                 // Notify the DataGridView that the contents of the cell
 975                 // have changed.
 976                 valueChanged = true;
 977                 this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
 978                 base.OnValueChanged(eventargs);
 979             }
 980         }
 981        
 982 
 983        
 984         #endregion;
 985 
 986         #region Grid添加亮度控件
 987 
 988         public class BrighterColumn : DataGridViewTextBoxColumn
 989         {
 990             public BrighterColumn()
 991             {
 992                 this.CellTemplate = new BrighterCell();
 993             }
 994             public override DataGridViewCell CellTemplate
 995             {
 996                 get
 997                 {
 998                     return base.CellTemplate;
 999                 }
1000                 set
1001                 {
1002                     if (!(value is BrighterCell))
1003                     {
1004                         throw new InvalidCastException("DataGridViewBrighterCell");
1005                     }
1006                     base.CellTemplate = value;
1007                 }
1008             }
1009             /// <summary>
1010             /// ProgressBar最大値
1011             /// </summary>
1012             public int Maximum
1013             {
1014                 get
1015                 {
1016                     return ((BrighterCell)this.CellTemplate).Maximum;
1017                 }
1018                 set
1019                 {
1020                     if (this.Maximum == value)
1021                         return;
1022                     ((BrighterCell)this.CellTemplate).Maximum = value;
1023 
1024                     if (this.DataGridView == null)
1025                         return;
1026                     int rowCount = this.DataGridView.RowCount;
1027                     for (int i = 0; i < rowCount; i++)
1028                     {
1029                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1030                         ((BrighterCell)r.Cells[this.Index]).Maximum = value;
1031                     }
1032                 }
1033             }
1034             /// <summary>
1035             /// ProgressBar最小値
1036             /// </summary>
1037             public int Mimimum
1038             {
1039                 get
1040                 {
1041                     return ((BrighterCell)this.CellTemplate).Mimimum;
1042                 }
1043                 set
1044                 {
1045                     if (this.Mimimum == value)
1046                         return;
1047                     ((BrighterCell)this.CellTemplate).Mimimum = value;
1048                     if (this.DataGridView == null)
1049                         return;
1050                     int rowCount = this.DataGridView.RowCount;
1051                     for (int i = 0; i < rowCount; i++)
1052                     {
1053                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1054                         ((BrighterCell)r.Cells[this.Index]).Mimimum = value;
1055                     }
1056                 }
1057             }
1058             public int BarValue
1059             {
1060                 get
1061                 {
1062                     return ((BrighterCell)this.CellTemplate).BarValue;
1063                 }
1064                 set
1065                 {
1066                     if (this.BarValue == value)
1067                         return;
1068                     ((BrighterCell)this.CellTemplate).BarValue = value;
1069                     if (this.DataGridView == null)
1070                         return;
1071                     int rowCount = this.DataGridView.RowCount;
1072                     for (int i = 0; i < rowCount; i++)
1073                     {
1074                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1075                         ((BrighterCell)r.Cells[this.Index]).BarValue = value;
1076                     }
1077                 }
1078             }
1079         }
1080         /// <summary>
1081         /// ProgressBarDataGridView
1082         /// </summary>
1083         public class BrighterCell : DataGridViewTextBoxCell
1084         {
1085             public BrighterCell()
1086             {
1087                 this.maximumValue = 100;
1088                 this.mimimumValue = 0;
1089             }
1090 
1091 
1092             private int mValue = 0;
1093             public int BarValue
1094             {
1095                 get
1096                 {
1097                     return this.mValue;
1098                 }
1099                 set
1100                 {
1101                     this.mValue = value;
1102                 }
1103             }
1104 
1105             private int maximumValue;
1106             public int Maximum
1107             {
1108                 get
1109                 {
1110                     return this.maximumValue;
1111                 }
1112                 set
1113                 {
1114                     this.maximumValue = value;
1115                 }
1116             }
1117 
1118             private int mimimumValue;
1119             public int Mimimum
1120             {
1121                 get
1122                 {
1123                     return this.mimimumValue;
1124                 }
1125                 set
1126                 {
1127                     this.mimimumValue = value;
1128                 }
1129             }
1130             public override Type ValueType
1131             {
1132                 get
1133                 {
1134                     return typeof(int);
1135                 }
1136             }
1137             public override object DefaultNewRowValue
1138             {
1139                 get
1140                 {
1141                     return 0;
1142                 }
1143             }
1144             public override object Clone()
1145             {
1146                 BrighterCell cell = (BrighterCell)base.Clone();
1147                 cell.Maximum = this.Maximum;
1148                 cell.Mimimum = this.Mimimum;
1149                 return cell;
1150             }
1151 
1152             protected override void Paint(Graphics graphics,
1153                 Rectangle clipBounds, Rectangle cellBounds,
1154                 int rowIndex, DataGridViewElementStates cellState,
1155                 object value, object formattedValue, string errorText,
1156                 DataGridViewCellStyle cellStyle,
1157                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
1158                 DataGridViewPaintParts paintParts)
1159             {
1160                 int intValue = 0;
1161                 if (value is int)
1162                     intValue = (int)value;
1163                 if (intValue < this.mimimumValue)
1164                     intValue = this.mimimumValue;
1165                 if (intValue > this.maximumValue)
1166                     intValue = this.maximumValue;
1167                 if (mValue != 0)
1168                 {
1169                     intValue = mValue;
1170                 }
1171                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
1172                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
1173                 {
1174                     this.PaintBorder(graphics, clipBounds, cellBounds,
1175                         cellStyle, advancedBorderStyle);
1176                 }
1177                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
1178                 Rectangle paintRect = new Rectangle(
1179                     cellBounds.Left + borderRect.Left,
1180                     cellBounds.Top + borderRect.Top,
1181                     cellBounds.Width - borderRect.Right,
1182                     cellBounds.Height - borderRect.Bottom);
1183                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
1184                 System.Drawing.Color bkColor;
1185                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
1186                 {
1187                     bkColor = cellStyle.SelectionBackColor;
1188                 }
1189                 else
1190                 {
1191                     bkColor = cellStyle.BackColor;
1192                 }
1193                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
1194                 {
1195                     using (SolidBrush backBrush = new SolidBrush(bkColor))
1196                     {
1197                         graphics.FillRectangle(backBrush, paintRect);
1198                     }
1199                 }
1200                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
1201                 paintRect.Width -= cellStyle.Padding.Horizontal;
1202                 paintRect.Height -= cellStyle.Padding.Vertical;
1203                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
1204                 {
1205                     //Rectangle barBounds = new Rectangle(
1206                     //        paintRect.Left, paintRect.Bottom - cellBounds.Height,
1207                     //       40, cellBounds.Height);
1208                     graphics.DrawImage(Properties.Resources.brightness, paintRect.X, paintRect.Bottom -22, 20, 20);
1209 
1210                 }
1211                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
1212                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
1213                      (paintParts & DataGridViewPaintParts.Focus) ==
1214                          DataGridViewPaintParts.Focus &&
1215                      this.DataGridView.Focused)
1216                 {
1217 
1218                     Rectangle focusRect = paintRect;
1219                     focusRect.Inflate(-3, -3);
1220                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
1221                 }
1222                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
1223                 {
1224                     if (value != null)
1225                     {
1226                         
1227                         string txt = value.ToString();
1228                         TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
1229                         System.Drawing.Color fColor = cellStyle.ForeColor;
1230                         paintRect.Inflate(-2, -2);
1231                         TextRenderer.DrawText(graphics, txt, cellStyle.Font,paintRect, fColor, flags);
1232                     }
1233                 }
1234                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
1235                     DataGridViewPaintParts.ErrorIcon &&
1236                     this.DataGridView.ShowCellErrors &&
1237                     !string.IsNullOrEmpty(errorText))
1238                 {
1239                     Rectangle iconBounds = this.GetErrorIconBounds(
1240                         graphics, cellStyle, rowIndex);
1241                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
1242                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
1243                 }
1244             }
1245 
1246            
1247         }
1248         
1249         #endregion
1250 
1251         #region Grid添加音量控件
1252 
1253         public class VolumeColumn : DataGridViewTextBoxColumn
1254         {
1255             public VolumeColumn()
1256             {
1257                 this.CellTemplate = new VolumeCell();
1258             }
1259             public override DataGridViewCell CellTemplate
1260             {
1261                 get
1262                 {
1263                     return base.CellTemplate;
1264                 }
1265                 set
1266                 {
1267                     if (!(value is VolumeCell))
1268                     {
1269                         throw new InvalidCastException("DataGridViewVolumeCell");
1270                     }
1271                     base.CellTemplate = value;
1272                 }
1273             }
1274             /// <summary>
1275             /// ProgressBar最大値
1276             /// </summary>
1277             public int Maximum
1278             {
1279                 get
1280                 {
1281                     return ((VolumeCell)this.CellTemplate).Maximum;
1282                 }
1283                 set
1284                 {
1285                     if (this.Maximum == value)
1286                         return;
1287                     ((VolumeCell)this.CellTemplate).Maximum = value;
1288 
1289                     if (this.DataGridView == null)
1290                         return;
1291                     int rowCount = this.DataGridView.RowCount;
1292                     for (int i = 0; i < rowCount; i++)
1293                     {
1294                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1295                         ((VolumeCell)r.Cells[this.Index]).Maximum = value;
1296                     }
1297                 }
1298             }
1299             /// <summary>
1300             /// ProgressBar最小値
1301             /// </summary>
1302             public int Mimimum
1303             {
1304                 get
1305                 {
1306                     return ((VolumeCell)this.CellTemplate).Mimimum;
1307                 }
1308                 set
1309                 {
1310                     if (this.Mimimum == value)
1311                         return;
1312                     ((VolumeCell)this.CellTemplate).Mimimum = value;
1313                     if (this.DataGridView == null)
1314                         return;
1315                     int rowCount = this.DataGridView.RowCount;
1316                     for (int i = 0; i < rowCount; i++)
1317                     {
1318                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1319                         ((VolumeCell)r.Cells[this.Index]).Mimimum = value;
1320                     }
1321                 }
1322             }
1323             public int BarValue
1324             {
1325                 get
1326                 {
1327                     return ((VolumeCell)this.CellTemplate).BarValue;
1328                 }
1329                 set
1330                 {
1331                     if (this.BarValue == value)
1332                         return;
1333                     ((VolumeCell)this.CellTemplate).BarValue = value;
1334                     if (this.DataGridView == null)
1335                         return;
1336                     int rowCount = this.DataGridView.RowCount;
1337                     for (int i = 0; i < rowCount; i++)
1338                     {
1339                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
1340                         ((VolumeCell)r.Cells[this.Index]).BarValue = value;
1341                     }
1342                 }
1343             }
1344         }
1345         /// <summary>
1346         /// ProgressBarDataGridView
1347         /// </summary>
1348         public class VolumeCell : DataGridViewTextBoxCell
1349         {
1350             public VolumeCell()
1351             {
1352                 this.maximumValue = 100;
1353                 this.mimimumValue = 0;
1354             }
1355 
1356 
1357             private int mValue = 0;
1358             public int BarValue
1359             {
1360                 get
1361                 {
1362                     return this.mValue;
1363                 }
1364                 set
1365                 {
1366                     this.mValue = value;
1367                 }
1368             }
1369 
1370             private int maximumValue;
1371             public int Maximum
1372             {
1373                 get
1374                 {
1375                     return this.maximumValue;
1376                 }
1377                 set
1378                 {
1379                     this.maximumValue = value;
1380                 }
1381             }
1382 
1383             private int mimimumValue;
1384             public int Mimimum
1385             {
1386                 get
1387                 {
1388                     return this.mimimumValue;
1389                 }
1390                 set
1391                 {
1392                     this.mimimumValue = value;
1393                 }
1394             }
1395             public override Type ValueType
1396             {
1397                 get
1398                 {
1399                     return typeof(int);
1400                 }
1401             }
1402             public override object DefaultNewRowValue
1403             {
1404                 get
1405                 {
1406                     return 0;
1407                 }
1408             }
1409             public override object Clone()
1410             {
1411                 VolumeCell cell = (VolumeCell)base.Clone();
1412                 cell.Maximum = this.Maximum;
1413                 cell.Mimimum = this.Mimimum;
1414                 return cell;
1415             }
1416 
1417             protected override void Paint(Graphics graphics,
1418                 Rectangle clipBounds, Rectangle cellBounds,
1419                 int rowIndex, DataGridViewElementStates cellState,
1420                 object value, object formattedValue, string errorText,
1421                 DataGridViewCellStyle cellStyle,
1422                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
1423                 DataGridViewPaintParts paintParts)
1424             {
1425                 int intValue = 0;
1426                 if (value is int)
1427                     intValue = (int)value;
1428                 if (intValue < this.mimimumValue)
1429                     intValue = this.mimimumValue;
1430                 if (intValue > this.maximumValue)
1431                     intValue = this.maximumValue;
1432                 if (mValue != 0)
1433                 {
1434                     intValue = mValue;
1435                 }
1436                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
1437                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
1438                 {
1439                     this.PaintBorder(graphics, clipBounds, cellBounds,
1440                         cellStyle, advancedBorderStyle);
1441                 }
1442                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
1443                 Rectangle paintRect = new Rectangle(
1444                     cellBounds.Left + borderRect.Left,
1445                     cellBounds.Top + borderRect.Top,
1446                     cellBounds.Width - borderRect.Right,
1447                     cellBounds.Height - borderRect.Bottom);
1448                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
1449                 System.Drawing.Color bkColor;
1450                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
1451                 {
1452                     bkColor = cellStyle.SelectionBackColor;
1453                 }
1454                 else
1455                 {
1456                     bkColor = cellStyle.BackColor;
1457                 }
1458                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
1459                 {
1460                     using (SolidBrush backBrush = new SolidBrush(bkColor))
1461                     {
1462                         graphics.FillRectangle(backBrush, paintRect);
1463                     }
1464                 }
1465                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
1466                 paintRect.Width -= cellStyle.Padding.Horizontal;
1467                 paintRect.Height -= cellStyle.Padding.Vertical;
1468                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
1469                 {
1470                     //Rectangle barBounds = new Rectangle(
1471                     //        paintRect.Left, paintRect.Bottom - cellBounds.Height,
1472                     //       40, cellBounds.Height);
1473                     graphics.DrawImage(Properties.Resources.speaker, paintRect.X, paintRect.Bottom - 22, 20, 20);
1474                     //graphics.DrawImage(Properties.Resources.speaker, paintRect.X, paintRect.Bottom - cellBounds.Height, 30, cellBounds.Height);
1475                 }
1476                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
1477                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
1478                      (paintParts & DataGridViewPaintParts.Focus) ==
1479                          DataGridViewPaintParts.Focus &&
1480                      this.DataGridView.Focused)
1481                 {
1482 
1483                     Rectangle focusRect = paintRect;
1484                     focusRect.Inflate(-3, -3);
1485                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
1486                 }
1487                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
1488                 {
1489                     if (value != null)
1490                     {
1491 
1492                         string txt = value.ToString();
1493                         TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
1494                         System.Drawing.Color fColor = cellStyle.ForeColor;
1495                         paintRect.Inflate(-2, -2);
1496                         TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags);
1497                     }
1498                 }
1499                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
1500                     DataGridViewPaintParts.ErrorIcon &&
1501                     this.DataGridView.ShowCellErrors &&
1502                     !string.IsNullOrEmpty(errorText))
1503                 {
1504                     Rectangle iconBounds = this.GetErrorIconBounds(
1505                         graphics, cellStyle, rowIndex);
1506                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
1507                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
1508                 }
1509             }
1510         }
1511 
1512         #endregion
1513 
1514         #region Grid添加滑块事件
1515         public class TrackBarCell : DataGridViewTextBoxCell
1516         {
1517            
1518             public TrackBarCell()
1519                 : base()
1520             {
1521                 // Use the short date format.
1522                 this.Style.Format = "d";
1523             }
1524             public override object Clone()
1525             {
1526                 TrackBarCell cell = (TrackBarCell)base.Clone();
1527                 return cell;
1528             }
1529            
1530             public override void InitializeEditingControl(int rowIndex, object
1531                 initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
1532             {
1533                 // Set the value of the editing control to the current cell value.
1534                 base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
1535                 //将单元格的值赋给控件
1536                 TrackBarEditingControl ctl = DataGridView.EditingControl as TrackBarEditingControl;
1537                 try
1538                 {
1539                     ctl.Value = (int)this.Value;
1540                 }
1541                 catch (Exception)
1542                 {
1543                     /*当数据库中对应日期为空(比如在添加数据时),在这里设置日期的默认值。*/
1544                     ctl.Value = 50;
1545                 }
1546                 //base.InitializeEditingControl(rowIndex, initialFormattedValue,
1547                 //    dataGridViewCellStyle);
1548                 //TrackBarEditingControl ctl =
1549                 //    DataGridView.EditingControl as TrackBarEditingControl;
1550                 //ctl.Value = (int)this.Value;
1551             }
1552 
1553             public override Type EditType
1554             {
1555                 get
1556                 {
1557                     // Return the type of the editing contol that CalendarCell uses.
1558                     return typeof(TrackBarEditingControl);
1559                 }
1560             }
1561 
1562             public override Type ValueType
1563             {
1564                 get
1565                 {
1566                     // Return the type of the value that CalendarCell contains.
1567                     return typeof(int);
1568                 }
1569             }
1570 
1571             public override Type FormattedValueType
1572             {
1573                 get
1574                 {
1575                     //return typeof(int);
1576                     return base.FormattedValueType;
1577                 }
1578             }
1579             public override object DefaultNewRowValue
1580             {
1581                 get
1582                 {
1583                     // Use the current date and time as the default value.
1584                     return 0;
1585                 }
1586             }
1587             
1588            
1589            
1590         }
1591 
1592         public class TrackBarColumn : DataGridViewColumn
1593         {
1594             public TrackBarColumn()
1595                 : base(new TrackBarCell())
1596             {
1597             }
1598 
1599             public override DataGridViewCell CellTemplate
1600             {
1601                 get
1602                 {
1603                     return base.CellTemplate;
1604                 }
1605                 set
1606                 {
1607                     // Ensure that the cell used for the template is a CalendarCell.
1608                     if (value != null &&
1609                         !value.GetType().IsAssignableFrom(typeof(TrackBar)))
1610                     {
1611                         throw new InvalidCastException("Must be a TrackBarCell");
1612                     }
1613                     base.CellTemplate = value;
1614                 }
1615             }
1616 
1617            
1618         }
1619 
1620         public class TrackBarEditingControl : TrackBar, IDataGridViewEditingControl
1621         {
1622             public ToolTip TTP =new ToolTip();
1623             DataGridView dataGridView;
1624             private bool valueChanged = false;
1625             int rowIndex;
1626 
1627             public TrackBarEditingControl()
1628             {
1629                 this.Value = 0;
1630                 this.Maximum = 100;
1631                 this.Minimum = 0;
1632             }
1633 
1634             // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
1635             // property.
1636             public object EditingControlFormattedValue
1637             {
1638                 get
1639                 {
1640                     return this.Value.ToString();
1641                 }
1642                 set
1643                 {
1644                     //int newValue =(int) value ;
1645                     ////int newValue = 0;
1646                     //if (newValue != null)
1647                     //{
1648                     //    this.Value = (int)(newValue);
1649                     //}
1650                     if (value is int)
1651                     {
1652                         try
1653                         {
1654                             // This will throw an exception of the string is 
1655                             // null, empty, or not in the format of a date.
1656                             this.Value = (int)value;
1657                         }
1658                         catch
1659                         {
1660                             // In the case of an exception, just use the 
1661                             // default value so we're not left with a null
1662                             // value.
1663                             this.Value = 50;
1664                         }
1665                     }
1666                 }
1667             }
1668 
1669             // Implements the 
1670             // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
1671             public object GetEditingControlFormattedValue(
1672                 DataGridViewDataErrorContexts context)
1673             {
1674                 return EditingControlFormattedValue;
1675             }
1676 
1677             // Implements the 
1678             // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
1679             public void ApplyCellStyleToEditingControl(
1680                 DataGridViewCellStyle dataGridViewCellStyle)
1681             {
1682                 this.Font = dataGridViewCellStyle.Font;
1683                // this.l = dataGridViewCellStyle.ForeColor;
1684                 this.BackColor = dataGridViewCellStyle.BackColor;
1685             }
1686 
1687             // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
1688             // property.
1689             public int EditingControlRowIndex
1690             {
1691                 get
1692                 {
1693                     return rowIndex;
1694                 }
1695                 set
1696                 {
1697                     rowIndex = value;
1698                 }
1699             }
1700 
1701             // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
1702             // method.
1703             public bool EditingControlWantsInputKey(
1704                 Keys key, bool dataGridViewWantsInputKey)
1705             {
1706                 // Let the DateTimePicker handle the keys listed.
1707                 switch (key & Keys.KeyCode)
1708                 {
1709                     case Keys.Left:
1710                     case Keys.Up:
1711                     case Keys.Down:
1712                     case Keys.Right:
1713                     case Keys.Home:
1714                     case Keys.End:
1715                     case Keys.PageDown:
1716                     case Keys.PageUp:
1717                         return true;
1718                     default:
1719                         return false;
1720                 }
1721             }
1722 
1723             // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
1724             // method.
1725             public void PrepareEditingControlForEdit(bool selectAll)
1726             {
1727                 // No preparation needs to be done.
1728             }
1729 
1730             // Implements the IDataGridViewEditingControl
1731             // .RepositionEditingControlOnValueChange property.
1732             public bool RepositionEditingControlOnValueChange
1733             {
1734                 get
1735                 {
1736                     return false;
1737                 }
1738             }
1739 
1740             // Implements the IDataGridViewEditingControl
1741             // .EditingControlDataGridView property.
1742             public DataGridView EditingControlDataGridView
1743             {
1744                 get
1745                 {
1746                     return dataGridView;
1747                 }
1748                 set
1749                 {
1750                     dataGridView = value;
1751                 }
1752             }
1753 
1754             // Implements the IDataGridViewEditingControl
1755             // .EditingControlValueChanged property.
1756             public bool EditingControlValueChanged
1757             {
1758                 get
1759                 {
1760                     return valueChanged;
1761                 }
1762                 set
1763                 {
1764                     valueChanged = value;
1765                 }
1766             }
1767 
1768             // Implements the IDataGridViewEditingControl
1769             // .EditingPanelCursor property.
1770             public Cursor EditingPanelCursor
1771             {
1772                 get
1773                 {
1774                     return base.Cursor;
1775                 }
1776             }
1777            
1778 
1779             protected override void OnValueChanged(EventArgs eventargs)
1780             {
1781                 // Notify the DataGridView that the contents of the cell
1782                 // have changed.
1783                 valueChanged = true;
1784                 this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
1785                 
1786                 base.OnValueChanged(eventargs);
1787             }
1788 
1789 
1790             protected override void OnScroll(EventArgs e)
1791             {
1792                 valueChanged = true;
1793                 TTP.SetToolTip(this, this.Value.ToString());
1794                 base.OnScroll(e);
1795             }
1796         }
1797          #endregion
1798 
1799         #region Grid添加时间段控件
1800         public class TimeZoneColumn : DataGridViewTextBoxColumn
1801         {
1802             public TimeZoneColumn()
1803             {
1804                 this.CellTemplate = new TimeZoneCell();
1805             }
1806         }
1807         public class TimeZoneCell : DataGridViewTextBoxCell
1808         {
1809             public TimeZoneCell()
1810                 : base()
1811             {
1812 
1813             }
1814             public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
1815             {
1816                 //将编辑控件的值设置成当前单元格的值.
1817                 base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
1818                 //
1819                 TimeZoneEditingControl ctl = DataGridView.EditingControl as TimeZoneEditingControl;
1820                 try
1821                 {
1822                     //ctl.Value = (DateTime)this.Value;
1823                 }
1824                 catch (Exception)
1825                 {
1826                     /*当数据库中对应日期为空(比如在添加数据时),在这里设置日期的默认值。*/
1827 
1828                     //ctl.Value = Convert.ToDateTime("1990-01-01 " + this.Value);
1829                 }
1830             }
1831             public override Type EditType
1832             {
1833                 get
1834                 {
1835                     return typeof(TimeZoneEditingControl);
1836                 }
1837             }
1838             public override Type ValueType
1839             {
1840                 get
1841                 {
1842                     return typeof(DateTime);
1843                 }
1844             }
1845             public override object DefaultNewRowValue
1846             {
1847                 get
1848                 {
1849                     return DateTime.Now;
1850                 }
1851             }
1852         }
1853 
1854         public class TimeZoneEditingControl : TimeZone, IDataGridViewEditingControl
1855         {
1856             DataGridView dataGridView;
1857             private bool valueChanged = false;
1858             int rowIndex;
1859 
1860             public TimeZoneEditingControl()
1861             {
1862                 //this.Format = DateTimePickerFormat.Time;
1863                 //this.ShowUpDown = true;
1864             }
1865 
1866             // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
1867             // property.
1868             public object EditingControlFormattedValue
1869             {
1870                 get
1871                 {
1872                     //return this.Value.ToString("HH:mm:ss");
1873                     return 0;
1874                 }
1875                 set
1876                 {
1877                     String newValue = value as String;
1878                     if (newValue != null)
1879                     {
1880                         //this.Value = DateTime.Parse(newValue);
1881                     }
1882                 }
1883             }
1884 
1885             // Implements the 
1886             // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
1887             public object GetEditingControlFormattedValue(
1888                 DataGridViewDataErrorContexts context)
1889             {
1890                  return EditingControlFormattedValue;
1891             }
1892 
1893             // Implements the 
1894             // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
1895             public void ApplyCellStyleToEditingControl(
1896                 DataGridViewCellStyle dataGridViewCellStyle)
1897             {
1898                 this.Font = dataGridViewCellStyle.Font;
1899                // this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
1900                 //this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
1901             }
1902 
1903             // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
1904             // property.
1905             public int EditingControlRowIndex
1906             {
1907                 get
1908                 {
1909                     return rowIndex;
1910                 }
1911                 set
1912                 {
1913                     rowIndex = value;
1914                 }
1915             }
1916 
1917             // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
1918             // method.
1919             public bool EditingControlWantsInputKey(
1920                 Keys key, bool dataGridViewWantsInputKey)
1921             {
1922                 // Let the DateTimePicker handle the keys listed.
1923                 switch (key & Keys.KeyCode)
1924                 {
1925                     case Keys.Left:
1926                     case Keys.Up:
1927                     case Keys.Down:
1928                     case Keys.Right:
1929                     case Keys.Home:
1930                     case Keys.End:
1931                     case Keys.PageDown:
1932                     case Keys.PageUp:
1933                         return true;
1934                     default:
1935                         return false;
1936                 }
1937             }
1938 
1939             // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
1940             // method.
1941             public void PrepareEditingControlForEdit(bool selectAll)
1942             {
1943                 // No preparation needs to be done.
1944             }
1945 
1946             // Implements the IDataGridViewEditingControl
1947             // .RepositionEditingControlOnValueChange property.
1948             public bool RepositionEditingControlOnValueChange
1949             {
1950                 get
1951                 {
1952                     return false;
1953                 }
1954             }
1955 
1956             // Implements the IDataGridViewEditingControl
1957             // .EditingControlDataGridView property.
1958             public DataGridView EditingControlDataGridView
1959             {
1960                 get
1961                 {
1962                     return dataGridView;
1963                 }
1964                 set
1965                 {
1966                     dataGridView = value;
1967                 }
1968             }
1969 
1970             // Implements the IDataGridViewEditingControl
1971             // .EditingControlValueChanged property.
1972             public bool EditingControlValueChanged
1973             {
1974                 get
1975                 {
1976                     return valueChanged;
1977                 }
1978                 set
1979                 {
1980                     valueChanged = value;
1981                 }
1982             }
1983 
1984             // Implements the IDataGridViewEditingControl
1985             // .EditingPanelCursor property.
1986             public Cursor EditingPanelCursor
1987             {
1988                 get
1989                 {
1990                     return base.Cursor;
1991                 }
1992             }
1993 
1994             
1995         }
1996         #endregion
1997 
1998         #region Grid添加调节音量带滑块
1999 
2000         public class VolumeTrackBarColumn : DataGridViewTextBoxColumn
2001         {
2002             public VolumeTrackBarColumn()
2003             {
2004                 this.CellTemplate = new VolumeTrackBarCell();
2005             }
2006             public override DataGridViewCell CellTemplate
2007             {
2008                 get
2009                 {
2010                     return base.CellTemplate;
2011                 }
2012                 set
2013                 {
2014                     if (!(value is VolumeTrackBarCell))
2015                     {
2016                         throw new InvalidCastException("DataGridViewVolumeCell");
2017                     }
2018                     base.CellTemplate = value;
2019                 }
2020             }
2021             /// <summary>
2022             /// ProgressBar最大値
2023             /// </summary>
2024             public int Maximum
2025             {
2026                 get
2027                 {
2028                     return ((VolumeCell)this.CellTemplate).Maximum;
2029                 }
2030                 set
2031                 {
2032                     if (this.Maximum == value)
2033                         return;
2034                     ((VolumeCell)this.CellTemplate).Maximum = value;
2035 
2036                     if (this.DataGridView == null)
2037                         return;
2038                     int rowCount = this.DataGridView.RowCount;
2039                     for (int i = 0; i < rowCount; i++)
2040                     {
2041                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2042                         ((VolumeCell)r.Cells[this.Index]).Maximum = value;
2043                     }
2044                 }
2045             }
2046             /// <summary>
2047             /// ProgressBar最小値
2048             /// </summary>
2049             public int Mimimum
2050             {
2051                 get
2052                 {
2053                     return ((VolumeCell)this.CellTemplate).Mimimum;
2054                 }
2055                 set
2056                 {
2057                     if (this.Mimimum == value)
2058                         return;
2059                     ((VolumeCell)this.CellTemplate).Mimimum = value;
2060                     if (this.DataGridView == null)
2061                         return;
2062                     int rowCount = this.DataGridView.RowCount;
2063                     for (int i = 0; i < rowCount; i++)
2064                     {
2065                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2066                         ((VolumeCell)r.Cells[this.Index]).Mimimum = value;
2067                     }
2068                 }
2069             }
2070             public int BarValue
2071             {
2072                 get
2073                 {
2074                     return ((VolumeCell)this.CellTemplate).BarValue;
2075                 }
2076                 set
2077                 {
2078                     if (this.BarValue == value)
2079                         return;
2080                     ((VolumeCell)this.CellTemplate).BarValue = value;
2081                     if (this.DataGridView == null)
2082                         return;
2083                     int rowCount = this.DataGridView.RowCount;
2084                     for (int i = 0; i < rowCount; i++)
2085                     {
2086                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2087                         ((VolumeCell)r.Cells[this.Index]).BarValue = value;
2088                     }
2089                 }
2090             }
2091         }
2092         /// <summary>
2093         /// ProgressBarDataGridView
2094         /// </summary>
2095         public class VolumeTrackBarCell : TrackBarCell
2096         {
2097             public VolumeTrackBarCell()
2098             {
2099                 this.maximumValue = 100;
2100                 this.mimimumValue = 0;
2101             }
2102 
2103 
2104             private int mValue = 0;
2105             public int BarValue
2106             {
2107                 get
2108                 {
2109                     return this.mValue;
2110                 }
2111                 set
2112                 {
2113                     this.mValue = value;
2114                 }
2115             }
2116 
2117             private int maximumValue;
2118             public int Maximum
2119             {
2120                 get
2121                 {
2122                     return this.maximumValue;
2123                 }
2124                 set
2125                 {
2126                     this.maximumValue = value;
2127                 }
2128             }
2129 
2130             private int mimimumValue;
2131             public int Mimimum
2132             {
2133                 get
2134                 {
2135                     return this.mimimumValue;
2136                 }
2137                 set
2138                 {
2139                     this.mimimumValue = value;
2140                 }
2141             }
2142             public override Type ValueType
2143             {
2144                 get
2145                 {
2146                     return typeof(int);
2147                 }
2148             }
2149             public override object DefaultNewRowValue
2150             {
2151                 get
2152                 {
2153                     return 0;
2154                 }
2155             }
2156             public override object Clone()
2157             {
2158                 VolumeTrackBarCell cell = (VolumeTrackBarCell)base.Clone();
2159                 cell.Maximum = this.Maximum;
2160                 cell.Mimimum = this.Mimimum;
2161                 return cell;
2162             }
2163 
2164             protected override void Paint(Graphics graphics,
2165                 Rectangle clipBounds, Rectangle cellBounds,
2166                 int rowIndex, DataGridViewElementStates cellState,
2167                 object value, object formattedValue, string errorText,
2168                 DataGridViewCellStyle cellStyle,
2169                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
2170                 DataGridViewPaintParts paintParts)
2171             {
2172                 int intValue = 0;
2173                 if (value is int)
2174                     intValue = (int)value;
2175                 if (intValue < this.mimimumValue)
2176                     intValue = this.mimimumValue;
2177                 if (intValue > this.maximumValue)
2178                     intValue = this.maximumValue;
2179                 if (mValue != 0)
2180                 {
2181                     intValue = mValue;
2182                 }
2183                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
2184                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
2185                 {
2186                     this.PaintBorder(graphics, clipBounds, cellBounds,
2187                         cellStyle, advancedBorderStyle);
2188                 }
2189                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
2190                 Rectangle paintRect = new Rectangle(
2191                     cellBounds.Left + borderRect.Left,
2192                     cellBounds.Top + borderRect.Top,
2193                     cellBounds.Width - borderRect.Right,
2194                     cellBounds.Height - borderRect.Bottom);
2195                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
2196                 System.Drawing.Color bkColor;
2197                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
2198                 {
2199                     bkColor = cellStyle.SelectionBackColor;
2200                 }
2201                 else
2202                 {
2203                     bkColor = cellStyle.BackColor;
2204                 }
2205                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
2206                 {
2207                     using (SolidBrush backBrush = new SolidBrush(bkColor))
2208                     {
2209                         graphics.FillRectangle(backBrush, paintRect);
2210                     }
2211                 }
2212                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
2213                 paintRect.Width -= cellStyle.Padding.Horizontal;
2214                 paintRect.Height -= cellStyle.Padding.Vertical;
2215                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
2216                 {
2217                     //Rectangle barBounds = new Rectangle(
2218                     //        paintRect.Left, paintRect.Bottom - cellBounds.Height,
2219                     //       40, cellBounds.Height);
2220                     graphics.DrawImage(Properties.Resources.speaker, paintRect.X, paintRect.Bottom - cellBounds.Height, 40, cellBounds.Height);
2221 
2222                 }
2223                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
2224                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
2225                      (paintParts & DataGridViewPaintParts.Focus) ==
2226                          DataGridViewPaintParts.Focus &&
2227                      this.DataGridView.Focused)
2228                 {
2229 
2230                     Rectangle focusRect = paintRect;
2231                     focusRect.Inflate(-3, -3);
2232                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
2233                 }
2234                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
2235                 {
2236                     if (value != null)
2237                     {
2238 
2239                         string txt = value.ToString();
2240                         TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
2241                         System.Drawing.Color fColor = cellStyle.ForeColor;
2242                         paintRect.Inflate(-2, -2);
2243                         TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags);
2244                     }
2245                 }
2246                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
2247                     DataGridViewPaintParts.ErrorIcon &&
2248                     this.DataGridView.ShowCellErrors &&
2249                     !string.IsNullOrEmpty(errorText))
2250                 {
2251                     Rectangle iconBounds = this.GetErrorIconBounds(
2252                         graphics, cellStyle, rowIndex);
2253                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
2254                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
2255                 }
2256             }
2257         }
2258 
2259         #endregion
2260 
2261         #region Grid添加亮度滑块控件
2262 
2263         public class BrighterTrackBarColumn : DataGridViewTextBoxColumn
2264         {
2265             public BrighterTrackBarColumn()
2266             {
2267                 this.CellTemplate = new BrighterTrackBarCell();
2268             }
2269             public override DataGridViewCell CellTemplate
2270             {
2271                 get
2272                 {
2273                     return base.CellTemplate;
2274                 }
2275                 set
2276                 {
2277                     if (!(value is BrighterTrackBarCell))
2278                     {
2279                         throw new InvalidCastException("DataGridViewBrighterCell");
2280                     }
2281                     base.CellTemplate = value;
2282                 }
2283             }
2284             /// <summary>
2285             /// ProgressBar最大値
2286             /// </summary>
2287             public int Maximum
2288             {
2289                 get
2290                 {
2291                     return ((BrighterTrackBarCell)this.CellTemplate).Maximum;
2292                 }
2293                 set
2294                 {
2295                     if (this.Maximum == value)
2296                         return;
2297                     ((BrighterTrackBarCell)this.CellTemplate).Maximum = value;
2298 
2299                     if (this.DataGridView == null)
2300                         return;
2301                     int rowCount = this.DataGridView.RowCount;
2302                     for (int i = 0; i < rowCount; i++)
2303                     {
2304                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2305                         ((BrighterTrackBarCell)r.Cells[this.Index]).Maximum = value;
2306                     }
2307                 }
2308             }
2309             /// <summary>
2310             /// ProgressBar最小値
2311             /// </summary>
2312             public int Mimimum
2313             {
2314                 get
2315                 {
2316                     return ((BrighterTrackBarCell)this.CellTemplate).Mimimum;
2317                 }
2318                 set
2319                 {
2320                     if (this.Mimimum == value)
2321                         return;
2322                     ((BrighterTrackBarCell)this.CellTemplate).Mimimum = value;
2323                     if (this.DataGridView == null)
2324                         return;
2325                     int rowCount = this.DataGridView.RowCount;
2326                     for (int i = 0; i < rowCount; i++)
2327                     {
2328                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2329                         ((BrighterTrackBarCell)r.Cells[this.Index]).Mimimum = value;
2330                     }
2331                 }
2332             }
2333             public int BarValue
2334             {
2335                 get
2336                 {
2337                     return ((BrighterTrackBarCell)this.CellTemplate).BarValue;
2338                 }
2339                 set
2340                 {
2341                     if (this.BarValue == value)
2342                         return;
2343                     ((BrighterTrackBarCell)this.CellTemplate).BarValue = value;
2344                     if (this.DataGridView == null)
2345                         return;
2346                     int rowCount = this.DataGridView.RowCount;
2347                     for (int i = 0; i < rowCount; i++)
2348                     {
2349                         DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
2350                         ((BrighterTrackBarCell)r.Cells[this.Index]).BarValue = value;
2351                     }
2352                 }
2353             }
2354         }
2355         /// <summary>
2356         /// ProgressBarDataGridView
2357         /// </summary>
2358         public class BrighterTrackBarCell : TrackBarCell
2359         {
2360             public BrighterTrackBarCell()
2361             {
2362                 this.maximumValue = 100;
2363                 this.mimimumValue = 0;
2364             }
2365 
2366 
2367             private int mValue = 0;
2368             public int BarValue
2369             {
2370                 get
2371                 {
2372                     return this.mValue;
2373                 }
2374                 set
2375                 {
2376                     this.mValue = value;
2377                 }
2378             }
2379 
2380 
2381          
2382             private int maximumValue;
2383             public int Maximum
2384             {
2385                 get
2386                 {
2387                     return this.maximumValue;
2388                 }
2389                 set
2390                 {
2391                     this.maximumValue = value;
2392                 }
2393             }
2394 
2395             private int mimimumValue;
2396             public int Mimimum
2397             {
2398                 get
2399                 {
2400                     return this.mimimumValue;
2401                 }
2402                 set
2403                 {
2404                     this.mimimumValue = value;
2405                 }
2406             }
2407             public override Type ValueType
2408             {
2409                 get
2410                 {
2411                     return typeof(int);
2412                 }
2413             }
2414             public override object DefaultNewRowValue
2415             {
2416                 get
2417                 {
2418                     return 0;
2419                 }
2420             }
2421             public override object Clone()
2422             {
2423                 BrighterTrackBarCell cell = (BrighterTrackBarCell)base.Clone();
2424                 cell.Maximum = this.Maximum;
2425                 cell.Mimimum = this.Mimimum;
2426                 return cell;
2427             }
2428             protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
2429             {
2430                 return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
2431             }
2432 
2433             
2434             protected override void Paint(Graphics graphics,
2435                 Rectangle clipBounds, Rectangle cellBounds,
2436                 int rowIndex, DataGridViewElementStates cellState,
2437                 object value, object formattedValue, string errorText,
2438                 DataGridViewCellStyle cellStyle,
2439                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
2440                 DataGridViewPaintParts paintParts)
2441             {
2442                 value = formattedValue;
2443                 int intValue = 0;
2444                 if (value is int)
2445                     intValue = (int)value;
2446                 if (intValue < this.mimimumValue)
2447                     intValue = this.mimimumValue;
2448                 if (intValue > this.maximumValue)
2449                     intValue = this.maximumValue;
2450                 if (mValue != 0)
2451                 {
2452                     intValue = mValue;
2453                 }
2454                 double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);
2455                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
2456                 {
2457                     this.PaintBorder(graphics, clipBounds, cellBounds,
2458                         cellStyle, advancedBorderStyle);
2459                 }
2460                 Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
2461                 Rectangle paintRect = new Rectangle(
2462                     cellBounds.Left + borderRect.Left,
2463                     cellBounds.Top + borderRect.Top,
2464                     cellBounds.Width - borderRect.Right,
2465                     cellBounds.Height - borderRect.Bottom);
2466                 bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
2467                 System.Drawing.Color bkColor;
2468                 if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
2469                 {
2470                     bkColor = cellStyle.SelectionBackColor;
2471                 }
2472                 else
2473                 {
2474                     bkColor = cellStyle.BackColor;
2475                 }
2476                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
2477                 {
2478                     using (SolidBrush backBrush = new SolidBrush(bkColor))
2479                     {
2480                         graphics.FillRectangle(backBrush, paintRect);
2481                     }
2482                 }
2483                 paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
2484                 paintRect.Width -= cellStyle.Padding.Horizontal;
2485                 paintRect.Height -= cellStyle.Padding.Vertical;
2486                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
2487                 {
2488                     //Rectangle barBounds = new Rectangle(
2489                     //        paintRect.Left, paintRect.Bottom - cellBounds.Height,
2490                     //       40, cellBounds.Height);
2491                     graphics.DrawImage(Properties.Resources.brightness, paintRect.X, paintRect.Bottom - 22, 20, 20);
2492 
2493                 }
2494                 if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
2495                      this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
2496                      (paintParts & DataGridViewPaintParts.Focus) ==
2497                          DataGridViewPaintParts.Focus &&
2498                      this.DataGridView.Focused)
2499                 {
2500 
2501                     Rectangle focusRect = paintRect;
2502                     focusRect.Inflate(-3, -3);
2503                     ControlPaint.DrawFocusRectangle(graphics, focusRect);
2504                 }
2505                 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
2506                 {
2507                     if (value != null)
2508                     {
2509 
2510                         string txt = value.ToString();
2511                         TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
2512                         System.Drawing.Color fColor = cellStyle.ForeColor;
2513                         paintRect.Inflate(-2, -2);
2514                         TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags);
2515                     }
2516                 }
2517                 if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
2518                     DataGridViewPaintParts.ErrorIcon &&
2519                     this.DataGridView.ShowCellErrors &&
2520                     !string.IsNullOrEmpty(errorText))
2521                 {
2522                     Rectangle iconBounds = this.GetErrorIconBounds(
2523                         graphics, cellStyle, rowIndex);
2524                     iconBounds.Offset(cellBounds.X, cellBounds.Y);
2525                     this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
2526                 }
2527             }
2528 
2529           
2530         }
2531 
2532         #endregion
2533 
2534         #region
2535         public delegate void DataGridViewCheckBoxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);
2536 
2537 
2538         public class datagridviewCheckboxHeaderEventArgs : EventArgs
2539         {
2540             private bool checkedState = false;
2541 
2542             public bool CheckedState
2543             {
2544                 get { return checkedState; }
2545                 set { checkedState = value; }
2546             }
2547         }
2548 
2549         public class DataGridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
2550         {
2551             Point checkBoxLocation;
2552             Size checkBoxSize;
2553             public bool _checked = false;
2554             Point _cellLocation = new Point();
2555             System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
2556                 System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
2557             public event DataGridViewCheckBoxHeaderEventHander OnCheckBoxClicked;
2558 
2559             protected override void Paint(
2560                 Graphics graphics,
2561                 Rectangle clipBounds,
2562                 Rectangle cellBounds,
2563                 int rowIndex,
2564                 DataGridViewElementStates dataGridViewElementState,
2565                 object value,
2566                 object formattedValue,
2567                 string errorText,
2568                 DataGridViewCellStyle cellStyle,
2569                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
2570                 DataGridViewPaintParts paintParts)
2571             {
2572                 //Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
2573                 //Rectangle paintRect = new Rectangle(
2574                 // cellBounds.Left + borderRect.Left,
2575                 // cellBounds.Top + borderRect.Top,
2576                 // cellBounds.Width - borderRect.Right,
2577                 // cellBounds.Height - borderRect.Bottom);
2578                 //string txt = value.ToString();
2579                 //TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.Left;
2580                 //System.Drawing.Color fColor = cellStyle.ForeColor;
2581                 //paintRect.Inflate(-2, -2);
2582                 //TextRenderer.DrawText(graphics, txt, cellStyle.Font,
2583                 //    paintRect, fColor, flags);
2584                 //value = "";
2585                 //formattedValue = "";
2586                 //cellBounds.Width = 40;
2587                 cellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
2588                 base.Paint(graphics, clipBounds, cellBounds, rowIndex,
2589                     dataGridViewElementState, value,
2590                     formattedValue, errorText, cellStyle,
2591                     advancedBorderStyle, paintParts);
2592 
2593                 Point p = new Point();
2594                 Size s = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal);
2595 
2596                 //p.X = cellBounds.Location.X +
2597                 //    (cellBounds.Width / 2) - (s.Width / 2) - 1;
2598                 //p.Y = cellBounds.Location.Y +
2599                 //    (cellBounds.Height / 2) - (s.Height / 2);
2600                 p.X = cellBounds.Location.X + (cellBounds.Width / 8);
2601                 //p.X = cellBounds.Location.X ;
2602                 p.Y = cellBounds.Location.Y +
2603                     (cellBounds.Height / 2) - (s.Height / 2);
2604 
2605                 _cellLocation = cellBounds.Location;
2606                 checkBoxLocation = p;
2607                 checkBoxSize = s;
2608                 if (_checked)
2609                     _cbState = System.Windows.Forms.VisualStyles.
2610                         CheckBoxState.CheckedNormal;
2611                 else
2612                     _cbState = System.Windows.Forms.VisualStyles.
2613                         CheckBoxState.UncheckedNormal;
2614 
2615                 CheckBoxRenderer.DrawCheckBox
2616                 (graphics, checkBoxLocation, _cbState);
2617 
2618 
2619             }
2620 
2621             protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
2622             {
2623                 Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
2624                 if (p.X >= checkBoxLocation.X && p.X <=
2625                     checkBoxLocation.X + checkBoxSize.Width
2626                 && p.Y >= checkBoxLocation.Y && p.Y <=
2627                     checkBoxLocation.Y + checkBoxSize.Height)
2628                 {
2629                     _checked = !_checked;
2630 
2631                     datagridviewCheckboxHeaderEventArgs ex = new datagridviewCheckboxHeaderEventArgs();
2632                     ex.CheckedState = _checked;
2633 
2634                     object sender = new object();
2635 
2636                     if (OnCheckBoxClicked != null)
2637                     {
2638                         OnCheckBoxClicked(sender, ex);
2639                         this.DataGridView.InvalidateCell(this);
2640                     }
2641                 }
2642                 base.OnMouseClick(e);
2643             }
2644         }
2645         #endregion
2649     }

在调用时可以采用以下代码:
 1 /// <summary>
 2         /// 初始化终端列表显示
 3         /// </summary>
 4         private void InitGrid()
 5         {
 6             int colWith = this.Grid.Width / 2;
 7             Grid.Columns.Clear();
 8             int i = 0;
 9             clsWindowGird.Editcol(i++, Grid, "^选择", "选择", 50, true, false, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enCheckBox);
10             clsWindowGird.Editcol(i++, Grid, "^播放器", "ID", 70, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
11             clsWindowGird.Editcol(i++, Grid, "^IP地址", "IP地址", 150, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
12             clsWindowGird.Editcol(i++, Grid, "^端口号", "端口号", 70, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
13            
14             clsWindowGird.Editcol(i++, Grid, "^播放器", "MAC地址", 110, true, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
15 
16             clsWindowGird.Editcol(i++, Grid, "^音量", "音量", colWith, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enVolumeTrackBar);
17             clsWindowGird.Editcol(i++, Grid, "^亮度", "亮度", colWith, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enBrighterTrackBar);
18             clsWindowGird.Editcol(i++, Grid, "^工作状态", "工作状态", 100, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
19             clsWindowGird.Editcol(i++, Grid, "^进度", "状态", 90, true, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enProgressBar);
20         
21             clsWindowGird.Editcol(i++, Grid, "^进度", "进度", 150, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enProgressBar);
22           
23      
24             clsWindowGird.Editcol(i++, Grid, "^标志", "标志", 150, false, true, DataGridViewColumnSortMode.NotSortable, clsWindowGird.GridColumnType.enTextBox);
25            
26            
27         }

 

 

转载于:https://www.cnblogs.com/lvshuyi/p/6772673.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值