?? tntdbgrids.pas
字號:
procedure TTntPopupListbox.CreateWnd;
begin
inherited CreateWnd;
Windows.SetParent(Handle, 0);
CallWindowProc(DefWndProc, Handle, wm_SetFocus, 0, 0);
end;
procedure TTntPopupListbox.WMChar(var Message: TWMChar);
var
Key: WideChar;
begin
Key := GetWideCharFromWMCharMsg(Message);
KeyPressW(Key);
SetWideCharForWMCharMsg(Message, Key);
inherited;
end;
procedure TTntPopupListbox.KeypressW(var Key: WideChar);
var
TickCount: Integer;
begin
case Key of
#8, #27: FSearchText := '';
#32..High(WideChar):
begin
TickCount := GetTickCount;
if TickCount - FSearchTickCount > 2000 then FSearchText := '';
FSearchTickCount := TickCount;
if Length(FSearchText) < 32 then FSearchText := FSearchText + Key;
if IsWindowUnicode(Handle) then
SendMessageW(Handle, LB_SelectString, WORD(-1), Longint(PWideChar(FSearchText)))
else
SendMessageA(Handle, LB_SelectString, WORD(-1), Longint(PAnsiChar(AnsiString(FSearchText))));
Key := #0;
end;
end;
end;
procedure TTntPopupListbox.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
(Owner as TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}).CloseUp((X >= 0) and (Y >= 0) and
(X < Width) and (Y < Height));
end;
{ TTntPopupDataList }
type
TTntPopupDataList = class(TPopupDataList)
protected
procedure Paint; override;
end;
procedure TTntPopupDataList.Paint;
var
FRecordIndex: Integer;
FRecordCount: Integer;
FKeySelected: Boolean;
FKeyField: TField;
procedure UpdateListVars;
begin
if ListActive then
begin
FRecordIndex := ListLink.ActiveRecord;
FRecordCount := ListLink.RecordCount;
FKeySelected := not VarIsNull(KeyValue) or
not ListLink.DataSet.BOF;
end else
begin
FRecordIndex := 0;
FRecordCount := 0;
FKeySelected := False;
end;
FKeyField := nil;
if ListLink.Active and (KeyField <> '') then
FKeyField := GetFieldProperty(ListLink.DataSet, Self, KeyField);
end;
function VarEquals(const V1, V2: Variant): Boolean;
begin
Result := False;
try
Result := V1 = V2;
except
end;
end;
var
I, J, W, X, TxtWidth, TxtHeight, LastFieldIndex: Integer;
S: WideString;
R: TRect;
Selected: Boolean;
Field: TField;
AAlignment: TAlignment;
begin
UpdateListVars;
Canvas.Font := Font;
TxtWidth := WideCanvasTextWidth(Canvas, '0');
TxtHeight := WideCanvasTextHeight(Canvas, '0');
LastFieldIndex := ListFields.Count - 1;
if ColorToRGB(Color) <> ColorToRGB(clBtnFace) then
Canvas.Pen.Color := clBtnFace else
Canvas.Pen.Color := clBtnShadow;
for I := 0 to RowCount - 1 do
begin
if Enabled then
Canvas.Font.Color := Font.Color else
Canvas.Font.Color := clGrayText;
Canvas.Brush.Color := Color;
Selected := not FKeySelected and (I = 0);
R.Top := I * TxtHeight;
R.Bottom := R.Top + TxtHeight;
if I < FRecordCount then
begin
ListLink.ActiveRecord := I;
if not VarIsNull(KeyValue) and
VarEquals(FKeyField.Value, KeyValue) then
begin
Canvas.Font.Color := clHighlightText;
Canvas.Brush.Color := clHighlight;
Selected := True;
end;
R.Right := 0;
for J := 0 to LastFieldIndex do
begin
Field := ListFields[J];
if J < LastFieldIndex then
W := Field.DisplayWidth * TxtWidth + 4 else
W := ClientWidth - R.Right;
S := GetWideDisplayText(Field);
X := 2;
AAlignment := Field.Alignment;
if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
case AAlignment of
taRightJustify: X := W - WideCanvasTextWidth(Canvas, S) - 3;
taCenter: X := (W - WideCanvasTextWidth(Canvas, S)) div 2;
end;
R.Left := R.Right;
R.Right := R.Right + W;
if SysLocale.MiddleEast then TControlCanvas(Canvas).UpdateTextFlags;
WideCanvasTextRect(Canvas, R, R.Left + X, R.Top, S);
if J < LastFieldIndex then
begin
Canvas.MoveTo(R.Right, R.Top);
Canvas.LineTo(R.Right, R.Bottom);
Inc(R.Right);
if R.Right >= ClientWidth then Break;
end;
end;
end;
R.Left := 0;
R.Right := ClientWidth;
if I >= FRecordCount then Canvas.FillRect(R);
if Selected then
Canvas.DrawFocusRect(R);
end;
if FRecordCount <> 0 then ListLink.ActiveRecord := FRecordIndex;
end;
//-----------------------------------------------------------------------------------------
// TDBGridInplaceEdit - Delphi 6 and higher
//-----------------------------------------------------------------------------------------
constructor TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.Create(Owner: TComponent);
begin
inherited Create(Owner);
FLookupSource := TDataSource.Create(Self);
end;
function TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.GetWidePickListBox: TTntCustomListBox;
var
PopupListbox: TTntPopupListbox;
begin
if not Assigned(FWidePickListBox) then
begin
PopupListbox := TTntPopupListbox.Create(Self);
PopupListbox.Visible := False;
PopupListbox.Parent := Self;
PopupListbox.OnMouseUp := ListMouseUp;
PopupListbox.IntegralHeight := True;
PopupListbox.ItemHeight := 11;
FWidePickListBox := PopupListBox;
end;
Result := FWidePickListBox;
end;
procedure TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.CloseUp(Accept: Boolean);
var
MasterField: TField;
ListValue: Variant;
begin
if ListVisible then
begin
if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
if ActiveList = DataList then
ListValue := DataList.KeyValue
else
if WidePickListBox.ItemIndex <> -1 then
ListValue := WidePickListBox.Items[WidePickListBox.ItemIndex];
SetWindowPos(ActiveList.Handle, 0, 0, 0, 0, 0, SWP_NOZORDER or
SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_HIDEWINDOW);
ListVisible := False;
if Assigned(FDataList) then
FDataList.ListSource := nil;
FLookupSource.Dataset := nil;
Invalidate;
if Accept then
if ActiveList = DataList then
with Grid as TTntCustomDBGrid, Columns[SelectedIndex].Field do
begin
MasterField := DataSet.FieldByName(KeyFields);
if MasterField.CanModify and DataLink.Edit then
MasterField.Value := ListValue;
end
else
if (not VarIsNull(ListValue)) and EditCanModify then
with Grid as TTntCustomDBGrid do
SetWideText(Columns[SelectedIndex].Field, ListValue)
end;
end;
procedure TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.DoEditButtonClick;
begin
(Grid as TTntCustomDBGrid).EditButtonClick;
end;
type TAccessTntCustomListbox = class(TTntCustomListbox);
procedure TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.DropDown;
var
Column: TTntColumn;
I, J, Y: Integer;
begin
if not ListVisible then
begin
with (Grid as TTntCustomDBGrid) do
Column := Columns[SelectedIndex] as TTntColumn;
if ActiveList = FDataList then
with Column.Field do
begin
FDataList.Color := Color;
FDataList.Font := Font;
FDataList.RowCount := Column.DropDownRows;
FLookupSource.DataSet := LookupDataSet;
FDataList.KeyField := LookupKeyFields;
FDataList.ListField := LookupResultField;
FDataList.ListSource := FLookupSource;
FDataList.KeyValue := DataSet.FieldByName(KeyFields).Value;
end
else if ActiveList = WidePickListBox then
begin
WidePickListBox.Items.Assign(Column.WidePickList);
DropDownRows := Column.DropDownRows;
// this is needed as inherited doesn't know about our WidePickListBox
if (DropDownRows > 0) and (WidePickListBox.Items.Count >= DropDownRows) then
WidePickListBox.Height := DropDownRows * TAccessTntCustomListbox(WidePickListBox as TTntCustomListbox).ItemHeight + 4
else
WidePickListBox.Height := WidePickListBox.Items.Count * TAccessTntCustomListbox(WidePickListBox as TTntCustomListbox).ItemHeight + 4;
if Text = '' then
WidePickListBox.ItemIndex := -1
else
WidePickListBox.ItemIndex := WidePickListBox.Items.IndexOf(Text);
J := WidePickListBox.ClientWidth;
for I := 0 to WidePickListBox.Items.Count - 1 do
begin
Y := WideCanvasTextWidth(WidePickListBox.Canvas, WidePickListBox.Items[I]);
if Y > J then J := Y;
end;
WidePickListBox.ClientWidth := J;
end;
end;
inherited DropDown;
end;
procedure TDBGridInplaceEdit{TNT-ALLOW TDBGridInplaceEdit}.UpdateContents;
var
Column: TTntColumn;
begin
inherited UpdateContents;
if EditStyle = esPickList then
ActiveList := WidePickListBox;
if FUseDataList then
begin
if FDataList = nil then
begin
FDataList := TTntPopupDataList.Create(Self);
FDataList.Visible := False;
FDataList.Parent := Self;
FDataList.OnMouseUp := ListMouseUp;
end;
ActiveList := FDataList;
end;
with (Grid as TTntCustomDBGrid) do
Column := Columns[SelectedIndex] as TTntColumn;
Self.ReadOnly := Column.ReadOnly;
Font.Assign(Column.Font);
ImeMode := Column.ImeMode;
ImeName := Column.ImeName;
end;
//-----------------------------------------------------------------------------------------
{ TTntDBGridInplaceEdit }
procedure TTntDBGridInplaceEdit.CreateWindowHandle(const Params: TCreateParams);
begin
TntCustomEdit_CreateWindowHandle(Self, Params);
end;
function TTntDBGridInplaceEdit.GetText: WideString;
begin
Result := TntControl_GetText(Self);
end;
procedure TTntDBGridInplaceEdit.SetText(const Value: WideString);
begin
TntControl_SetText(Self, Value);
end;
procedure TTntDBGridInplaceEdit.WMSetText(var Message: TWMSetText);
begin
if (not FBlockSetText) then
inherited;
end;
procedure TTntDBGridInplaceEdit.UpdateContents;
var
Grid: TTntCustomDBGrid;
begin
Grid := Self.Grid as TTntCustomDBGrid;
EditMask := Grid.GetEditMask(Grid.Col, Grid.Row);
Text := Grid.GetEditText(Grid.Col, Grid.Row);
MaxLength := Grid.GetEditLimit;
FBlockSetText := True;
try
inherited;
finally
FBlockSetText := False;
end;
end;
procedure TTntDBGridInplaceEdit.DblClick;
begin
FInDblClick := True;
try
inherited;
finally
FInDblClick := False;
end;
end;
{ TTntGridDataLink }
procedure TTntGridDataLink.GridUpdateFieldText(Sender: TField; const Text: AnsiString);
begin
Sender.OnSetText := OriginalSetText;
if Assigned(Sender) then
SetWideText(Sender, (Grid as TTntCustomDBGrid).FEditText);
end;
procedure TTntGridDataLink.RecordChanged(Field: TField);
var
CField: TField;
begin
inherited;
if Grid.HandleAllocated then begin
CField := Grid.SelectedField;
if ((Field = nil) or (CField = Field)) and
(Assigned(CField) and (GetWideText(CField) <> (Grid as TTntCustomDBGrid).FEditText)) then
begin
with (Grid as TTntCustomDBGrid) do begin
InvalidateEditor;
if InplaceEditor <> nil then InplaceEditor.Deselect;
end;
end;
end;
end;
procedure TTntGridDataLink.UpdateData;
var
Field: TField;
begin
Field := (Grid as TTntCustomDBGrid).SelectedField;
// remember "set text"
if Field <> nil then
OriginalSetText := Field.OnSetText;
try
// redirect "set text" to self
if Field <> nil then
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -