有没有 显示字符串宽度为 多少像素 的办法

newair 2003-07-08 04:03:22
如何判断一个字符串占多少像素 ,又相关的函数么

比如800*600的显示器上判断一列字符在屏幕上占的的宽度,随便什么字体,就是看看有没有什么方法
...全文
452 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
DelphiBoy2003 2003-07-17
  • 打赏
  • 举报
回复
我就不回答了
东八泰 2003-07-16
  • 打赏
  • 举报
回复
用canvas.TextWidth不就可以了吗。
Iamfish 2003-07-16
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);

var iHeight,iWidth:integer;

begin
//self.Canvas.Font.Name:='宋体'; //设置字体
//self.Canvas.Font.Size:=20; //字体大小
iHeight:=self.Canvas.TextHeight('1234567');
iWidth:=self.Canvas.TextWidth('1234567');

ShowMessage('H:'+IntToStr(iHeight)+#13+'W:'+IntToStr(iWidth));
end;
Hank 2003-07-14
  • 打赏
  • 举报
回复
TCanvas.TextWidth

Returns the width, in pixels, of a string rendered in the current font.

function TextWidth(const Text: string): Integer;
function TextWidth(const Text: WideString): Integer;

Description

Use TextWidth to determine the length a string will occupy in the image. TextWidth indicates whether a given string will fit in the available space. Other graphical elements in the image such as lines, or additional strings can be positioned to accommodate the width of the text.

TextWidth returns the same value as TextExtent(Text).cx.
CloneCenter 2003-07-09
  • 打赏
  • 举报
回复
TCanvas.TextWidth

procedure TForm1.FormCreate(Sender: TObject);

var
i: Integer;
begin
with PageControl1 do
begin
for i := 0 to PageCount - 1 do
begin
if (Canvas.TextWidth(Pages[i].Caption) * 2) > TabWidth then
TabWidth := Canvas.TextWidth(Pages[i].Caption) * 2;
if (Canvas.TextHeight(Pages[i].Caption) * 2) > TabHeight then
TabHeight := Canvas.TextHeight(Pages[i].Caption) * 2;
end;

end;
end;
ghostmirror 2003-07-09
  • 打赏
  • 举报
回复
有个简单的方法就是你把这个字符串写到(利用canvas.textrect())一个paintbox上面,然后
用一个循环扫描一下就可以知道结果了
呵呵~~~
firetoucher 2003-07-08
  • 打赏
  • 举报
回复
你可以用GetTextMetrics得到字体宽度的像素,然后自己运算一下(注意w和i等宽度是不一样的,不过你也可以得到)
newair 2003-07-08
  • 打赏
  • 举报
回复
没人回答?

那我自己搞定好了

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧