試看看!!應該OK~- //---------------------------------------------------------------------------
- #ifndef Unit1H
- #define Unit1H
- //---------------------------------------------------------------------------
- #include <Classes.hpp>
- #include <Controls.hpp>
- #include <StdCtrls.hpp>
- #include <Forms.hpp>
- #include <Grids.hpp>
- //---------------------------------------------------------------------------
- class TForm1 : public TForm
- {
- __published: // IDE-managed Components
- TStringGrid *StringGrid1;
- void __fastcall StringGrid1MouseDown(TObject *Sender,
- TMouseButton Button, TShiftState Shift, int X, int Y);
- void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol,
- int ARow, TRect &Rect, TGridDrawState State);
- private: // User declarations
- public: // User declarations
- __fastcall TForm1(TComponent* Owner);
- void __fastcall SetSGPos(int Col,int Row);
- void __fastcall GetSGPos(int *Col,int *Row);
- int m_ACol ;
- int m_ARow ;
- };
- //---------------------------------------------------------------------------
- extern PACKAGE TForm1 *Form1;
- //---------------------------------------------------------------------------
- #endif
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- for(int x=1;x<StringGrid1->ColCount;x++){
- for(int y=1;y<StringGrid1->RowCount;y++){
- StringGrid1->Cells[x][y] = "TEST" ;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::SetSGPos(int Col,int Row)
- {
- m_ACol = Col ;
- m_ARow = Row ;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::GetSGPos(int *Col,int *Row)
- {
- *Col = m_ACol ;
- *Row = m_ARow ;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender,
- TMouseButton Button, TShiftState Shift, int X, int Y)
- {
- if( Button == mbLeft ){
- SetSGPos(StringGrid1->Col,StringGrid1->Row);
- StringGrid1->Repaint() ;
- }
- }
- //--------------------------------------------------------------------------
- void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
- int ARow, TRect &Rect, TGridDrawState State)
- {
- int Col = 0 , Row = 0 ;
- GetSGPos( &Col , &Row ) ;
- if (State.Contains(gdFixed)) return;
- if (ARow==Row && ACol == Col){
- StringGrid1->Canvas->Font->Color=clRed ;
- }
- else{
- StringGrid1->Canvas->Font->Color=clBlack ;
- }
- StringGrid1->Canvas->TextRect (Rect, Rect.Left, Rect.Top,StringGrid1->Cells[ACol][ARow]);
- }
- //---------------------------------------------------------------------------
複製代碼 ... |