【问题标题】:Use Styled SysColor for selected text in TEdit对 TEdit 中的选定文本使用 Styled SysColor
【发布时间】:2014-07-31 04:09:45
【问题描述】:

我正在使用 Delphi XE3 构建一个 VCL 应用程序。此 VCL 应用程序使用来自单元 Vcl.Styles 的内置样式设置样式。

在我使用 SysColor 的样式中,clHighlight 已被更改,但是当在 TEdit(或 TComboBoxTComboBoxTMemo)中选择一段文本时,将使用默认系统突出显示颜色(默认为蓝色)用于为所选文本的背景着色。

注意:其他控件将 SysColor clHighlight 用于样式中的选定项目。

问题:如何在样式中指定这种颜色?

【问题讨论】:

    标签: delphi vcl delphi-xe3


    【解决方案1】:

    这是WinApi的一个限制,这些控件使用的高亮颜色不能直接修改。唯一的解决方法是挂钩并用StyleServices.GetSystemColor 函数替换GetSysColor 方法。像这样

    implementation
    
    uses
      DDetours,
      WinApi.Windows,
      Vcl.Styles,
      Vcl.Themes;
    
    var
      TrampolineGetSysColor:  function (nIndex: Integer): DWORD; stdcall;
      GetSysColorOrgPointer : Pointer = nil;
    
    function InterceptGetSysColor(nIndex: Integer): DWORD; stdcall;
    begin
      if StyleServices.IsSystemStyle then
       Result:= TrampolineGetSysColor(nIndex)
      else
       Result:= StyleServices.GetSystemColor(nIndex or Integer($FF000000));
    end;
    
    initialization
     if StyleServices.Available then
     begin
        GetSysColorOrgPointer := GetProcAddress(GetModuleHandle('user32.dll'), 'GetSysColor');
        @TrampolineGetSysColor := InterceptCreate(GetSysColorOrgPointer, @InterceptGetSysColor);
     end;
    finalization
      if GetSysColorOrgPointer<>nil then
        InterceptRemove(@TrampolineGetSysColor);
    
    end. 
    

    之前

    之后

    顺便说一句,VCL Styles Utils 项目包括一个带有这个钩子的单元。

    【讨论】:

    • 将 Vcl.Styles.Hooks 添加到项目后,编辑控件看起来很棒。仅:在单元DDetours 中我需要添加{$Q-} 以避免在启动时出现EIntOverflow 异常。
    • 可以在项目页面code.google.com/p/delphi-detours-library上报告关于detours库的问题
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2012-01-26
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多