šŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13016

Re: Controls into a Grid

#13016
From: JD
Date: Wed, 05 Nov 2003 12:09
105 lines
4858 bytes
"McKool" <kenethw@peak-system.com> wrote:
> iļæ½m trying to find the way to put a combobox or checkbox
> into a cell in a grid.

Here's a sample of using a CheckBox in a grid. The cells'
Object property is used to hold a (bool) CanCheck and
IsCheckBox value.

How it works:

Constructor initializes the CheckBox States.
OnDrawCell paints the CheckBox.
OnMouseDown and OnMouseUp detects a change in the check state.


TPoint ClickedCell;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
    bool IsCheckBox, Checked = false;
    TStringGrid* pGrid = StringGrid1;
    for( int Col = pGrid->FixedCols; Col < pGrid->ColCount; ++Col )
      {
          for( int Row = pGrid->FixedRows; Row < pGrid->RowCount; ++Row )
            {
              if( this [ Col ][ Row ] is a CheckBox ) IsCheckBox = true;
              else IsCheckBox = false;
              pGrid->Objects[ Col ][ Row ] = reinterpret_cast<TObject*>( MAKELONG( Checked, IsCheckBox ) );
            }
      }

    ClickedCell = Point(-1, -1);
}
//-------------------------------------------------------------
void __fastcall TForm1::GridDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
    TStringGrid *pGrid = static_cast<TStringGrid*>( Sender );
    TCanvas *pCanvas   = pGrid->Canvas;

    if( State.Contains( gdFixed ) )
      {
          pCanvas->Brush->Color = pGrid->FixedColor;
          pCanvas->Font->Color  = pGrid->Font->Color;
      }
    else if( State.Contains( gdFocused ) )
      {
          pCanvas->Brush->Color = clHighlight;
          pCanvas->Font->Color  = clHighlightText;
      }
    else
      {
          pCanvas->Brush->Color = pGrid->Color;
          pCanvas->Font->Color  = pGrid->Font->Color;
      }

    pCanvas->FillRect( Rect );
    if( State.Contains( gdFocused ) ) DrawFocusRect( pCanvas->Handle, &Rect );

    // if a check box
    if( HIWORD( reinterpret_cast<int>( pGrid->Objects[ ACol ][ ARow ] ) ) )
      {
          // get a Rect for the CheckBox
          TRect R = Rect;
          OffsetRect( R, 2, 1 );
          R.right  = R.left + GetSystemMetrics( SM_CXMENUCHECK );
          R.bottom = R.top  + GetSystemMetrics( SM_CYMENUCHECK );
          unsigned int BoxState = DFCS_BUTTONCHECK;
          // set the check state
          if( LOWORD( reinterpret_cast<int>( pGrid->Objects[ ACol ][ ARow ] ) ) ) BoxState |= DFCS_CHECKED;
          DrawFrameControl( pCanvas->Handle, &R, DFC_BUTTON, BoxState );
          // offset to draw the text
          Rect.left += R.right - R.left + 3;
      }
    DrawText( pCanvas->Handle, pGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, DT_LEFT | DT_SINGLELINE | DT_VCENTER );
}
//-------------------------------------------------------------
void __fastcall TForm1::GridMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TStringGrid* pGrid = static_cast<TStringGrid*>( Sender );
    int Col, Row;
    pGrid->MouseToCell( X, Y, Col, Row );
    ClickedCell = Point( Col, Row );
}
//-------------------------------------------------------------
void __fastcall TForm1::GridMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TStringGrid* pGrid = static_cast<TStringGrid*>( Sender );
    int Col, Row;
    pGrid->MouseToCell( X, Y, Col, Row );
    if( Col == ClickedCell.x && Row == ClickedCell.y )
      {
          bool IsCheckBox = HIWORD( reinterpret_cast<int>( pGrid->Objects[ Col ][ Row ] ) );
          if( IsCheckBox )
            {
                bool Checked = !LOWORD( reinterpret_cast<int>( pGrid->Objects[ Col ][ Row ] ) );
                pGrid->Objects[ Col ][ Row ] = reinterpret_cast<TObject*>( MAKELONG( Checked, IsCheckBox ) );
                RECT R = pGrid->CellRect( Col, Row );
                InvalidateRect( pGrid->Handle, &R, false );
            }
      }
}
//-------------------------------------------------------------

~ JD
Technical Details
Message-ID: <3fa8f689$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f9f6f26@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10cc67,d89c278f84ba5ec5
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-11-05 05:11:15 PST
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: Controls into a Grid
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.147.98.108     
References:  <3f9f6f26@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <3fa8f689$1@newsgroups.borland.com>
Date: 5 Nov 2003 05:09:29 -0700
X-Trace: newsgroups.borland.com 1068037769 207.105.83.66 (5 Nov 2003 05:09:29 -0700)
Lines: 106
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail