lin11112
初階會員
![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:42 回覆:83 積分:25 註冊:2003-02-17
發送簡訊給我
|
請問各位大大
如何在StringGrid的CELL中
取得由mouse DblClick cell時,所選取的部分字串
謝謝指導
|
daniel__lee
高階會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:18 回覆:124 積分:113 註冊:2002-11-10
發送簡訊給我
|
剛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
初階會員
![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:42 回覆:83 積分:25 註冊:2003-02-17
發送簡訊給我
|
感謝daniel__lee大大
但是此方式會將CELL內的字串全部取出
但小弟要的功能是只取CELL內的部分字串
EX : CELL內如為"P123 /P234"
再DblClick後"/P234"會變成藍色
而小弟便只要取出"/P234"
|
pwipwi
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:68 回覆:629 積分:349 註冊:2004-04-08
發送簡訊給我
|
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
初階會員
![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:42 回覆:83 積分:25 註冊:2003-02-17
發送簡訊給我
|
pwipwi大大
小弟使用大大所提供的程式碼
在DblClick後,但Editor都是NULL,而無法取得部分字串
請問程式中為何要加636
|
pwipwi
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:68 回覆:629 積分:349 註冊:2004-04-08
發送簡訊給我
|
引言:
pwipwi大大
小弟使用大大所提供的程式碼
在DblClick後,但Editor都是NULL,而無法取得部分字串
請問程式中為何要加636
Option中的goEditing有設為true嗎? 636是FInplaceEditor的offset。
|
lin11112
初階會員
![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:42 回覆:83 積分:25 註冊:2003-02-17
發送簡訊給我
|
pwipwi大大
Option中的goEditing已設為true
|
pwipwi
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:68 回覆:629 積分:349 註冊:2004-04-08
發送簡訊給我
|
這是最保險的程式,不過我跑出來的結果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;
}
}
}
//-----
這麼做後,應該可以跑出結果:
![](http://delphi.ktop.com.tw/loadfile.php?TOPICID=18503321&CC=413819) 最後再和你之前的程式比較,問題出在那
|
m8815010
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:99 回覆:372 積分:289 註冊:2003-11-13
發送簡訊給我
|
引言:
感謝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
初階會員
![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:42 回覆:83 積分:25 註冊:2003-02-17
發送簡訊給我
|
|