如何测试控件是否为RichEdit控件 [英] How to test if a control is a RichEdit control
问题描述
Delphi中的大多数TWinControl后代都有一个重写方法CreateParams来定义其子类,例如:"EDIT","COMBOBOX","BUTTON","RICHEDIT"等.
Most TWinControl descendant in Delphi has an override method CreateParams to define it's subclass such as: 'EDIT', 'COMBOBOX', 'BUTTON', 'RICHEDIT' and etc.
CreateSubClass(Params, 'EDIT');
CreateSubClass(Params, 'COMBOBOX');
CreateSubClass(Params, 'BUTTON');
Delphi有很多丰富的编辑控件,包括来自第三方供应商的控件.所有这些控件都是RichEdit的子类.
There are quite a number of rich edit control for Delphi including controls from third party vendors. All those controls are sub class of RichEdit.
我想知道是否有一种方法可以通过测试CreateParams中定义的SubClass来测试RichEdit控件,而不管它是原始供应商吗?
I am wondering if there is a way to test a control is RichEdit regardless of it's original vendor by testing the SubClass defined in CreateParams?
推荐答案
感谢所有反馈.我认为没有办法获取TWinControl的Windows类名称.
Thanks for all the feedback. I think there is no way to get the windows class name for the TWinControl.
这是从JamesB的版本修改而来的IsRichEdit的另一个版本:
Here is another version of IsRichEdit modified from JamesB's version:
type TWinControlAccess = class(TWinControl);
function IsRichEdit(C: TWinControl): boolean;
const A: array[0..8] of string = (
'RICHEDIT',
'RICHEDIT20A', 'RICHEDIT20W',
'RICHEDIT30A', 'RICHEDIT30W',
'RICHEDIT41A', 'RICHEDIT41W',
'RICHEDIT50A', 'RICHEDIT50W'
);
var Info: TWNDClass;
p: pointer;
s: string;
begin
p := TWinControlAccess(C).DefWndProc;
Result := False;
for s in A do begin
if GetClassInfo(HInstance, PChar(s), Info) and (Info.lpfnWndProc = p) then begin
Result := True;
Break;
end;
end;
end;
如果Windows中有RichEdit类的较新版本,我们可能会修改数组A.
We may modify array A if there is newer version of RichEdit class from Windows.
另一个可能但有风险的解决方案是,我只检查控件的VCL类名称是否包含'RichEdit'字符串,因为Delphi或第三方供应商提供的几乎丰富的编辑VCL类都以这种方式命名控件.
Another possible but risky solution is I just check if the control's VCL class name contain 'RichEdit' string as almost rich edit VCL class from Delphi or 3rd party vendors name the controls that way.
这篇关于如何测试控件是否为RichEdit控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!