Search code examples
delphirichedit

How to test if a control is a RichEdit control


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');

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.

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?


Solution

  • Thanks for all the feedback. I think there is no way to get the windows class name for the TWinControl.

    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;
    

    We may modify array A if there is newer version of RichEdit class from Windows.

    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.