为适应多语言,需要对界面控件大小、位置多动态改变,因此需要根据其Caption计算实际像素大小。

找资料未有易用现成的,遂参数其它方法,写以函数处之,代码如下:

uses
TypInfo; function GetTextWidth(AControl: TControl): Integer;
function GetControlFont: TFont;
var
PropInfo: PPropInfo;
begin
Result := nil;
PropInfo := GetPropInfo(AControl.ClassInfo, 'Font');
if Assigned(PropInfo) then
Result := GetObjectProp(AControl, PropInfo) as TFont;
end; var
sCaption: string;
fnt: TFont;
cvs: TCanvas;
begin
Result := ;
if not IsPublishedProp(AControl, 'Caption') then Exit;
sCaption := Trim(GetPropValue(AControl, 'Caption', True));
if sCaption = '' then Exit;
fnt := GetControlFont;
if not Assigned(fnt) then Exit; cvs := TCanvas.Create;
try
cvs.Handle := GetDC();
cvs.Font.Assign(fnt);
Result := cvs.TextWidth(sCaption);
finally
cvs.Free;
end;
end;

界面可根据控件文本长度动态做修正了。比如:

btnConvert.Width := GetTextWidth(btnConvert) + ;

根据上面结论,可封装动态修改控件宽度及位置代码,函数如下:

procedure AdjustPostitionAndWidth(ACtrl: TControl; const AAlignment: TAlignment;
AOffSet: Integer);
function Scale(const v: Integer): Integer;
begin
Result := Trunc(v * Screen.PixelsPerInch / );
end; function GetTextWidth: Integer;
function GetControlFont: TFont;
var
PropInfo: PPropInfo;
begin
Result := nil;
PropInfo := GetPropInfo(ACtrl.ClassInfo, 'Font');
if Assigned(PropInfo) then
Result := GetObjectProp(ACtrl, PropInfo) as TFont;
end; var
sCaption: string;
fnt: TFont;
cvs: TCanvas;
begin
Result := ;
if not IsPublishedProp(ACtrl, 'Caption') then Exit;
sCaption := Trim(GetPropValue(ACtrl, 'Caption', True));
if sCaption = '' then Exit;
fnt := GetControlFont;
if not Assigned(fnt) then Exit; cvs := TCanvas.Create;
try
cvs.Handle := GetDC();
cvs.Font.Assign(fnt);
Result := cvs.TextWidth(sCaption);
finally
cvs.Free;
end;
end; var
os, tw: Integer;
begin
AOffSet := Scale(AOffSet);
tw := GetTextWidth();
if tw + AOffSet < ACtrl.Width then Exit; os := tw + AOffSet - ACtrl.Width;
ACtrl.Width := tw + AOffSet;
case AAlignment of
taCenter:
ACtrl.Left := ACtrl.Left - os div ;
taRightJustify:
ACtrl.Left := ACtrl.Left - os;
end;
end;

如此,多语言显未可无忧矣!

Delphi: 获取控件文本宽度(像素)的更多相关文章

  1. Android在OnCreate中获取控件的宽度和高度

    在Android中,有时需要对控件进行测量,得到的控件宽度和高度可以用来做一些计算.在需要自适应屏幕的情况下,这种计算就显得特别重要.另一方便,由于需求的原因,希望一进入界面后,就能得到控件的宽度和高 ...

  2. Android下在onCreate中获取控件的宽度和高度(通过回调)

    有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息, 而直接调用View组件的getWidth().getHeight().getMeasuredWidth().getMeasu ...

  3. android 获取屏幕的高度和宽度、获取控件在屏幕中的位置、获取屏幕中控件的高度和宽度

    (一)获取屏幕的高度和宽度 有两种方法: 方法1: WindowManager wm = (WindowManager) getContext().getSystemService(Context.W ...

  4. Android 在OnCreate()中获取控件高度与宽度

    试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白. 上代码: 方法一: int ...

  5. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  6. Delphi TcxtreeList控件说明 转

    Delphi TcxtreeList控件说明   树.cxTreeList 属性: Align:布局,靠左,靠右,居中等 AlignWithMargins:带边框的布局 Anchors:停靠 (akT ...

  7. Delphi StringGrid控件的用法

    Delphi StringGrid控件 组件名称:StringGrid         ●固定行及固定列:  StringGrid.FixedCols:=固定行之数;  StringGrid.Fixe ...

  8. javascript控制滚动条的位置,获取控件的位置

    一.如下是定位鼠标在视窗中的位置,先定位视窗和页面直接的距离. function getMousePoint() { var point = {x:0,y:0}; // 如果浏览器支持 pageYOf ...

  9. js获取控件位置以及不同浏览器中的差别

    js获取控件位置(坐标位置)在不同浏览器中的差别. //获取坐标位置 function getpos(e) { var t=e.offsetTop; var l=e.offsetLeft; var h ...

随机推荐

  1. django--用户认证组件

    用户认证组件 用户认证组件: 功能:用session记录登录验证状态 前提:用户表:django自带的auth_user 创建超级用户: python3 manage.py createsuperus ...

  2. sping IOC和DI 初始化和关系

    springIOC和spring DI作为spring core的核心思想,有必要学习下才能更好的使用spring ========================================== ...

  3. Seaweed-FS综合使用测试(转)

    2016-03-16 12:17:48   Seaweed-FS综合使用测试 参考信息 https://github.com/chrislusf/seaweedfs/ https://bintray. ...

  4. SED命令用法整理

    sed '/Started/'q  匹配到Started字符串则退出sed命令 sed '/Started/{/in/q}'  同时匹配到Started和in两个字符时则退出sed命令 ------- ...

  5. linux内核中的const成员是否可以修改?

    本文的基础知识:由于前半部分内容是转的,且不知道原文出处,没法给出原文地址,大家自行百度 const的实现机制 const究竟是如何实现的呢?对于声明为const的内置类型,例如int,short,l ...

  6. LeetCode OJ 77. Combinations

    题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...

  7. vue项目动态控制数据变动时箭头样式

    html代码 <div class="top_precent"> <span :class="{arrow:numPrecent<0}" ...

  8. 免費查看SQL PLAN的工具 - SQL Sentry Plan Explorer

    今天 Terry大 介紹給小弟這個 SQL Sentry Plan Explorer 工具,可以用來看SQL Plan. 什麼? 用SSMS看不就很清楚了嗎? 這個Tool有把SQL Plan幫我們整 ...

  9. mac shortcut

    在Windows系统中,如果你想跳到行首.行尾直接点击home.end键就可以了,但MacBook的相关快捷键就有些区别了,相关快捷键如下: Ctrl+A:到行首(达到Home键的效果) .fn键+左 ...

  10. Python类的进阶.md

    属性绑定 在python中可以给类对象动态的绑定属性 但是由于这种特性,随意动态绑定也会带来麻烦,因此可用__slots__来限制可绑定的属性名称 __slots__的绑定对于子类是不生效的,只对当前 ...