線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1508
推到 Plurk!
推到 Facebook!

有關StringGrid的CELL問題

答題得分者是:m8815010
lin11112
初階會員


發表:42
回覆:83
積分:25
註冊:2003-02-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-03 12:49:14 IP:61.30.xxx.xxx 未訂閱
請問各位大大 如何在StringGrid的CELL中 取得由mouse DblClick cell時,所選取的部分字串 謝謝指導
daniel__lee
高階會員


發表:18
回覆:124
積分:113
註冊:2002-11-10

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-03 13:34:00 IP:61.218.xxx.xxx 未訂閱
剛try了一下ok沒問題 應該是你想要的    //---------------------------------------------------------------------------    #include  #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { for(int i=0;i<5;i ) { for(int j=0;j<5;j ) StringGrid1->Cells[i][j]="(" IntToStr(i) "," IntToStr(j) ")"; } } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DblClick(TObject *Sender) { AnsiString st; int col,row; col=StringGrid1->Col; row=StringGrid1->Row; st=StringGrid1->Cells[col][row]; ShowMessage(st); } //--------------------------------------------------------------------------- ~ 勿在浮沙上面築高塔 ~
------
~ 勿在浮沙上面築高塔 ~
lin11112
初階會員


發表:42
回覆:83
積分:25
註冊:2003-02-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-03 14:21:14 IP:210.202.xxx.xxx 未訂閱
感謝daniel__lee大大 但是此方式會將CELL內的字串全部取出 但小弟要的功能是只取CELL內的部分字串 EX : CELL內如為"P123 /P234" 再DblClick後"/P234"會變成藍色 而小弟便只要取出"/P234"
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-03 15:11:48 IP:211.76.xxx.xxx 未訂閱
lin11112你好:     試試下面的Code,我只保證在BCB6的VCL下可以用,下一版可能就會失效了。另外..我這是破壞物件導向的行為....,能不要用就不要用。(不過誰叫他元件設計時,不開放TInplaceEdit) < class="code"> void __fastcall TForm1::StringGrid1DblClick(TObject *Sender) { TInplaceEdit* Editor = dynamic_cast(*(TInplaceEdit**)((unsigned int)StringGrid1 636)); if(Editor) Caption = Editor->SelText; }
lin11112
初階會員


發表:42
回覆:83
積分:25
註冊:2003-02-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-03 15:34:38 IP:210.202.xxx.xxx 未訂閱
pwipwi大大 小弟使用大大所提供的程式碼 在DblClick後,但Editor都是NULL,而無法取得部分字串 請問程式中為何要加636
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-03 22:42:56 IP:211.76.xxx.xxx 未訂閱
引言: pwipwi大大 小弟使用大大所提供的程式碼 在DblClick後,但Editor都是NULL,而無法取得部分字串 請問程式中為何要加636
Option中的goEditing有設為true嗎? 636是FInplaceEditor的offset。
lin11112
初階會員


發表:42
回覆:83
積分:25
註冊:2003-02-17

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-11-04 08:11:29 IP:211.20.xxx.xxx 未訂閱
pwipwi大大 Option中的goEditing已設為true
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-11-04 10:54:16 IP:211.76.xxx.xxx 未訂閱
這是最保險的程式,不過我跑出來的結果offset仍是636。如果你無法跑出來的話,可以用debuger檢查SringGrid1裡InplaceEditor有沒有配置出來。    請你先寫個測試程式看是否可行:只放一個StringGrid,Option只把goEditing設為true。在OnDblClick裡加上:  
 
// 以下是動態尋找offset的方法
void __fastcall TForm1::StringGrid1DblClick(TObject *Sender)
{
    for(unsigned int i = sizeof(TCustomControl); i < sizeof(TStringGrid);i+=4)
        {
        TInplaceEdit* Editor =
        dynamic_cast(*(TInplaceEdit**)((unsigned int)StringGrid1+i));
        if(IsBadReadPtr(Editor,sizeof(TInplaceEdit)))
            continue;
        if(Editor)
            {
            Caption = Editor->SelText;
            return;
            }
        }
}
//-----
這麼做後,應該可以跑出結果: 最後再和你之前的程式比較,問題出在那
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-11-04 12:17:04 IP:61.63.xxx.xxx 未訂閱
引言: 感謝daniel__lee大大 但是此方式會將CELL內的字串全部取出 但小弟要的功能是只取CELL內的部分字串 EX : CELL內如為"P123 /P234" 再DblClick後"/P234"會變成藍色 而小弟便只要取出"/P234"
lin11112你好: >: < class="code">Unit1.h ~~ class TForm1 : public TForm { __published: // IDE-managed Components TStringGrid *StringGrid1; void __fastcall FormCreate(TObject *Sender); private: // User declarations TWndMethod OldStringGridWndProc; //add this line void __fastcall StringGridWndProc(TMessage& Message); //add this line public: // User declarations __fastcall TForm1(TComponent* Owner); }; Unit1.cpp ~~ TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { OldStringGridWndProc = StringGrid1->WindowProc; StringGrid1->WindowProc = StringGridWndProc; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { StringGrid1->RowCount=10; //測試環境給定 StringGrid1->ColCount=10; int cnt=0; for (int i=0;i<10;i ) for (int j=0;j<10;j ) StringGrid1->Cells[j][i]=IntToStr(cnt ) "test"; StringGrid1->EditorMode=true; //開啟可編輯模式 } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGridWndProc(TMessage& Message) { OldStringGridWndProc(Message); if (Message.Msg==WM_CTLCOLOREDIT) { HWND hEditCtrl=(HWND)Message.LParam; //get the handle of the selected edit control char txt[128]; //suppose the length of text is about 128 ZeroMemory(txt,128); SendMessage(hEditCtrl,WM_GETTEXT,128,(LPARAM)txt); //get text value of the edit control int lResult; lResult=SendMessage(hEditCtrl,EM_GETSEL,NULL,NULL); //get the position you selected int spos=LOWORD(lResult); //get start position int epos=HIWORD(lResult); //get end position if (spos!=epos) //show text you selected on the "Form1->Caption" this->Caption=AnsiString(txt).SubString(spos 1,epos-spos); else this->Caption=""; } } 目前測還ok啦!
lin11112
初階會員


發表:42
回覆:83
積分:25
註冊:2003-02-17

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-11-08 08:22:17 IP:211.20.xxx.xxx 未訂閱
感謝m8815010大大 已試成功
系統時間:2024-04-28 5:49:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!