下面的代码定义了一个
TcxDBBlobEdit
后代及其自己的 OnKeyPress
。OnKeyPress
(“此处断点”)永远不会被触发。type
TPasswordTcxDBBlobEdit = class(TcxDBBlobEdit) // DevExpress component
private
procedure SetPasswordMode(const Value: Boolean);
protected
FPasswordMode : Boolean;
FPasswordChar : Char;
FPrivateKeyFld: String;
procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
public
property PasswordMode: Boolean read FPasswordMode write SetPasswordMode;
property PasswordChar: Char write FPasswordChar;
property PrivateKeyFld: String write FPrivateKeyFld;
constructor Create(AOwner: TComponent); override;
procedure KeyPress(var Key: Char); override; Key: Char); override;
end;
{ TPasswordTcxDBBlobEdit }
constructor TPasswordTcxDBBlobEdit.Create(AOwner: TComponent);
begin
inherited;
FPasswordMode := false;
FPasswordChar := '*';
end;
procedure TPasswordTcxDBBlobEdit.KeyPress(var Key: Char);
var
lCtrl,
lShift,
lPaste: Boolean;
begin
if not FPasswordMode then Exit; // <-- Breakpoint here
lCtrl := (GetKeyState(VK_CONTROL) < 0); // High bit set
lShift := (GetKeyState(VK_SHIFT) < 0);
lPaste := lCtrl and ((Key = 'V') or (Key = 'v') or (Key = #$16)) // #$16 https://stackoverflow.com/questions/78928063/weird-16-value-for-v-character-in-onkeypress
or
lShift and (GetKeyState(VK_INSERT) < 0);
if not lPaste then
Key := #0;
inherited;
end;
在FormCreate中创建新组件:
procedure TDialoogEditProvider.FormCreate(Sender: TObject);
begin
FEditFTPPrivateKey := TPasswordTcxDBBlobEdit.Create(nil); // Before inherited because this assigns FEditFTPPrivateKey.DataBinding.DataSource/DataField
FEditFTPPrivateKey.PasswordMode := true;
FEditFTPPrivateKey.Parent := PanelFTP;
FEditFTPPrivateKey.Properties.BlobEditKind := bekMemo;
FEditFTPPrivateKey.Properties.BlobPaintStyle := bpsText;
FEditFTPPrivateKey.Properties.ImmediateDropDownWhenActivated := False;
FEditFTPPrivateKey.TabOrder := ComboFTPType.TabOrder + 1;
FEditFTPPrivateKey.Width := ComboFTPType.Width;
FEditFTPPrivateKey.Height := ComboFTPType.Height;
FEditFTPPrivateKey.Left := ComboFTPType.Left;
FEditFTPPrivateKey.Top := ComboFTPType.Top + ComboFTPType.Height + 4;
FEditFTPPrivateKey.ScrollBars := ssVertical;
FEditFTPPrivateKey.Hint := sReadOnlyButPaste;
inherited;
end;
Assigned(FEditFTPPrivateKey.OnKeyPress)
在 FormCreate
BrakNicku 的评论成功了:
继承是
TcxDBBlobEdit
- > TcxDBBlobEdit
-> TcxCustomBlobEdit
它是类型为
(Tcx)Memo
的弹出编辑器,并且 that 需要 OnKeypress handler
。
工作代码现在是:
type
TPasswordTcxDBBlobEdit = class(TcxDBBlobEdit)
private
procedure SetPasswordMode(const Value: Boolean);
procedure MemoKeyPress(Sender: TObject; var Key: Char);
protected
FPasswordMode : Boolean;
FPasswordChar : Char;
FPrivateKeyFld: String;
procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
public
property PasswordMode: Boolean read FPasswordMode write SetPasswordMode;
property PasswordChar: Char write FPasswordChar;
property PrivateKeyFld: String write FPrivateKeyFld;
constructor Create(AOwner: TComponent); override;
procedure PropertiesPopup(Sender: TObject);
end;
procedure TPasswordTcxDBBlobEdit.PropertiesPopup(Sender: TObject);
begin
((Sender as TcxDBBlobEdit).Properties.PopupControl as TcxPopupMemo).OnKeyPress := MemoKeyPress;
inherited;
end;
procedure TPasswordTcxDBBlobEdit.MemoKeyPress(Sender: TObject; var Key: Char);
var
lCtrl,
lShift,
lPaste: Boolean;
begin
if not FPasswordMode then Exit; // Alles toegestaan
lCtrl := (GetKeyState(VK_CONTROL) < 0); // High bit set
lShift := (GetKeyState(VK_SHIFT) < 0);
lPaste := lCtrl and ((Key = 'V') or (Key = 'v') or (Key = #$16)) // #$16 https://stackoverflow.com/questions/78928063/weird-16-value-for-v-character-in-onkeypress
or
lShift and (GetKeyState(VK_INSERT) < 0);
if not lPaste then
Key := #0;
inherited;
end;
(更新了标题并在问题中添加了 DevExpress 标签,因为这是相关的)