全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1753
推到 Plurk!
推到 Facebook!

Edit元件移動到stringgrid元件上面

答題得分者是:jow
small7011
一般會員


發表:19
回覆:34
積分:10
註冊:2007-07-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2011-03-11 15:31:15 IP:211.72.xxx.xxx 訂閱
我想要把Edit元件移到stringgrid元件上面,但我試了很久始終無法移到設定的cell位置,希望各位大大能幫忙解惑

[code cpp]
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TPoint OrgPoint,TempPoint,NewPoint;
if (ARow == 2 && ACol == 1)
{
OrgPoint.x = Rect.Left;
OrgPoint.y = Rect.Top;

//轉換座標.
TempPoint =StringGrid1->ClientToScreen(OrgPoint);
NewPoint =ScreenToClient(TempPoint);

//SetBounds(int ALeft, int ATop, int AWidth, int AHeight);
Edit1->SetBounds( NewPoint.x StringGrid1->GridLineWidth*2 ,
NewPoint.y StringGrid1->GridLineWidth*2 ,
Rect.Right - Rect.Left ,
Rect.Bottom - Rect.Top
);
}
[/code]

------
lee
GrandRURU
站務副站長


發表:240
回覆:1680
積分:1874
註冊:2005-06-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2011-03-26 09:46:08 IP:111.249.xxx.xxx 未訂閱
除了DrawCell事件外,還有SelectCell事件

這邊給個範例,雖然是Delphi的不過應該也可以輕鬆轉才是


[code delphi]
procedure TfrmMain.StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
var CanSelect: Boolean);
var R: TRect;
begin
if ((Col = 1) and (Row <> 0)) then
begin
{Size and position the combo box to fit the cell}
R := StringGrid1.CellRect(Col, Row);
R.Left := R.Left StringGrid1.Left;
R.Right := R.Right StringGrid1.Left;
R.Top := R.Top StringGrid1.Top;
R.Bottom := R.Bottom StringGrid1.Top;

{Show the combobox}
with cbInplaceComboBox do
begin
Left := R.Left 1;
Top := R.Top 1;
Width := (R.Right 1) - R.Left;
Height := (R.Bottom 1) - R.Top;

ItemIndex := Items.IndexOf(StringGrid1.Cells[Col, Row]);
Visible := True;
SetFocus;
end;
end;
CanSelect := True;
end;

procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
const
AlignFlags: array [TAlignment] of Integer =
(DT_LEFT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_RIGHT or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
DT_CENTER or DT_VCENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX);
var s: string;
begin
inherited;

with Rect do
begin
Left := Left 2;
Top := Top 2;
Right := Right - 5
end;

s := StringGrid1.Cells[Col, Row];

if (Row = 0) and (Col < StringGrid1.ColCount) then
begin
StringGrid1.Canvas.Font.Style := StringGrid1.Canvas.Font.Style [fsBold];
StringGrid1.Canvas.Brush.Color := StringGrid1.FixedColor;
StringGrid1.Canvas.FillRect(Rect);

DrawText(StringGrid1.Canvas.Handle,
PChar(s), Length(s),
Rect, AlignFlags[taCenter]);
end
else
if (Col = 0) and (Row > 0) and (Row < StringGrid1.RowCount) then
begin
StringGrid1.Canvas.FillRect(Rect);
DrawText(StringGrid1.Canvas.Handle,
PChar(s), Length(s),
Rect, AlignFlags[taRightJustify]);
end;
end;
[/code]



===================引 用 small7011 文 章===================
我想要把Edit元件移到stringgrid元件上面,但我試了很久始終無法移到設定的cell位置,希望各位大大能幫忙解惑

[code cpp]
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TPoint OrgPoint,TempPoint,NewPoint;
if (ARow == 2 && ACol == 1)
{
OrgPoint.x = Rect.Left;
OrgPoint.y = Rect.Top;

//轉換座標.
TempPoint =StringGrid1->ClientToScreen(OrgPoint);
NewPoint =ScreenToClient(TempPoint);

//SetBounds(int ALeft, int ATop, int AWidth, int AHeight);
Edit1->SetBounds( NewPoint.x StringGrid1->GridLineWidth*2 ,
NewPoint.y StringGrid1->GridLineWidth*2 ,
Rect.Right - Rect.Left ,
Rect.Bottom - Rect.Top
);
}
[/code]

jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2011-03-28 12:18:20 IP:112.104.xxx.xxx 未訂閱

[code delphi]
procedure TfrmMain.FormCreate(Sender: TObject);
begin
Edit1.Parent := StringGrid1;
Edit1.BoundsRect := StringGrid1.CellRect(1,1);
end;
[/code]
small7011
一般會員


發表:19
回覆:34
積分:10
註冊:2007-07-24

發送簡訊給我
#4 引用回覆 回覆 發表時間:2011-04-11 18:29:50 IP:211.20.xxx.xxx 訂閱
jow大你實在是太厲害了,完全不需要DrawCell事件就可以完成位置取代的動作,謝謝你指導.

[code cpp]
void __fastcall TForm2::FormCreate(TObject *Sender)
{
Edit1->Parent = StringGrid1;
Edit1->BoundsRect = StringGrid1->CellRect(1,1);
}
[/code]

===================引 用 jow 文 章===================

[code delphi]
procedure TfrmMain.FormCreate(Sender: TObject);
begin
Edit1.Parent := StringGrid1;
Edit1.BoundsRect := StringGrid1.CellRect(1,1);
end;
[/code]

------
lee
系統時間:2024-04-20 12:19:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!