📄 toolctrlseh.pas
字号:
{*******************************************************}
{ }
{ EhLib v2.2 }
{ Tool controls }
{ }
{ Copyright (c) 2001 by Dmitry V. Bolshakov }
{ }
{*******************************************************}
unit ToolCtrlsEh;
{$I EhLib.Inc}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
{$IFDEF EH_LIB_6} Variants, {$ENDIF}
StdCtrls, Mask, Db, DBCtrls, Buttons, ExtCtrls, Menus, ComCtrls, CommCtrl;
const
CM_IGNOREEDITDOWN = WM_USER + 102;
type
IComobEditEh = interface
['{B64255B5-386A-4524-8BC7-7F49DDB410F4}']
procedure CloseUp(Accept: Boolean);
end;
TFieldsArrEh = array of TField;
{ Standard events }
TButtonClickEventEh = procedure(Sender: TObject; var Handled: Boolean) of object;
TButtonDownEventEh = procedure(Sender: TObject; TopButton: Boolean;
var AutoRepeat: Boolean; var Handled: Boolean) of object;
TCloseUpEventEh = procedure(Sender: TObject; Accept: Boolean) of object;
TNotInListEventEh = procedure(Sender: TObject; NewText: String;
var RecheckInList: Boolean) of object;
TUpdateDataEventEh = procedure(Sender: TObject; var Handled: Boolean) of object;
{ TEditButtonControlEh }
TEditButtonStyleEh = (ebsDropDownEh, ebsEllipsisEh, ebsGlyphEh, ebsUpDownEh,
ebsPlusEh, ebsMinusEh);
TEditButtonControlEh = class(TSpeedButton)
private
FActive: Boolean;
FAlwaysDown: Boolean;
FButtonNum: Integer;
FNoDoClick: Boolean;
FOnDown: TButtonDownEventEh;
FStyle: TEditButtonStyleEh;
FTimer: TTimer;
function GetTimer: TTimer;
procedure ResetTimer(Interval: Cardinal);
procedure SetActive(const Value: Boolean);
procedure SetAlwaysDown(const Value: Boolean);
procedure SetStyle(const Value: TEditButtonStyleEh);
procedure TimerEvent(Sender: TObject);
procedure UpdateDownButtonNum(X, Y: Integer);
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Paint; override;
property Timer: TTimer read GetTimer;
public
procedure Click; override;
procedure EditButtonDown(TopButton: Boolean; var AutoRepeat: Boolean);
procedure SetState(NewState:TButtonState; IsActive:Boolean; ButtonNum:Integer);
procedure SetWidthNoNotify(AWidth:Integer);
property Active: Boolean read FActive write SetActive;
property AlwaysDown: Boolean read FAlwaysDown write SetAlwaysDown;
property Style: TEditButtonStyleEh read FStyle write SetStyle default ebsDropDownEh;
property OnDown: TButtonDownEventEh read FOnDown write FOnDown;
end;
TEditButtonControlLineRec = record
EditButtonControl: TEditButtonControlEh;
ButtonLine: TShape;
end;
TEditButtonControlList = array of TEditButtonControlLineRec;
{ TEditButtonEh }
TEditButtonEh = class(TCollectionItem)
private
FDropdownMenu: TPopupMenu;
FEditControl: TWinControl;
FGlyph: TBitmap;
FHint: String;
FNumGlyphs: Integer;
FOnButtonClick: TButtonClickEventEh;
FOnButtonDown: TButtonDownEventEh;
FOnChanged: TNotifyEvent;
FShortCut: TShortCut;
FStyle: TEditButtonStyleEh;
FVisible: Boolean;
FWidth: Integer;
function GetGlyph: TBitmap;
procedure SetGlyph(const Value: TBitmap);
procedure SetHint(const Value: String);
procedure SetNumGlyphs(Value: Integer);
procedure SetStyle(const Value: TEditButtonStyleEh);
procedure SetVisible(const Value: Boolean);
procedure SetWidth(const Value: Integer);
protected
function CreateEditButtonControl:TEditButtonControlEh; virtual;
procedure Changed; overload;
public
constructor Create(Collection: TCollection); overload; override;
constructor Create(EditControl: TWinControl); reintroduce; overload;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
published
property DropdownMenu: TPopupMenu read FDropdownMenu write FDropdownMenu;
property Glyph: TBitmap read GetGlyph write SetGlyph;
property Hint: String read FHint write SetHint;
property NumGlyphs: Integer read FNumGlyphs write SetNumGlyphs default 1;
//property ShortCut: TShortCut read FShortCut write FShortCut default 32808; //Menus.ShortCut(VK_DOWN, [ssAlt]);
property ShortCut: TShortCut read FShortCut write FShortCut default scNone;
property Style: TEditButtonStyleEh read FStyle write SetStyle default ebsDropDownEh;
property Visible: Boolean read FVisible write SetVisible default False;
property Width: Integer read FWidth write SetWidth default 0;
property OnClick: TButtonClickEventEh read FOnButtonClick write FOnButtonClick;
property OnDown: TButtonDownEventEh read FOnButtonDown write FOnButtonDown;
end;
TEditButtonEhClass = class of TEditButtonEh;
{ TVisibleEditButtonEh }
TVisibleEditButtonEh = class(TEditButtonEh)
public
constructor Create(Collection: TCollection); overload; override;
constructor Create(EditControl: TWinControl); overload;
published
property Visible default True;
property ShortCut default 32808; //Menus.ShortCut(VK_DOWN, [ssAlt]);
end;
{ TEditButtonsEh }
TEditButtonsEh = class(TCollection)
private
FOnChanged: TNotifyEvent;
function GetEditButton(Index: Integer): TEditButtonEh;
procedure SetEditButton(Index: Integer; Value: TEditButtonEh);
protected
FOwner: TPersistent;
function GetOwner: TPersistent; override;
procedure Update(Item: TCollectionItem); override;
public
constructor Create(Owner: TPersistent; EditButtonClass: TEditButtonEhClass);
function Add: TEditButtonEh;
property Items[Index: Integer]: TEditButtonEh read GetEditButton write SetEditButton; default;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
end;
{ TSpecRowEh }
TSpecRowEh = class(TPersistent)
private
FCellsStrings: TStrings;
FCellsText: String;
FColor: TColor;
FFont: TFont;
FOnChanged: TNotifyEvent;
FOwner: TPersistent;
FSelected: Boolean;
FShortCut: TShortCut;
FShowIfNotInKeyList: Boolean;
FUpdateCount: Integer;
FValue: Variant;
FVisible: Boolean;
function GetCellText(Index: Integer): String;
function GetColor: TColor;
function GetFont: TFont;
function IsColorStored: Boolean;
function IsFontStored: Boolean;
function IsValueStored: Boolean;
procedure FontChanged(Sender: TObject);
procedure SetCellsText(const Value: String);
procedure SetColor(const Value: TColor);
procedure SetFont(const Value: TFont);
procedure SetShowIfNotInKeyList(const Value: Boolean);
procedure SetValue(const Value: Variant);
procedure SetVisible(const Value: Boolean);
protected
FColorAssigned: Boolean;
FFontAssigned: Boolean;
function GetOwner: TPersistent; override;
procedure Changed;
public
constructor Create(Owner: TPersistent);
destructor Destroy; override;
function DefaultColor: TColor;
function DefaultFont: TFont;
function LocateKey(KeyValue: Variant): Boolean;
procedure Assign(Source: TPersistent); override;
procedure BeginUpdate;
procedure EndUpdate;
property CellText[Index: Integer]: String read GetCellText;
property Selected: Boolean read FSelected write FSelected;
property UpdateCount: Integer read FUpdateCount;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
published
property CellsText: String read FCellsText write SetCellsText;
property Color: TColor read GetColor write SetColor stored IsColorStored;
property Font: TFont read GetFont write SetFont stored IsFontStored;
property ShortCut: TShortCut read FShortCut write FShortCut default 32814; //Menus.ShortCut(VK_DOWN, [ssAlt]);
property ShowIfNotInKeyList: Boolean read FShowIfNotInKeyList write SetShowIfNotInKeyList default True;
property Value: Variant read FValue write SetValue stored IsValueStored;
property Visible: Boolean read FVisible write SetVisible default False;
end;
{ TSizeGripEh }
TSizeGripPostion = (sgpTopLeft,sgpTopRight,sgpBottomRight,sgpBottomLeft);
TSizeGripChangePosition = (sgcpToLeft,sgcpToRight,sgcpToTop,sgcpToBottom);
TSizeGripEh = class(TCustomControl)
private
FInitScreenMousePos:TPoint;
FInternalMove: Boolean;
FOldMouseMovePos:TPoint;
FParentRect:TRect;
FParentResized:TNotifyEvent;
FPosition: TSizeGripPostion;
FTriangleWindow: Boolean;
function GetVisible: Boolean;
procedure SetPosition(const Value: TSizeGripPostion);
procedure SetTriangleWindow(const Value: Boolean);
procedure SetVisible(const Value: Boolean);
procedure WMMove(var Message: TMessage); message WM_MOVE;
protected
procedure CreateWnd; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure Paint; override;
procedure ParentResized; dynamic;
public
constructor Create(AOwner: TComponent); override;
procedure ChangePosition(NewPosition: TSizeGripChangePosition);
procedure UpdatePosition;
property Position:TSizeGripPostion read FPosition write SetPosition default sgpBottomRight;
property TriangleWindow:Boolean read FTriangleWindow write SetTriangleWindow default True;
property Visible: Boolean read GetVisible write SetVisible;
property OnParentResized:TNotifyEvent read FParentResized write FParentResized;
end;
const
cm_SetSizeGripChangePosition = WM_USER + 100;
{ TPopupMonthCalendarEh }
const
CM_CLOSEUPEH = WM_USER + 101;
type
TPopupMonthCalendarEh = class(TMonthCalendar)
private
procedure CMCloseUpEh(var Message: TMessage); message CM_CLOSEUPEH;
procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
protected
function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override;
function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure PostCloseUp(Accept: Boolean);
public
constructor Create(AOwner: TComponent); override;
property Color;
end;
TDrawButtonControlStyleEh = (bcsDropDownEh, bcsEllipsisEh, bcsUpDownEh,
bcsCheckboxEh, bcsPlusEh, bcsMinusEh);
procedure PaintButtonControlEh(DC: HDC;ARect:TRect;ParentColor:TColor;
Style:TDrawButtonControlStyleEh; DownButton:Integer;
Flat,Active,Enabled:Boolean; State: TCheckBoxState);
function GetDefaultFlatButtonWidth:Integer;
var
FlatButtonWidth: Integer;
procedure GetFieldsProperty(List: TList; DataSet: TDataSet;
Control: TComponent; const FieldNames: String); overload;
function GetFieldsProperty(DataSet: TDataSet; Control: TComponent;
const FieldNames: String):TFieldsArrEh; overload;
procedure DataSetSetFieldValues(DataSet: TDataSet; Fields: String; Value:Variant);
function VarEquals(const V1, V2: Variant): Boolean;
var UseButtonsBitmapCache: Boolean = True;
procedure ClearButtonsBitmapCache;
{$IFNDEF EH_LIB_5}
function Supports(const Instance: IUnknown; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
{$ENDIF}
implementation
uses DBConsts {$IFDEF EH_LIB_6} ,VDBConsts, Types {$ENDIF};
type
TWinControlCracker = class(TWinControl) end;
TControlCracker = class(TControl) end;
{$IFNDEF EH_LIB_5}
function Supports(const Instance: IUnknown; const IID: TGUID; out Intf): Boolean; overload;
begin
Result := (Instance <> nil) and (Instance.QueryInterface(IID, Intf) = 0);
end;
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
var
LUnknown: IUnknown;
begin
Result := (Instance <> nil) and
((Instance.GetInterface(IUnknown, LUnknown) and Supports(LUnknown, IID, Intf)) or
Instance.GetInterface(IID, Intf));
end;
{$ENDIF}
procedure DrawCheck(DC: HDC; R: TRect; AState: TCheckBoxState; AEnabled, AFlat: Boolean);
var
DrawState,oldRgn: Integer;
DrawRect: TRect;
// OldBrushColor: TColor;
// OldBrushStyle: TBrushStyle;
// OldPenColor: TColor;
Rgn, SaveRgn: HRgn;
// Brush,SaveBrush: HBRUSH;
begin
SaveRgn := 0;
oldRgn := 0;
DrawRect := R;
with DrawRect do
if (Right - Left) > (Bottom - Top) then
begin
Left := Left + ((Right - Left) - (Bottom - Top)) div 2;
Right := Left + (Bottom - Top);
end else if (Right - Left) < (Bottom - Top) then
begin
Top := Top + ((Bottom - Top) - (Right - Left)) div 2;
Bottom := Top + (Right - Left);
end;
case AState of
cbChecked:
DrawState := DFCS_BUTTONCHECK or DFCS_CHECKED;
cbUnchecked:
DrawState := DFCS_BUTTONCHECK;
else // cbGrayed
DrawState := DFCS_BUTTON3STATE or DFCS_CHECKED;
end;
if not AEnabled then
DrawState := DrawState or DFCS_INACTIVE;
// with Canvas do
// begin
if AFlat then
begin
{ Remember current clipping region }
SaveRgn := CreateRectRgn(0,0,0,0);
oldRgn := GetClipRgn(DC, SaveRgn);
{ Clip 3d-style checkbox to prevent flicker }
with DrawRect do
Rgn := CreateRectRgn(Left + 2, Top + 2, Right - 2, Bottom - 2);
SelectClipRgn(DC, Rgn);
DeleteObject(Rgn);
end;
if AFlat then InflateRect(DrawRect,1,1);
DrawFrameControl(DC, DrawRect, DFC_BUTTON, DrawState);
if AFlat then
begin
//SelectClipRgn(Handle, SaveRgn);
if oldRgn = 0 then
SelectClipRgn(DC, 0)
else
SelectClipRgn(DC, SaveRgn);
DeleteObject(SaveRgn);
{ Draw flat rectangle in-place of clipped 3d checkbox above }
InflateRect(DrawRect,-1,-1);
FrameRect(DC,DrawRect,GetSysColorBrush(COLOR_BTNSHADOW));
InflateRect(DrawRect,1,1);
FrameRect(DC,DrawRect,GetCurrentObject(DC,OBJ_BRUSH));
end;
// end;
end;
const
DownFlags: array [Boolean] of Integer = (0,DFCS_PUSHED);
FlatFlags: array [Boolean] of Integer = (0,DFCS_FLAT);
EnabledFlags: array [Boolean] of Integer = (DFCS_INACTIVE,0);
IsDownFlags: array [Boolean] of Integer = (DFCS_SCROLLUP, DFCS_SCROLLDOWN);
PressedFlags: array [Boolean] of Integer = (EDGE_RAISED, EDGE_SUNKEN);
procedure DrawEllipsisButton(DC: HDC; ARect: TRect; Enabled, Active, Flat, Pressed: Boolean);
var InterP,PWid,W,H:Integer;
ElRect:TRect;
Brush,SaveBrush: HBRUSH;
begin
ElRect := ARect;
Brush := GetSysColorBrush(COLOR_BTNFACE);
if Flat then
begin
Windows.FillRect(DC, ElRect, Brush);
InflateRect(ElRect,-1,-1)
end else
begin
DrawEdge(DC, ElRect, PressedFlags[Pressed], BF_RECT or BF_MIDDLE);
InflateRect(ElRect,-2,-2);
//Windows.FillRect(DC, ElRect, Brush);
end;
InterP := 2;
PWid := 2;
W := ElRect.Right - ElRect.Left ;//+ Ord(not Active and Flat);
if W < 12 then InterP := 1;
if W < 8 then PWid := 1;
W := ElRect.Left + W div 2 - PWid div 2 + Ord(Pressed) ;//- Ord(not Active and Flat);
H := ElRect.Top + (ElRect.Bottom - ElRect.Top) div 2 - PWid div 2 + Ord(Pressed);
if not Enabled then
begin
Inc(W);Inc(H);
Brush := GetSysColorBrush(COLOR_BTNHILIGHT);
SaveBrush := SelectObject(DC, Brush);
PatBlt(DC, W, H, PWid, PWid, PATCOPY);
PatBlt(DC, W - InterP - PWid, H, PWid, PWid, PATCOPY);
PatBlt(DC, W + InterP + PWid, H, PWid, PWid, PATCOPY);
Dec(W);Dec(H);
SelectObject(DC, SaveBrush);
Brush := GetSysColorBrush(COLOR_BTNSHADOW);
end else
Brush := GetSysColorBrush(COLOR_BTNTEXT);
SaveBrush := SelectObject(DC, Brush);
PatBlt(DC, W, H, PWid, PWid, PATCOPY);
PatBlt(DC, W - InterP - PWid, H, PWid, PWid, PATCOPY);
PatBlt(DC, W + InterP + PWid, H, PWid, PWid, PATCOPY);
SelectObject(DC, SaveBrush);
end;
procedure DrawPlusMinusButton(DC: HDC; ARect: TRect; Enabled, Active, Flat, Pressed, Plus: Boolean);
var PWid,PHet,W,H,PlusInd,MinWH:Integer;
ElRect:TRect;
Brush,SaveBrush: HBRUSH;
begin
ElRect := ARect;
Brush := GetSysColorBrush(COLOR_BTNFACE);
if Flat then
begin
Windows.FillRect(DC, ElRect, Brush);
InflateRect(ElRect,-1,-1)
end else
begin
DrawEdge(DC, ElRect, PressedFlags[Pressed], BF_RECT or BF_MIDDLE);
InflateRect(ElRect,-2,-2);
Windows.FillRect(DC, ElRect, Brush);
end;
MinWH := ElRect.Right - ElRect.Left;//+ Ord(not Active and Flat);
if ElRect.Bottom - ElRect.Top < MinWH then
MinWH := ElRect.Bottom - ElRect.Top;
PWid := MinWH * 4 div 7;
if PWid = 0 then PWid := 1;
PHet := PWid div 3;
if PHet = 0 then PHet := 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -