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
[45758] Re:급질문이요~ㅠTStringGrid에 대해서요..ㅠ
고황일 [hwangil] 1498 읽음    2006-07-20 20:38
왕왕왕초보 님이 쓰신 글 :
: 제가 하려고 하는 작업은요,
: DB에서 읽어온 데이터를 순차적으로 TStringGrid에 출력시키고,
: 타이머를 이용해서 15초 간격으로 DB의 자료와 TStringGrid의 자료를 비교하여
: 바뀐 데이터가 있으면 그 데이터를 TStringGrid에서 수정하여 출력한뒤
: 그 Row의 색을 다른색으로 변경시켜 주려고 하는데요,
:
: 여기서 질문은요,
: 1. TStringGrid에서 Row의 색을 변환시켜줄 방법이 있을까요?
:
: 2. 만약 저러한 방법이 없다면 그 줄에 있는 칼럼들마다 색을 변화시켜 줄 방법은 있을까요?
:
: 3. 제가
:     TCanvas *sc = StringGrid1->Canvas;
:     sc->Brush->Color = clLime;
:     sc->FillRect(Rect);
:     sc->TextRect(Rect, Rect.Left, Rect.Top, StringGrid1->Cells[ACol][ARow]);
:    이런식으로 코딩해서 테스트 결과 아주 잠깐 색이 변했다가 다시 흰색으로 돌아오던데
:     저 상태를 유지시켜줄 방법은 없을까요?
:
: 4. TStringGrid가 처음 실행시에 첫번째 셀이 셀렉트 된 상태로 뜨는데, 특정 셀을 셀렉트 시키고 안시키고를
:     제어할 소스는 없는건가요?
:
: 이상 네가지 입니다.
:
: 제발 도움을 주세요..ㅠ급합니다..ㅠㅠ

답변.. 입니다.

1. Row의 색 변화는 StringGrid의 OnDrawCell Event를 이용하여 가능합니다.
현재 Paint되는 Row가 원하는 Row이면 그때 속성을 바꾸면 됩니다.

2. 위와 같은 방법으로 가능합니다.

3.  위의 소스는 OnDrawCell Event에서 수행해야합니다.

4. StringGrid->Selection을 이용하면 됩니다. (Help 를 찾아 봤습니다.)
   일단 빈폼에 StringGrid를 하나 올려 놓구요.. 기본으로 5x5일겁니다.
   FormCreate 이벤트에 붙여 넣으십시오..

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

이렇게 하면 되는 군요

1,2,3에 대한 예제입니다.

void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
// 그래픽을 처리할 공간..
Graphics::TBitmap       *BM_Buff = new Graphics::TBitmap(); 
int     CenterX = 0;
int     CenterY = 0;
TRect   Area;
int     TextHeight;

//------- 이하는 모든 셀에 대한 처리
// 텍스트 정렬 위치 지정
unsigned oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_CENTER);

// 해당 셀에 대한 글자의 높이 추출
    TextHeight = StringGrid1->Canvas->TextHeight(StringGrid1->Cells[ACol][ARow]);
// 셀의 중앙 높이에 텍스트를 기입
    StringGrid1->Canvas->TextRect( Rect
                                 , (Rect.left + Rect.right)/2
                                 , Rect.top + (Rect.Height()/2 - TextHeight/2)
                                 , StringGrid1->Cells[ACol][ARow]);
// 텍스트 정렬  
    SetTextAlign(StringGrid1->Canvas->Handle, oldalign);
//----- 여기 까지가 모든 셀에 대한 일괄처리

// 여기부터 각각의 셀에 대한 처리

// 현재 셀의 위치를 찾아서....
        if ((ACol==0) && (ARow >= 1))
        {
// 위와 같이 정렬하고
           oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_RIGHT);
           StringGrid1->Canvas->TextRect( Rect
                                        , Rect.Right - 10
                                        , Rect.top + (Rect.Height()/2 - TextHeight/2)
                                        , StringGrid1->Cells[ACol][ARow]);
           SetTextAlign(StringGrid1->Canvas->Handle, oldalign);

// 이미지를 받아서..
           ImageList1->GetBitmap(MachineData[ARow-1].OnLineState, BM_Buff);
           if ((MachineData[ARow-1].OnLineState != 0) && (MachineData[ARow-1].Machine_No == 0))
              ImageList1->GetBitmap(2, BM_Buff);

           CenterX = Rect.Left;                                           
           CenterY = Rect.Top;
// 셀에 이미지를 붙여 넣고..
           StringGrid1->Canvas->Draw(CenterX, CenterY, BM_Buff);
        }

        if ((ACol==2) && (ARow >= 1))
        {
           oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_RIGHT);
           StringGrid1->Canvas->TextRect( Rect
                                        , Rect.Right - 10
                                        , Rect.top + (Rect.Height()/2 - TextHeight/2)
                                        , StringGrid1->Cells[ACol][ARow]);
           SetTextAlign(StringGrid1->Canvas->Handle, oldalign);

           ImageList1->GetBitmap(MachineData[ARow-1].Run_State+3, BM_Buff);     // 운전 상태 표시

             CenterX = Rect.Left;
             CenterY = Rect.Top;
             StringGrid1->Canvas->Draw(CenterX, CenterY, BM_Buff);
        }
        if ((ARow >= 1) && ((ACol == 3) || (ACol == 4)))                        //오른쪽 정렬
        {
           oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_RIGHT);
           StringGrid1->Canvas->TextRect( Rect
                                        , Rect.Right - 10
                                        , Rect.top + (Rect.Height()/2 - TextHeight/2)
                                        , StringGrid1->Cells[ACol][ARow]);
           SetTextAlign(StringGrid1->Canvas->Handle, oldalign);
        }
        if ((ACol==6) && (ARow >= 1))
        {
           oldalign = SetTextAlign(StringGrid1->Canvas->Handle, TA_RIGHT);
           StringGrid1->Canvas->TextRect( Rect
                                        , Rect.Right - 10
                                        , Rect.top + (Rect.Height()/2 - TextHeight/2)
                                        , StringGrid1->Cells[ACol][ARow]);
           SetTextAlign(StringGrid1->Canvas->Handle, oldalign);

            switch (MachineData[ARow-1].AlarmState)                            // 경보 처리부
            {
               case 0 : ImageList1->GetBitmap(7, BM_Buff); break;
               case 1 : ImageList1->GetBitmap(6, BM_Buff); break;
             }
             CenterX = Rect.Left;
             CenterY = Rect.Top;
             StringGrid1->Canvas->Draw(CenterX, CenterY, BM_Buff);
        }

        if ((ACol == 8) && (ARow >= 1))
        {
// 여기서는 진행 그래프를 만들어 넣는곳
        TRect   iInfo;
        TRect   dArea;
        double     iStep;
        TColor  oColor = StringGrid1->Canvas->Brush->Color;
        double  ProductTime = 0.0f;
        int     ProdC = 0;
        int     BarHeight = 12;
        AnsiString MSG;

                iInfo.left = (Rect.left + 60);
                iInfo.top = Rect.top + (Rect.Height() / 2) - (BarHeight / 2);
                iInfo.right = (Rect.right-20);
                iInfo.bottom = iInfo.top + BarHeight;

                dArea = iInfo;

                iStep = (double)iInfo.Width() / 96.0f;

// iInfo의 크기가 셀보다 작아요.. 셀의 크기와 같이 한다면.. 색을 바꿀수도 있겠죠?
// 셀에 박스도 그려요..
                StringGrid1->Canvas->Rectangle(iInfo);

                for (int i=0; i < 96; i++)
                {
                        dArea.left = iInfo.left + (int)((double)i * iStep);
                        dArea.right = iInfo.left + (int)((double)(i+1) * iStep);

                    switch (MachineData[ARow-1].TimeStamp[i])
                    {
// 상태에 따라서 색도 바꾸고요...
                        case '0':  // 확인 할수 없음.
                                break;
                        case '1': //  가동정지
                                StringGrid1->Canvas->Brush->Color = clGray;
                                StringGrid1->Canvas->FillRect(dArea);
                                break;
                        case '2': // 생산대기
                                StringGrid1->Canvas->Brush->Color = clRed;
                                StringGrid1->Canvas->FillRect(dArea);
                                break;
                        case '3': // 자동 생산
                                ProdC ++;
                                StringGrid1->Canvas->Brush->Color = clBlue;
                                StringGrid1->Canvas->FillRect(dArea);
                                break;
                    }
                }
                StringGrid1->Canvas->Brush->Color = oColor;

                ProductTime = ((double) ProdC / 96.0f) * 100;     // 백분율 계산

// 그중간에 텍스트도 넣었네요.. 얼마나 진행되었는지...
                MSG.sprintf("%5.1f%%", ProductTime);
                StringGrid1->Canvas->TextOutA(Rect.left+15, iInfo.top, MSG);

        }

        delete BM_Buff;

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


이상입니다.. 복잡하지만 분석하면 재미있을듯... ^^

+ -

관련 글 리스트
45745 급질문이요~ㅠTStringGrid에 대해서요..ㅠ 왕왕왕초보 850 2006/07/20
45758     Re:급질문이요~ㅠTStringGrid에 대해서요..ㅠ 고황일 1498 2006/07/20
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.