如何在TStringGrid中加入TComboBox TCheckBox

 
.cpp code:

//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "TSettingfrm.h"
#include "math.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSettingfrm *Settingfrm;
//---------------------------------------------------------------------------
__fastcall TSettingfrm::TSettingfrm(TComponent* Owner)
: TForm(Owner)
{
//StringGridOldWndProc = StringGrid1->WindowProc;
//StringGrid1->WindowProc = StringGridWndProc;
DoubleBuffered = true;
StringGrid1->DoubleBuffered = true;
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::FormCreate(TObject *Sender)
{
//------------------------------------
StringGrid1->Cells[0][0] = "機能";
StringGrid1->Cells[1][0] = "設定";
StringGrid1->Cells[2][0] = "整形前の例";
StringGrid1->Cells[3][0] = "整形後の例";

//------------------------------------
StringGrid1->Cells[0][1] = "インデント //n スペース";

//------------------------------------
StringGrid1->Cells[0][2] = "インデント //n タブ";

//------------------------------------
StringGrid1->Cells[0][3] = "インデント //n クラス";

//------------------------------------
StringGrid1->Cells[0][4] = "インデント //n switch文";
StringGrid1->Cells[2][4] = "switch (foo)/n/
{/n/
case 1:/n/
/   a += 1;/n/
/   break;/n/
/n/
case 2:/n/
{/n/
/   a += 2;/n/
/   break;/n/
}/n/
}/n/
";
StringGrid1->Cells[3][4] = "switch (foo)/n/
{/n/
/   case 1:/n/
/       a += 1;/n/
/       break;/n/
                                /n/
/   case 2:/n/
/   {/n/
/       a += 2;/n/
/       break;/n/
/   }/n/
}/n/
";

//------------------------------------
StringGrid1->Cells[0][5] = "インデント //n case文";
StringGrid1->Cells[2][5] = "switch (foo)/n/
{/n/
/   case 1:/n/
/       a += 1;/n/
/       break;/n/
/n/
/   case 2:/n/
/   {/n/
/       a += 2;/n/
/       break;/n/
/   }/n/
}/n/
/n/
";
StringGrid1->Cells[3][5] = "switch (foo)/n/
{/n/
/   case 1:/n/
/       a += 1;/n/
/       break;/n/
/n/
/   case 2:/n/
/       {/n/
/           a += 2;/n/
/           break;/n/
/       }/n/
}/n/
                                /n/
";

//------------------------------------
StringGrid1->Cells[0][6] = "インデント //n 括弧";
StringGrid1->Cells[2][6] = "if (isFoo)/n/
{/n/
/   bar();/n/
}/n/
else/n/
/   anotherBar();/n/
                                /n/
";
StringGrid1->Cells[3][6] = "if (isFoo)/n/
/   {/n/
/   bar();/n/
/ }/n/
else/n/
/   anotherBar();/n/
                                /n/
";
//------------------------------------
//------------------------------------
//------------------------------------
TGridRect myRect;
myRect.Left = -1;
myRect.Top = -1;
myRect.Right = -1;
myRect.Bottom = -1;
StringGrid1->Selection = myRect;
AddControls();
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::StringGrid1DrawCell(TObject *Sender, int ACol,
  int ARow, TRect &Rect, TGridDrawState State)
{
TRect tmprect = Rect;
AnsiString str = StringGrid1->Cells[ACol][ARow];
int height = DrawText(StringGrid1->Canvas->Handle,PAnsiChar(str.c_str()),str.Length(),&tmprect,DT_CALCRECT|DT_WORDBREAK);
if (StringGrid1->RowHeights[ARow] < height+5){
   //Rect.Bottom = Rect.Top + height+5;
StringGrid1->RowHeights[ARow]= height+5;
}else{
StringGrid1->Canvas->FillRect(Rect);
//InflateRect(&Rect,-2,-2); //少しだけ描画範囲を狭める
Rect.Left = Rect.Left+2;
Rect.Top = Rect.Top+2;
DrawText(StringGrid1->Canvas->Handle,PAnsiChar(str.c_str()),str.Length(),&Rect,DT_WORDBREAK);
//DrawTextEx(StringGrid1->Canvas->Handle,PChar(str.c_str()), str.Length(), &Rect, DT_EDITCONTROL, NULL);
    //SetControlsAlignment();
}
}
//---------------------------------------------------------------------------
void  TSettingfrm::AddControls()
{
TCheckBox * tmpcheckbox = NULL;
TComboBox * tmpcombobox = NULL;
TPanel * tmppanel = NULL;
int i =1;
for(; i< 3;i++){
tmppanel = new TPanel(StringGrid1);
tmppanel->Parent = this;
tmppanel->Width = 0;
tmppanel->Visible = false;
tmppanel->Color = clWhite;
tmppanel->Tag = i;

tmpcombobox = new TComboBox(StringGrid1);
tmpcombobox->Parent = tmppanel;
tmpcombobox->Align = alClient;
for(int j=0 ; j<4 ; j++){
tmpcombobox->Items->Add(AnsiString(pow(2,j)));
}
tmpcombobox->Color = clWhite;
//tmpcombobox->Tag = i;
tmpcombobox->ItemIndex = 2;
//NewCheckBox->OnClick = CheckBox1.OnClick; // Assign a previus OnClick event in an existing TCheckBox
StringGrid1->Objects[1][i] = tmppanel;
}
for( ;i <= 6;i++){
tmpcheckbox = new TCheckBox(StringGrid1);
tmpcheckbox->Parent = this;
tmpcheckbox->Width = 0;
tmpcheckbox->Visible = false;
tmpcheckbox->Color = clWhite;
tmpcheckbox->Tag = i;
tmpcheckbox->Caption = "インデントを挿入";
//NewCheckBox->OnClick = CheckBox1.OnClick; // Assign a previus OnClick event in an existing TCheckBox
StringGrid1->Objects[1][i] = tmpcheckbox;
}
}
//---------------------------------------------------------------------------
void  TSettingfrm::SetControlsAlignment()
{

TCheckBox * tmpcheckbox = NULL;
TComboBox * tmpcombobox = NULL;
TPanel * tmppanel = NULL;
TRect Rect;
int i= 1;
for(; i < 3;i++){
        /*
NewCheckBox = (TCheckBox*)(StringGrid1->Objects[4][i]);
if( NewCheckBox != NULL ){
Rect = StringGrid1->CellRect(4,i); // here, we get the cell rect for our contol...
NewCheckBox->Left = StringGrid1->Left + Rect.Left+2;
NewCheckBox->Top = StringGrid1->Top + Rect.Top+2;
NewCheckBox->Width = Rect.Right - Rect.Left;
NewCheckBox->Height = Rect.Bottom - Rect.Top;
NewCheckBox->Visible = true;
}
panel = (TPanel*)(StringGrid1->Objects[4][i]);
if( panel != NULL ){
Rect = StringGrid1->CellRect(4,i); // here, we get the cell rect for our contol...
panel->Left = StringGrid1->Left + Rect.Left+2;
panel->Top = StringGrid1->Top + Rect.Top+2;
panel->Width = Rect.Right - Rect.Left;
panel->Height = Rect.Bottom - Rect.Top;
panel->Visible = true;
//panel->Caption = "fadsfasd";
}
tmpcombobox = (TComboBox*)(StringGrid1->Objects[1][i]);
if( tmpcombobox != NULL ){
Rect = StringGrid1->CellRect(1,i); // here, we get the cell rect for our contol...
tmpcombobox->Left = StringGrid1->Left + Rect.Left+2;
tmpcombobox->Top = StringGrid1->Top + Rect.Top+2;
tmpcombobox->Width = Rect.Right - Rect.Left;
tmpcombobox->Height = Rect.Bottom - Rect.Top;
tmpcombobox->Visible = true;
//panel->Caption = "fadsfasd";
}
*/
tmppanel = (TPanel*)(StringGrid1->Objects[1][i]);
if( tmppanel != NULL ){
Rect = StringGrid1->CellRect(1,i); // here, we get the cell rect for our contol...
tmppanel->Left = StringGrid1->Left + Rect.Left+2;
tmppanel->Top = StringGrid1->Top + Rect.Top+2;
tmppanel->Width = Rect.Right - Rect.Left;
tmppanel->Height = Rect.Bottom - Rect.Top;
tmppanel->Visible = true;
}
}
for( ; i <= 6;i++){
tmpcheckbox = (TCheckBox*)(StringGrid1->Objects[1][i]);
if( tmpcheckbox != NULL ){
Rect = StringGrid1->CellRect(1,i); // here, we get the cell rect for our contol...
tmpcheckbox->Left = StringGrid1->Left + Rect.Left+2;
tmpcheckbox->Top = StringGrid1->Top + Rect.Top+2;
tmpcheckbox->Width = Rect.Right - Rect.Left;
tmpcheckbox->Height = Rect.Bottom - Rect.Top;
tmpcheckbox->Visible = true;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::btnOkClick(TObject *Sender)
{
StringGrid1->Visible = !StringGrid1->Visible;
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::FormResize(TObject *Sender)
{
StringGrid1->Rows[0]->BeginUpdate();
StringGrid1->Repaint();
   StringGrid1->Rows[0]->EndUpdate();
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::StringGrid1Enter(TObject *Sender)
{

TGridRect myRect;
myRect.Left = -1;
myRect.Top = -1;
myRect.Right = -1;
myRect.Bottom = -1;
StringGrid1->Selection = myRect;

}
//---------------------------------------------------------------------------

void __fastcall TSettingfrm::StringGrid1Exit(TObject *Sender)
{
TGridRect myRect;
myRect.Left = -1;
myRect.Top = -1;
myRect.Right = -1;
myRect.Bottom = -1;
StringGrid1->Selection = myRect;
}
//---------------------------------------------------------------------------

void __fastcall TSettingfrm::StringGrid1TopLeftChanged(TObject *Sender)
{
//StringGrid1->LeftCol = StringGrid1->FixedCols;
//StringGrid1->OnDrawCell = NULL;
StringGrid1->Rows[0]->BeginUpdate();
AllUnvisible();
SetControlsAlignment();
StringGrid1->Rows[0]->EndUpdate();
//StringGrid1->OnDrawCell = StringGrid1DrawCell;
}
//---------------------------------------------------------------------------
void __fastcall TSettingfrm::StringGridWndProc(TMessage &Message)
{
switch(Message.Msg){
case WM_VSCROLL:
case WM_HSCROLL:
case WM_MOUSEWHEEL:
case WM_CAPTURECHANGED:
case WM_PAINT:
//Perform(WM_SETREDRAW , 0 , 0 );
StringGrid1->Rows[0]->BeginUpdate();
AllUnvisible();
StringGridOldWndProc( Message );
SetControlsAlignment();
StringGrid1->Rows[0]->EndUpdate();
//Perform(WM_SETREDRAW , 1 , 0 );
//Refresh();
return;
}
StringGridOldWndProc( Message );
}
//---------------------------------------------------------------------------
void TSettingfrm::AllUnvisible(void)
{
int i= 1;
for( ; i <= 6;i++){
   ( (TControl*)(StringGrid1->Objects[1][i]) )->Visible = false;
}
}

.h code:

//---------------------------------------------------------------------------

#ifndef TSettingfrmH
#define TSettingfrmH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TSettingfrm : public TForm
{
__published: // IDE-managed Components
TButton *btnOk;
TButton *btnreset;
TButton *btnCancel;
TButton *btnhelp;
TLabel *Label1;
TLabel *Label2;
TLabel *lblDoc;
TLabel *Label4;
TButton *btnChk;
TStringGrid *StringGrid1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow,
           TRect &Rect, TGridDrawState State);
void __fastcall btnOkClick(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
void __fastcall StringGrid1Enter(TObject *Sender);
void __fastcall StringGrid1Exit(TObject *Sender);
void __fastcall StringGrid1TopLeftChanged(TObject *Sender);
private: // User declarations
TWndMethod StringGridOldWndProc;
void AddControls(void);
void SetControlsAlignment(void);
void __fastcall StringGridWndProc(TMessage &Message);
void AllUnvisible(void);
public: // User declarations
__fastcall TSettingfrm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TSettingfrm *Settingfrm;
//---------------------------------------------------------------------------
#endif

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值