在DBGrid里加入CheckBox可视化组件

转自 http://mydelphi.5d6d.com/thread-302-1-2.html

注意要USES DB 单元

主要代码:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
   DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
   CtrlState: array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
begin
   if Column.Field.DataType = ftBoolean then
   begin
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle,Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]);
   end;
end;

全部示列单元文件内容:

 

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, DBClient, ExtCtrls, DBCtrls;

type
TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    ClientDataSet1Value: TBooleanField;
    DBNavigator1: TDBNavigator;
    ClientDataSet1ID: TAutoIncField;
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid1CellClick(Column: TColumn);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1             : TForm1;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
CtrlState: array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
begin

if Column.Field.DataType = ftBoolean then
begin
    DBGrid1.Canvas.FillRect(Rect);
    DrawFrameControl(DBGrid1.Canvas.Handle,Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]);
end;

end;

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
   if DBGrid1.SelectedField.DataType = ftBoolean then
   begin
     DBGrid1.SelectedField.Dataset.Edit;
     DBGrid1.SelectedField.AsBoolean :=not DBGrid1.SelectedField.AsBoolean;
     DBGrid1.SelectedField.Dataset.Post;
   end;
end;

end.

参数强解(英文原HELP)

DrawFrameControlThe DrawFrameControl function draws a frame control of the specified type and style.
BOOL DrawFrameControl(   HDC [url="]hdc[/url] ,     // handle to device context    LPRECT [url="]lprc[/url] , // bounding rectangle    UINT [url="]uType[/url] ,  // frame-control type    UINT [url="]uState[/url]  // frame-control state );
Parameters hdc [in] Handle to the device context of the window in which to draw the control. lprc [in] Pointer to a [url=ms-help://borland.bds5/Gdi/rectangl_6cqa.htm] RECT[/url] structure that contains the logical coordinates of the bounding rectangle for frame control. uType [in] Specifies the type of frame control to draw. This parameter can be one of the following values.
ValueMeaning
DFC_BUTTONStandard button
DFC_CAPTIONTitle bar
DFC_MENUMenu bar
DFC_POPUPMENUWindows 98/Me, Windows 2000/XP: Popup menu item.
DFC_SCROLLScroll bar

uState [in] Specifies the initial state of the frame control. If uType is DFC_BUTTON, uState can be one of the following values.
ValueMeaning
DFCS_BUTTON3STATEThree-state button
DFCS_BUTTONCHECKCheck box
DFCS_BUTTONPUSHPush button
DFCS_BUTTONRADIORadio button
DFCS_BUTTONRADIOIMAGEImage for radio button (nonsquare needs image)
DFCS_BUTTONRADIOMASKMask for radio button (nonsquare needs mask)

If uType is DFC_CAPTION, uState can be one of the following values.
ValueMeaning
DFCS_CAPTIONCLOSEClose button
DFCS_CAPTIONHELPHelp button
DFCS_CAPTIONMAXMaximize button
DFCS_CAPTIONMINMinimize button
DFCS_CAPTIONRESTORERestore button

If uType is DFC_MENU, uState can be one of the following values.
ValueMeaning
DFCS_MENUARROWSubmenu arrow
DFCS_MENUARROWRIGHTSubmenu arrow pointing left. This is used for the right-to-left cascading menus used with right-to-left languages such as Arabic or Hebrew.
DFCS_MENUBULLETBullet
DFCS_MENUCHECKCheck mark

If uType is DFC_SCROLL, uState can be one of the following values.
ValueMeaning
DFCS_SCROLLCOMBOBOXCombo box scroll bar
DFCS_SCROLLDOWNDown arrow of scroll bar
DFCS_SCROLLLEFTLeft arrow of scroll bar
DFCS_SCROLLRIGHTRight arrow of scroll bar
DFCS_SCROLLSIZEGRIPSize grip in bottom-right corner of window
DFCS_SCROLLSIZEGRIPRIGHTSize grip in bottom-left corner of window. This is used with right-to-left languages such as Arabic or Hebrew.
DFCS_SCROLLUPUp arrow of scroll bar

The following style can be used to adjust the bounding rectangle of the push button.
ValueMeaning
DFCS_ADJUSTRECTBounding rectangle is adjusted to exclude the surrounding edge of the push button.

One or more of the following values can be used to set the state of the control to be drawn.
ValueMeaning
DFCS_CHECKEDButton is checked.
DFCS_FLATButton has a flat border.
DFCS_HOTWindows 98/Me, Windows 2000/XP: Button is hot-tracked.
DFCS_INACTIVEButton is inactive (grayed).
DFCS_MONOButton has a monochrome border.
DFCS_PUSHEDButton is pushed.
DFCS_TRANSPARENTWindows 98/Me, Windows 2000/XP: The background remains untouched.

转载于:https://www.cnblogs.com/fubin/archive/2008/10/01/1302784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值