📄 toolctrlseh.pas
字号:
function TSpecRowEh.IsValueStored: Boolean;
begin
Result := not VarEquals(FValue,Null);
end;
function TSpecRowEh.IsFontStored: Boolean;
begin
Result := FFontAssigned;
end;
function TSpecRowEh.IsColorStored: Boolean;
begin
Result := FColorAssigned;
end;
function TSpecRowEh.LocateKey(KeyValue: Variant): Boolean;
begin
Result := Visible and VarEquals(Value,KeyValue);
end;
procedure TSpecRowEh.BeginUpdate;
begin
Inc(FUpdateCount);
end;
procedure TSpecRowEh.EndUpdate;
begin
Dec(FUpdateCount);
Changed;
end;
{ TLookupCtrlDataLinkEh }
procedure GetFieldsProperty(List: TList; DataSet: TDataSet;
Control: TComponent; const FieldNames: String);
var
Pos: Integer;
Field: TField;
FieldName: String;
begin
Pos := 1;
while Pos <= Length(FieldNames) do
begin
FieldName := ExtractFieldName(FieldNames, Pos);
Field := DataSet.FindField(FieldName);
if Field = nil then
DatabaseErrorFmt(SFieldNotFound, [FieldName], Control);
if Assigned(List) then List.Add(Field);
end;
end;
function GetFieldsProperty(DataSet: TDataSet; Control: TComponent;
const FieldNames: String):TFieldsArrEh;
var FieldList:TList;
i:Integer;
begin
FieldList := TList.Create;
GetFieldsProperty(FieldList,DataSet, Control, FieldNames);
SetLength(Result,FieldList.Count);
for i := 0 to FieldList.Count-1 do Result[i] := FieldList[i];
FieldList.Free;
end;
procedure DataSetSetFieldValues(DataSet: TDataSet; Fields: String; Value:Variant);
var FieldList: TList;
i:Integer;
begin
if VarEquals(Value,Null) then
begin
FieldList := TList.Create;
try
Dataset.GetFieldList(FieldList,Fields);
for i := 0 to FieldList.Count-1 do
TField(FieldList[i]).Clear;
finally
FieldList.Free;
end;
end else
DataSet.FieldValues[Fields] := Value;
end;
constructor TLookupCtrlDataLinkEh.Create;
begin
inherited Create;
// VisualControl := True;
end;
procedure TLookupCtrlDataLinkEh.ActiveChanged;
begin
if FDBLookupControl <> nil then FDBLookupControl.UpdateDataFields;
end;
procedure TLookupCtrlDataLinkEh.FocusControl(Field: TFieldRef);
begin
if (Field^ <> nil) and (Field^ = FDBLookupControl.Field) and
(FDBLookupControl <> nil) and FDBLookupControl.CanFocus then
begin
Field^ := nil;
FDBLookupControl.SetFocus;
end;
end;
procedure TLookupCtrlDataLinkEh.LayoutChanged;
begin
if FDBLookupControl <> nil then FDBLookupControl.UpdateDataFields;
end;
procedure TLookupCtrlDataLinkEh.RecordChanged(Field: TField);
begin
if FDBLookupControl <> nil then FDBLookupControl.DataLinkRecordChanged(Field);
end;
{ TLookupCtrlListLinkEh }
constructor TLookupCtrlListLinkEh.Create;
begin
inherited Create;
// VisualControl := True;
end;
procedure TLookupCtrlListLinkEh.ActiveChanged;
begin
if FDBLookupControl <> nil then FDBLookupControl.UpdateListFields;
end;
procedure TLookupCtrlListLinkEh.DataSetChanged;
begin
if FDBLookupControl <> nil then FDBLookupControl.ListLinkDataChanged;
end;
procedure TLookupCtrlListLinkEh.LayoutChanged;
begin
if FDBLookupControl <> nil then FDBLookupControl.UpdateListFields;
end;
{ TDBLookupControlEh }
constructor TDBLookupControlEh.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if NewStyleControls
then ControlStyle := [csOpaque]
else ControlStyle := [csOpaque, csFramed];
ParentColor := False;
TabStop := True;
FLookupSource := TDataSource.Create(Self);
FDataLink := TLookupCtrlDataLinkEh.Create;
FDataLink.FDBLookupControl := Self;
FListLink := TLookupCtrlListLinkEh.Create;
FListLink.FDBLookupControl := Self;
FListFields := TList.Create;
FKeyValue := Null;
FSpecRow := TSpecRowEh.Create(Self);
FSpecRow.OnChanged := SpecRowChanged;
end;
destructor TDBLookupControlEh.Destroy;
begin
FSpecRow.Free;
FListFields.Free;
FListFields := nil;
FListLink.FDBLookupControl := nil;
FListLink.Free;
FListLink := nil;
FDataLink.FDBLookupControl := nil;
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TDBLookupControlEh.CanModify: Boolean;
function MasterFieldsCanModify: Boolean;
var i:Integer;
begin
Result := True;
for i := 0 to Length(FMasterFields)-1 do
if not FMasterFields[i].CanModify then
begin
Result := False;
Exit;
end;
end;
begin
Result := FListActive and not ReadOnly and ((FDataLink.DataSource = nil) or
(Length(FMasterFields) <> 0) and MasterFieldsCanModify);
end;
procedure TDBLookupControlEh.CheckNotCircular;
begin
if FListLink.Active and FListLink.DataSet.IsLinkedTo(DataSource) then
DatabaseError(SCircularDataLink);
end;
procedure TDBLookupControlEh.CheckNotLookup;
begin
if FLookupMode then DatabaseError(SPropDefByLookup);
if FDataLink.DataSourceFixed then DatabaseError(SDataSourceFixed);
end;
procedure TDBLookupControlEh.UpdateDataFields;
function MasterFieldNames: String;
var i:Integer;
begin
Result := '';
for i := 0 to Length(FMasterFields)-1 do
if Result = '' then
Result := FMasterFields[i].FieldName else
Result := Result + ';' + FMasterFields[i].FieldName;
end;
begin
//FDataField := nil;
//FMasterField := nil;
FMasterFieldNames := '';
if FDataLink.Active and (FDataFieldName <> '') then
begin
CheckNotCircular;
FDataFields := GetFieldsProperty(FDataLink.DataSet, Self, FDataFieldName);
if (Length(FDataFields) = 1) and (FDataFields[0].FieldKind = fkLookup)
then FMasterFields := GetFieldsProperty(FDataLink.DataSet, Self, FDataFields[0].KeyFields)
else FMasterFields := FDataFields;
FMasterFieldNames := MasterFieldNames;
end;
SetLookupMode((Length(FDataFields) = 1) and (FDataFields[0].FieldKind = fkLookup));
DataLinkRecordChanged(nil);
end;
procedure TDBLookupControlEh.DataLinkRecordChanged(Field: TField);
function FieldFound(Value:TField) :Boolean;
var i:Integer;
begin
Result := False;
for i := 0 to Length(FMasterFields)-1 do
if FMasterFields[i] = Value then
begin
Result := True;
Exit;
end;
end;
begin
if (Field = nil) or FieldFound(Field) then
if Length(FMasterFields) > 0
then SetKeyValue(FDataLink.DataSet.FieldValues[FMasterFieldNames])
else SetKeyValue(Null);
end;
function TDBLookupControlEh.GetBorderSize: Integer;
var
Params: TCreateParams;
R: TRect;
begin
CreateParams(Params);
SetRect(R, 0, 0, 0, 0);
AdjustWindowRectEx(R, Params.Style, False, Params.ExStyle);
Result := R.Bottom - R.Top;
end;
function TDBLookupControlEh.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
function TDBLookupControlEh.GetKeyFieldName: string;
begin
if FLookupMode then Result := '' else Result := FKeyFieldName;
end;
function TDBLookupControlEh.GetListSource: TDataSource;
begin
if FLookupMode then Result := nil else Result := FListLink.DataSource;
end;
function TDBLookupControlEh.GetReadOnly: Boolean;
begin
Result := FDataLink.ReadOnly;
end;
function TDBLookupControlEh.GetTextHeight: Integer;
var
DC: HDC;
SaveFont: HFont;
Metrics: TTextMetric;
begin
DC := GetDC(0);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
Result := Metrics.tmHeight;
end;
function TDBLookupControlEh.GetSpecRowHeight: Integer;
var
DC: HDC;
SaveFont: HFont;
Metrics: TTextMetric;
begin
DC := GetDC(0);
SaveFont := SelectObject(DC, SpecRow.Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
Result := Metrics.tmHeight;
end;
procedure TDBLookupControlEh.KeyValueChanged;
begin
if not SpecRow.Visible then
SpecRow.Selected := False
else
begin
SpecRow.Selected := VarEquals(FKeyValue,SpecRow.Value);
if not FLockPosition and not SpecRow.Selected and SpecRow.ShowIfNotInKeyList then
if not LocateKey
then SpecRow.Selected := True
else ListLinkDataChanged
end;
end;
procedure TDBLookupControlEh.UpdateListFields;
var
DataSet: TDataSet;
ResultField: TField;
i: Integer;
begin
FListActive := False;
//FKeyField := nil;
FListField := nil;
FListFields.Clear;
if FListLink.Active and (FKeyFieldName <> '') then
begin
CheckNotCircular;
DataSet := FListLink.DataSet;
FKeyFields := GetFieldsProperty(DataSet, Self, FKeyFieldName);
try
DataSet.GetFieldList(FListFields, FListFieldName);
except
DatabaseErrorFmt(SFieldNotFound, [Self.Name, FListFieldName]);
end;
if FLookupMode then
begin
ResultField := GetFieldProperty(DataSet, Self, FDataFields[0].LookupResultField);
if FListFields.IndexOf(ResultField) < 0 then
FListFields.Insert(0, ResultField);
FListField := ResultField;
end else
begin
if FListFields.Count = 0 then
for i := 0 to Length(FKeyFields)-1 do FListFields.Add(FKeyFields[i]);
if (FListFieldIndex >= 0) and (FListFieldIndex < FListFields.Count) then
FListField := FListFields[FListFieldIndex] else
FListField := FListFields[0];
end;
FListActive := True;
end;
end;
procedure TDBLookupControlEh.ListLinkDataChanged;
begin
end;
function TDBLookupControlEh.LocateKey: Boolean;
var
KeySave: Variant;
begin
Result := False;
try
KeySave := FKeyValue;
if not VarIsNull(FKeyValue) and (FListLink.DataSet <> nil) and
FListLink.DataSet.Active and
FListLink.DataSet.Locate(FKeyFieldName, FKeyValue, []) then
begin
Result := True;
FKeyValue := KeySave;
end;
except
end;
end;
procedure TDBLookupControlEh.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
if (FDataLink <> nil) and (AComponent = DataSource)
then DataSource := nil;
if (FListLink <> nil) and (AComponent = ListSource)
then ListSource := nil;
end;
end;
procedure TDBLookupControlEh.ProcessSearchKey(Key: Char);
var
TickCount: Integer;
S: string;
CharMsg: TMsg;
begin
if (FListField <> nil) and (FListField.FieldKind in [fkData, fkInternalCalc]) and
(FListField.DataType in [ftString]) then
case Key of
#8, #27: SearchText := '';
#32..#255:
if CanModify then
begin
TickCount := GetTickCount;
if TickCount - SearchTickCount > 2000 then SearchText := '';
SearchTickCount := TickCount;
if SysLocale.FarEast and (Key in LeadBytes) then
if PeekMessage(CharMsg, Handle, WM_CHAR, WM_CHAR, PM_REMOVE) then
begin
if CharMsg.Message = WM_Quit then
begin
PostQuitMessage(CharMsg.wparam);
Exit;
end;
SearchText := SearchText + Key;
Key := Char(CharMsg.wParam);
end;
if Length(SearchText) < 32 then
begin
S := SearchText + Key;
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -