J
jack.shi
Unregistered / Unconfirmed
GUEST, unregistred user!
unit JackCombobox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, DB, DBCtrls;
type
TJackCombobox = class;
TMyComboBox = class(TDBGrid)
private
FEdit: TJackCombobox;
protected
procedure CreateParams(var params: TCreateParams); override;
procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
public
constructor Create(AOwner: TComponent); override;
end;
TJackCombobox = class(TEdit)
private
FDataLink: TFieldDataLink;
FMyComboBox: TMyComboBox;
FDowned: Boolean;
FFocused: Boolean;
FGridWidth: integer;
FDataSource: TDataSource;
FGridQuery: TDataSet;
FGridNumber: string;
FGridChinese: string;
FGridEnglish: string;
procedure ShowCombobox;
procedure HideComboBox;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
protected
procedure WMSIZE(var Msg: TWMSize); message WM_SIZE;
procedure CMCancelMode(var Msg: TCMCancelMode); message CM_CANCELMODE;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Change; override;
procedure DoEnter; override;
procedure DoExit; override;
published
property GridWidth: integer read FGridWidth write FGridWidth default 120;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property GridNumber: string read FGridNumber write FGridNumber;
property GridChinese: string read FGridChinese write FGridChinese;
property GridEnglish: string read FGridEnglish write FGridEnglish;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JackTools', [TJackCombobox]);
end;
{ TJackCombobox }
procedure TJackCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;
constructor TJackCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FGridWidth := Width;
FMyComboBox := TMyComboBox.Create(self);
FDowned := False;
FFocused := False;
end;
destructor TJackCombobox.Destroy;
begin
FMyComboBox.Free;
FMyComboBox := nil;
inherited Destroy;
end;
procedure TJackCombobox.HideComboBox;
begin
SetWindowPos(FMyComboBox.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE +
SWP_HideWindow + SWP_NOSIZE + SWP_NOZORDER);
FDowned := not FDowned;
end;
procedure TJackCombobox.ShowCombobox;
var
Point: TPoint;
begin
Point.x := Left;
Point.y := top + Height;
Point := Parent.ClienttoScreen(Point);
FMyCombobox.Width := GridWidth;
SetWindowPos(FMyComboBox.Handle, HWND_TOP, Point.x, Point.y, 0, 0,
SWP_showWindow + SWP_NOSIZE);
// SetWindowPos(FMyComboBox.Handle, HWND_TOP, Point.x, Point.y, 0, 0, SWP_NOMOVE
// + SWP_NOZORDER + SWP_NOACTIVATE + SWP_NOREDRAW);
FDowned := not FDowned;
end;
function TJackCombobox.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TJackCombobox.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then
Value.FreeNotification(Self);
FMyCombobox.DataSource := FDataLink.DataSource;
end;
procedure TJackCombobox.WMSIZE(var Msg: TWMSize);
begin
FMyCombobox.Width := GridWidth;
end;
procedure TJackCombobox.Change;
var
FindName: string;
begin
if not (csDesigning in ComponentState) and FFocused then
begin
fgridquery := FDataLink.DataSource.DataSet;
if Text <> '' then
begin
if Text[1] in ['0'..'9'] then
FindName := FGridNumber
else if (Text[1] in ['A'..'Z']) or (Text[1] in ['a'..'z']) then
FindName := FGridEnglish
else
FindName := FGridChinese;
end;
fGridQuery.Locate(FindName, Text, [loPartialKey]);
if not FDowned then
ShowComboBox;
end;
inherited;
end;
procedure TJackCombobox.DoEnter;
begin
FFocused := True;
inherited;
end;
procedure TJackCombobox.DoExit;
begin
FFocused := False;
if FDowned then
HideComboBox;
inherited;
end;
{ TMyComboBox }
constructor TMyComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit := TJackCombobox(AOwner);
ControlStyle := ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];
Width := FEdit.GridWidth;
Height := 130;
Options := [dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect,
dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit];
DataSource := FEdit.FDataSource;
ReadOnly := True;
ScrollBars := ssVertical;
Visible := False;
Parent := TWinControl(AOwner);
end;
procedure TMyComboBox.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style := style or ws_popup;
EXStyle := EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;
procedure TMyComboBox.WMSetFocus(var Msg: TWMSetFocus);
begin
inherited;
FEdit.SetFocus;
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, DB, DBCtrls;
type
TJackCombobox = class;
TMyComboBox = class(TDBGrid)
private
FEdit: TJackCombobox;
protected
procedure CreateParams(var params: TCreateParams); override;
procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
public
constructor Create(AOwner: TComponent); override;
end;
TJackCombobox = class(TEdit)
private
FDataLink: TFieldDataLink;
FMyComboBox: TMyComboBox;
FDowned: Boolean;
FFocused: Boolean;
FGridWidth: integer;
FDataSource: TDataSource;
FGridQuery: TDataSet;
FGridNumber: string;
FGridChinese: string;
FGridEnglish: string;
procedure ShowCombobox;
procedure HideComboBox;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
protected
procedure WMSIZE(var Msg: TWMSize); message WM_SIZE;
procedure CMCancelMode(var Msg: TCMCancelMode); message CM_CANCELMODE;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Change; override;
procedure DoEnter; override;
procedure DoExit; override;
published
property GridWidth: integer read FGridWidth write FGridWidth default 120;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property GridNumber: string read FGridNumber write FGridNumber;
property GridChinese: string read FGridChinese write FGridChinese;
property GridEnglish: string read FGridEnglish write FGridEnglish;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JackTools', [TJackCombobox]);
end;
{ TJackCombobox }
procedure TJackCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;
constructor TJackCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FGridWidth := Width;
FMyComboBox := TMyComboBox.Create(self);
FDowned := False;
FFocused := False;
end;
destructor TJackCombobox.Destroy;
begin
FMyComboBox.Free;
FMyComboBox := nil;
inherited Destroy;
end;
procedure TJackCombobox.HideComboBox;
begin
SetWindowPos(FMyComboBox.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE +
SWP_HideWindow + SWP_NOSIZE + SWP_NOZORDER);
FDowned := not FDowned;
end;
procedure TJackCombobox.ShowCombobox;
var
Point: TPoint;
begin
Point.x := Left;
Point.y := top + Height;
Point := Parent.ClienttoScreen(Point);
FMyCombobox.Width := GridWidth;
SetWindowPos(FMyComboBox.Handle, HWND_TOP, Point.x, Point.y, 0, 0,
SWP_showWindow + SWP_NOSIZE);
// SetWindowPos(FMyComboBox.Handle, HWND_TOP, Point.x, Point.y, 0, 0, SWP_NOMOVE
// + SWP_NOZORDER + SWP_NOACTIVATE + SWP_NOREDRAW);
FDowned := not FDowned;
end;
function TJackCombobox.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TJackCombobox.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then
Value.FreeNotification(Self);
FMyCombobox.DataSource := FDataLink.DataSource;
end;
procedure TJackCombobox.WMSIZE(var Msg: TWMSize);
begin
FMyCombobox.Width := GridWidth;
end;
procedure TJackCombobox.Change;
var
FindName: string;
begin
if not (csDesigning in ComponentState) and FFocused then
begin
fgridquery := FDataLink.DataSource.DataSet;
if Text <> '' then
begin
if Text[1] in ['0'..'9'] then
FindName := FGridNumber
else if (Text[1] in ['A'..'Z']) or (Text[1] in ['a'..'z']) then
FindName := FGridEnglish
else
FindName := FGridChinese;
end;
fGridQuery.Locate(FindName, Text, [loPartialKey]);
if not FDowned then
ShowComboBox;
end;
inherited;
end;
procedure TJackCombobox.DoEnter;
begin
FFocused := True;
inherited;
end;
procedure TJackCombobox.DoExit;
begin
FFocused := False;
if FDowned then
HideComboBox;
inherited;
end;
{ TMyComboBox }
constructor TMyComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit := TJackCombobox(AOwner);
ControlStyle := ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];
Width := FEdit.GridWidth;
Height := 130;
Options := [dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect,
dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit];
DataSource := FEdit.FDataSource;
ReadOnly := True;
ScrollBars := ssVertical;
Visible := False;
Parent := TWinControl(AOwner);
end;
procedure TMyComboBox.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style := style or ws_popup;
EXStyle := EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;
procedure TMyComboBox.WMSetFocus(var Msg: TWMSetFocus);
begin
inherited;
FEdit.SetFocus;
end;
end.