参考文章:
http://topic.youkuaiyun.com/t/20030625/10/1954419.html
http://bcbjournal.org/articles/vol4/0009/Working_with_string_grids.htm
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22691390.html
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21945304.html
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20539050.html?sfQueryTermInfo=1+checkbox+combobox+stringgrid
http://www.xawk.com/delphi-hax252.html
http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=30547
http://delphi.ktop.com.tw/board.php?cid=30&fid=69&tid=77780
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21905652.html
http://cc.codegear.com/Item.aspx?id=15441
.cpp
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
SetCheckBox(*StringGrid1,1,1,true,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
bool Checked;
if( (ACol == 2) && (ARow > 0)){
InflateRect(&Rect, -2, -2);
if (StringGrid1->Cells[ACol][ARow] == 't'){
Checked = false;
}else{
Checked = true;
}
if(StringGrid1->Canvas){
StringGrid1->Canvas->FillRect(Rect);
if(Checked){
DrawFrameControl(StringGrid1->Canvas->Handle, &Rect, DFC_SCROLL , DFCS_SCROLLDOWN );
DrawFrameControl(StringGrid1->Canvas->Handle, &Rect, DFC_SCROLL , DFCS_SCROLLDOWN );
}else{
DrawFrameControl(StringGrid1->Canvas->Handle, &Rect, DFC_BUTTON, DFCS_SCROLLLEFT | DFCS_ADJUSTRECT | DFCS_FLAT);
}
}
}
TStringGrid* StringGrid =static_cast
(Sender);
RECT RText = static_cast
(Rect);
const AnsiString text(StringGrid->Cells[ACol][ARow]);
TCanvas* SGCanvas =StringGrid->Canvas;
// if this cell contains a checkbox
if(GetCheckBox(*StringGrid, ACol, ARow)) {
// set the flags for rendering
// checked/unchecked
unsigned int state =DFCS_BUTTONCHECK;
if(GetCheckState(*StringGrid, ACol, ARow)) {
state = state | DFCS_CHECKED;
}
// size the checkbox
RECT RCell =static_cast
(Rect);
OffsetRect(&RCell, 2,0.5 * (RCell.bottom - RCell.top));
RCell.right = RCell.left +GetSystemMetrics(SM_CXMENUCHECK);
//RCell.bottom = RCell.top +GetSystemMetrics(SM_CYMENUCHECK);
RCell.top -= 0.5 *(RCell.bottom - RCell.top) + 2;
// draw the checkbox
//DrawFrameControl(StringGrid1->Canvas->Handle,&RCell, DFC_BUTTON, state);
//DrawFrameControl(StringGrid1->Canvas->Handle,&RCell, DFC_BUTTON , DFCS_BUTTON3STATE );
//StringGrid1->Canvas->Brush->Color = clBlack;
//FrameRect(StringGrid1->Canvas->Handle , &RCell , StringGrid1->Canvas->Brush);
//DrawFocusRect(StringGrid1->Canvas->Handle , &RCell);
StringGrid1->Canvas->TextRect(RCell, 10, 10, "Hello World");
StringGrid1->Canvas->Brush->Color = clBlack;
StringGrid1->Canvas->FrameRect(RCell);
// move the text over
RText.left = RCell.right;
}
// draw the text
RText.left += 2; RText.top += 2;
DrawText(SGCanvas->Handle,text.c_str(), text.Length(), &RText,DT_LEFT |DT_VCENTER |DT_SINGLELINE);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::GetCheckState(TStringGrid& AGrid, int ACol,int ARow)
{
return HIWORD(reinterpret_cast
(AGrid.Objects[ACol][ARow]));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetCheckState(TStringGrid& AGrid,int ACol, int ARow, bool AChecked)
{
long data = reinterpret_cast
(AGrid.Objects[ACol][ARow]); AGrid.Objects[ACol][ARow] =reinterpret_cast
(MAKELONG(LOWORD(data), AChecked)); } //--------------------------------------------------------------------------- bool __fastcall TForm1::GetCheckBox(TStringGrid& AGrid, int ACol, int ARow) { return LOWORD(reinterpret_cast
(AGrid.Objects[ACol][ARow])); } //--------------------------------------------------------------------------- void __fastcall TForm1::SetCheckBox(TStringGrid& AGrid, int ACol, int ARow,bool AShow, bool AChecked) { AGrid.Objects[ACol][ARow] =reinterpret_cast
(MAKELONG(AShow, false)); SetCheckState(AGrid, ACol, ARow, AChecked); } //---------------------------------------------------------------------------
.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TStringGrid *StringGrid1; TListView *ListView1; void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); bool __fastcall GetCheckState(TStringGrid& AGrid, int ACol,int ARow); void __fastcall SetCheckState(TStringGrid& AGrid,int ACol, int ARow, bool AChecked); bool __fastcall GetCheckBox(TStringGrid& AGrid, int ACol, int ARow); void __fastcall SetCheckBox(TStringGrid& AGrid, int ACol, int ARow,bool AShow, bool AChecked); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
本文介绍了一个使用 Delphi 实现带有复选框功能的 StringGrid 控件的方法。通过自定义绘制单元格的方式,实现了在 StringGrid 的指定列显示复选框,并提供了 Get 和 Set 方法来控制复选框的状态。
4612

被折叠的 条评论
为什么被折叠?



