代码之家  ›  专栏  ›  技术社区  ›  Chau Chee Yang

如何测试控件是否为RichEdit控件

  •  2
  • Chau Chee Yang  · 技术社区  · 16 年前

    Delphi中的大多数TwinControl子代都有一个override方法createParams来定义它的子类,例如:“edit”、“comboBox”、“button”、“richedit”等。

    CreateSubClass(Params, 'EDIT');
    CreateSubClass(Params, 'COMBOBOX');
    CreateSubClass(Params, 'BUTTON');
    

    Delphi有很多丰富的编辑控件,包括来自第三方供应商的控件。所有这些控件都是richedit的子类。

    我想知道是否有一种方法可以通过测试createparams中定义的子类来测试控件i s richedit,而不管它是原始供应商?

    4 回复  |  直到 8 年前
        1
  •  2
  •   Remy Lebeau    8 年前

    使用Win32 API GetClassName() RealGetWindowClass() 函数(见) What makes RealGetWindowClass so much more real than GetClassName? 对于它们之间的差异),然后检查结果中是否有各种可用的richedit类名:

    • 'RICHEDIT' (1)
    • 'RICHEDIT20A' 'RICHEDIT20W' (2。X+)
    • 'RICHEDIT50W' (4.1)
    • 'TRichEdit' (VCL包装器)
    • 对于其他第三方包装,等等
        2
  •  1
  •   Chau Chee Yang    16 年前

    感谢所有的反馈。我认为无法获取TwinControl的Windows类名。

    以下是从Jamesb版本修改而来的另一个版本的isrichedit:

    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。

    另一个可能但有风险的解决方案是,我只是检查控件的VCL类名是否包含“rich edit”字符串,就像来自Delphi或第三方供应商的近乎rich edit VCL类那样命名控件。

        3
  •  0
  •   J__    16 年前

    我错过什么了吗?是否不仅仅是一个测试案例:

    if (MyControl is TRichEdit)
    

    if (MyControl is TCustomRichEdit)
    
        4
  •  0
  •   James    16 年前

    你可以使用

     function GetClassInfo(hInstance: HINST; lpClassName: PChar;  var lpWndClass: TWndClass): BOOL;
    

    我想这就是雷米想做的。

    类似:

    Function IsRichEdit(MyControl : TWinControl):Boolean;
    var 
        Info : TWNDClass;
    begin
        Result := False;
        if GetClassInfo(HInstance,PCHAR('RICHEDIT'),Info) and (Info.lpfnWndProc = MyControl.DefWndProc) then 
            Result := True
        else if GetClassInfo(HInstance,PCHAR('RICHEDIT20A'),Info) and (Info.lpfnWndProc = MyControl.DefWndProc)  then 
            Result := True
        else if GetClassInfo(HInstance,PCHAR('RICHEDIT30A'),Info) and (Info.lpfnWndProc = MyControl.DefWndProc)  then 
            Result := True
        else if GetClassInfo(HInstance,PCHAR('RICHEDIT41A'),Info) and (Info.lpfnWndProc = MyControl.DefWndProc)  then 
            Result := True
        else if GetClassInfo(HInstance,PCHAR('RICHEDIT50A'),Info) and (Info.lpfnWndProc = MyControl.DefWndProc)  then 
            Result := True
    end;
    

    如果您使用的是Delphi>2007,那么可能还需要测试“w”(Unicode)版本,例如“richedit20w”。

    编辑 :添加了info.wndproc测试以匹配控件。

    奇怪的是,这对cxcontrol s不起作用,因为cxrichedit不是使用rich edit窗口的控件(它是一个包含的控件,因此需要传递cxcontrol.innercontrol才能返回true)。

    编辑 除了创建的第一个richedit控件外,我无法让它工作。