delphi – 如何在VCL样式的应用程序中居中设置TListView标题?

delphi – 如何在VCL样式的应用程序中居中设置TListView标题?,第1张

概述我有一个列表视图控件(这里是ListView),我用这样的代码填充它: var Item: TListItem; Column: TListColumn;begin ListView.ViewStyle := vsReport; Column := ListView.Columns.Add; Column.Width := 200; Column.Alignme 我有一个列表视图控件(这里是ListVIEw),我用这样的代码填充它:

var  Item: TListItem;  Column: TListColumn;begin      ListVIEw.VIEwStyle := vsReport;  Column := ListVIEw.Columns.Add;  Column.WIDth := 200;  Column.Alignment:= taCenter;  Column.Caption:= 'Column 1';  Column:= ListVIEw.Columns.Add;  Column.WIDth := 200;  Column.Alignment := taCenter;  Column.Caption := 'Column 2';  Item := ListVIEw.Items.Add;  Item.Caption := 'Item 1';  Item.SubItems.Add('Subitem 1');end;

问题是当我在我的应用程序中使用VCL样式时,文本不在列表视图标题中居中

如何使标题标题在VCL样式的应用程序中居中?

解决方法 负责绘制列标题的样式挂钩从不检查列的文本对齐,并始终以左对齐方式绘制文本,显然是疏忽.

首先创建Vcl.ComCtrls.TListVIEwStyleHook的后代和祖先的类助手,以便我们可以访问我们需要的私有变量.

TListVIEwStyleHookHelper = class helper for TListVIEwStyleHook    function getFheaderHandle: HWnd;  end;TListVIEwStyleHookEx = class(Vcl.ComCtrls.TListVIEwStyleHook)  strict protected    procedure DrawheaderSection(Canvas: TCanvas; R: TRect; Index: Integer;      const Text: string; Ispressed,IsBackground: Boolean); overrIDe;  end;

修复方法:

uses  WinAPI.Commctrl;function TListVIEwStyleHookHelper.getFheaderHandle: HWnd;begin  Result := Self.FheaderHandle;end;procedure TListVIEwStyleHookEx.DrawheaderSection(Canvas: TCanvas; R: TRect;  Index: Integer; const Text: string; Ispressed,IsBackground: Boolean);var  Item: THDItem;  ImageList: HIMAGEList;  DrawState: Tthemedheader;  IconWIDth,IconHeight: Integer;  Details: TthemedElementDetails;  LListVIEw: TListVIEw;  DT_Align: Integer;begin  FillChar(Item,SizeOf(Item),0);  Item.mask := HDI_FORMAT;  header_GetItem(getFheaderHandle,Index,Item);  if IsBackground then    DrawState := thheaderItemnormal  else if Ispressed then    DrawState := thheaderItempressed  else    DrawState := thheaderItemnormal;  Details := StyleServices.GetElementDetails(DrawState);  StyleServices.DrawElement(Canvas.Handle,Details,R);  ImageList := SendMessage(getFheaderHandle,HDM_GEtimageList,0);  Item.mask := HDI_FORMAT or HDI_IMAGE;  InflateRect(R,-2,-2);  IconWIDth := 0;  if (ImageList <> 0) and header_GetItem(getFheaderHandle,Item) then  begin    if Item.fmt and HDF_IMAGE = HDF_IMAGE then    begin      ImageList_Draw(ImageList,Item.iImage,Canvas.Handle,R.left,R.top,ILD_transparent);      ImageList_GetIconSize(ImageList,IconWIDth,IconHeight);      Inc(R.left,IconWIDth + 5);    end;  end;  if IconWIDth = 0 then    Inc(R.left,2);  DT_Align := 0;  if Control is TListVIEw then  begin    LListVIEw := TListVIEw(Control);      if (Index > -1) and (Index < LListVIEw.Columns.Count) then        case LListVIEw.Columns[Index].Alignment of          taleftJustify:            DT_Align := 0;          taRightJustify:            DT_Align := 2;          taCenter:            DT_Align := 1;        end;  end;  DrawControlText(Canvas,Text,R,DT_VCENTER or DT_Align or    DT_SINGLEliNE or DT_END_ELliPSIS);end;

最后我们必须为TListVIEw控件注册我们的扩展样式钩子:

Initialization TCustomStyleEngine.RegisterStyleHook(TListVIEw,TListVIEwStyleHookEx);Finalization TCustomStyleEngine.UnRegisterStyleHook(TListVIEw,TListVIEwStyleHookEx);
总结

以上是内存溢出为你收集整理的delphi – 如何在VCL样式的应用程序中居中设置TListView标题?全部内容,希望文章能够帮你解决delphi – 如何在VCL样式的应用程序中居中设置TListView标题?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1240198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存