C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[50190] Re:그리드 셀 선택시 색변경방법...좀 알려주셈 고수님들
답변자 [] 3232 읽음    2007-08-07 13:12
간단한 소스 입니다.  한번 보시면 사용 방법이 이해되실 겁니다.

[1번, 구조체 아이템 할당]
==================================================================================
void __fastcall TForm1::InitGrid()
{
    TStringGrid *p = dynamic_cast<TStringGrid*>(FindComponent(aItem));

     if( p!=NULL )
     {
        p->ColWidths[0] = 60;
        p->Cells[0][0] = " 1층";
        p->Cells[0][1] = " 2층";
        p->Cells[0][2] = " 3층";
        p->Cells[0][3] = " 4층";
        p->Cells[0][4] = " 5층";

        // sItemObject 구조체 생성 및 각 그리드에 할당
        for( int i=0; i<p->RowCount; i++ )
        {
            for( int j=0; j<p->ColCount; j++ )
            {
                sItemObject * pItem = new sItemObject;
                pItem->bIsReg       = false;
                pItem->bIsSelect    = false;
                pItem->bIsAlive     = false;
                pItem->szName       = "N";
                if( j!=0 )  p->Cells[j][i] = pItem->szName;
                p->Objects[j][i] = (TObject*)pItem;                        // <---- 할당 코드, Cells 이 아닌 Objects 이용
            }
        }
    }
}

[2번 그리드에 할당된 아이템 속성에 따라 색상 변경]
==================================================================================
void __fastcall TForm1::aaaDrawCell(TObject *Sender, int ACol, int ARow,
            TRect &Rect, TGridDrawState State)
{
     TStringGrid* pGrid = dynamic_cast<TStringGrid*>(Sender);
    TCanvas * sc = pGrid->Canvas;

    unsigned oldalign = SetTextAlign( pGrid->Canvas->Handle, TA_CENTER );
    sItemObject * p = (sItemObject*)pGrid->Objects[ACol][ARow];

    if( p!=NULL )
    {
            /** @brief 단말기가 등록 됐다면 */
            if( p->bIsReg )
            {
                /** @brief 단말기가 존재하나 살아 있다면 */
                if( p->bIsAlive )
                {
                    /** @brief 단말기가 선택 됐다면 */
                    if( p->bIsSelect )
                    {
                        sc->Brush->Color = clRed;
                        sc->Pen->Color = clBlack;
                        sc->FillRect( Rect );
                        sc->Font->Color = clWhite;

                    }
                    /** @brief 단말기가 선택 해제 됐다면 */
                    else
                    {
                        sc->Brush->Color = clWhite;
                        sc->Pen->Color = clBlack;
                        sc->FillRect( Rect );
                        sc->Font->Color = clBlack;
                    }
              }
         }
    }
   
    /** @brief 0번 Column 색상 및 폰트 색상 정의 */
    if( ACol==0 )
    {
        sc->Brush->Color = clHighlight;
        sc->FillRect( Rect );
        sc->Font->Color = clWhite;
    }

    sc->TextRect( Rect, (Rect.Right+Rect.Left)/2, Rect.Top+5, pGrid->Cells[ACol][ARow] );
    SetTextAlign( pGrid->Canvas->Handle, oldalign );
}

[3번 각 그리드 마우스 클릭 시 버튼 속성 반전 처리]
==================================================================================
void __fastcall TForm1::aaaMouseDown(TObject *Sender, TMouseButton Button,
            TShiftState Shift, int X, int Y)
{
    int col, row;
    int iEvent;

    TStringGrid * pGrid = dynamic_cast<TStringGrid*>(Sender);
    pGrid->MouseToCell( X, Y, col, row );
    pGrid->SetFocus();

    if ( Button == mbLeft )
    {
        if ( col > 0 )
        {
             //<---  구조체 정의 sItemObject => Grid 생성 sItemObject 생성, 각 그리드에 할당
            // <--- 할당 객체 주소 접근, 버튼 이벤트 갱신 처리
            sItemObject * pItem = reinterpret_cast<sItemObject*>(pGrid->Objects[col][row]);
            if( pItem!=NULL )
            {
                  if( !pItem->bIsAlive ) return ;

                  pItem->bIsSelect = !pItem->bIsSelect; // <---- 버튼 한번 클릭 시 Active, 다시 한번 클릭 시 DeActive
                  pGrid->Refresh();                               // <---- Grid 색상 변환 바로 업데이트, 갱신(DrawCell 함수 호출)
            }
        }
     }



궁금자 님이 쓰신 글 :
: 그리드로 버튼을 208개 생성 했습니다.
:
: 마우스로 셀을 클릭하면 힌색에서 빨강으로 바뀌고....다시 클릭하면 빨강에서 힌색으로 바뀌게....
:
: 그리고....제가 클릭한 건....모두 빨강으로 바뀌게 할수 있는 방법 업나요....
:
: 셀이 하나 밖에 빨강으로 안되네요.....하나 클릭하고....다른거 클릭하면 다른곳으로 넘어가 버림.
:
: //---------------------------------------------------------------------------
: MarkBlock[13][16];
: int xxx;
: int preCol, preRow;
: void __fastcall TForm1::aaaMouseDown(TObject *Sender, TMouseButton Button,
:             TShiftState Shift, int X, int Y)
: {
: String str;
:     long iCol, iRow;//,preCol,preRow;
:     aaa->MouseToCell(X,Y,iCol,iRow);
:     if(Shift.Contains(ssLeft)) {
:
:         Memo1->Lines->Add(""+AnsiString((int)iRow)+"," + AnsiString((int)iCol));
: //        str = ""+AnsiString((int)iRow)+"," + AnsiString((int)iCol);
:
:         preCol = iCol;
:         preRow = iRow;
:     }
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::aaaDrawCell(TObject *Sender, int ACol, int ARow,
:             TRect &Rect, TGridDrawState State)
: {
:
:     if ( ACol == preCol && ARow == preRow )
:     {
:         Memo1->Lines->Add("ccc");
:         aaa->Canvas->Brush->Color = clRed;
:         aaa->Canvas->Pen->Color = clBlack;
:         aaa->Canvas->Font->Color = clBlack;
:     }
:     if(State.Contains(gdFocused)) {
:         aaa->Canvas->DrawFocusRect(Rect);
:     }
:
: //--가운데 정렬 텍스트
:     unsigned oldalign = SetTextAlign(aaa->Canvas->Handle, TA_CENTER);
:         aaa->Canvas->TextRect(Rect, (Rect.Right+Rect.Left)/2, Rect.Top+2,
:                 aaa->Cells[ACol][ARow]);
:         SetTextAlign(aaa->Canvas->Handle, oldalign);
: //-----
:
:
: }
: //---------------------------------------------------------------------------

+ -

관련 글 리스트
50180 그리드 셀 선택시 색변경방법...좀 알려주셈 고수님들 궁금자 1349 2007/08/06
50190     Re:그리드 셀 선택시 색변경방법...좀 알려주셈 고수님들 답변자 3232 2007/08/07
50194         Re:Re:당신은 천재입니다. 궁금자 2193 2007/08/07
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.